AiVision#

Initializing the AiVision Class#

An AI Vision Sensor is created by using the following constructor:

AiVision(port, colors, codes)

This constructor uses three parameter:

Parameter

Description

port

A valid Smart Port that the AI Vision Sensor is connected to.

colors

Optional. The name of one or more Colordesc objects.

codes

Optional. The name of one or more Codedesc objects.

# Create a new Color Signature "red" with the Colordesc class.
red = Colordesc(1, 207, 19, 25, 10.00, 0.20)
# Create a new AI Vision Sensor "ai_vision" with the AiVision
# class, with the "red' Colordesc.
ai_vision = AiVision(Ports.PORT1, red)

This ai_vision AiVision object and red Colordesc object will be used in all subsequent examples throughout this API documentation when referring to AiVision class methods.

Class Methods#

take_snapshot()#

The take_snapshot(type, count) method takes the current snapshot visible to the AI Vision Sensor and detects the objects of a given signature, code, or signature id.

Parameters

Description

type

A Colordesc, Codedesc, Tagdesc, or AiObjdesc object.

count

Optional. The maximum number of objects to obtain. The default is 8.

Returns: A tuple of of detected objects or an empty tuple if nothing is available.

while True:
    # Create a tuple named "red_objects" of detected objects
    # matching the "red" Colordesc.
    red_objects = ai_vision.take_snapshot(red)
    
    # Clear the screen/reset so that we can display 
    # new information.
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    
    # Print the amount of objects detected that matched
    # the "red" Colordesc using the len command.
    brain.screen.print("Object Count:", len(red_objects))
    # Wait 0.5 seconds before repeating the loop and
    # taking a new snapshot.
    wait(0.5, SECONDS)

largest_object()#

The largest_object() method retrieves the largest object detected by the AI Vision Sensor in the latest snapshot.

The object returned by largest_object() has fifteen properties that can be accessed to obtain various information about the detected object.

AI Vision Object Properties

Description

id

The object’s ID.

type

The object’s type.

originX

The top left x position of the object.

originY

The top left y position of the object.

centerX

The center x position of the object.

centerY

The center y position of the object.

width

The object’s width in pixels.

height

The object’s height in pixels.

angle

The object’s angle in pixels. Note: This is only usable with Color Codes/Codedesc objects.

exists

If the AI Vision camera detects the object. True if an object is detected, False if an object is not detected.

tag

The coordinates of an AprilTag.

className

The class name of the object.

color

The object’s color.

score

The confidence score for this object ranges from 0% - 100%. It represents how confident the AI Vision Sensor is in accurately identifying the object.

area

The area of the object`s bounding box in pixels.

Returns: An object or None if it does not exist.

while True: 
    # Create a tuple named "red_objects" of detected objects
    # matching the "red" Colordesc.
    red_objects = ai_vision.take_snapshot(red)
    
    # Clear the screen/reset so that we can display 
    # new information.
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)

    # Print the largest detected object's CenterX
    # coordinate to the Brain's screen.
    brain.screen.print("Center X: ", ai_vision.largest_object.centerX)
    # Wait 0.5 seconds before repeating the loop and
    # taking a new snapshot.
    wait(0.5, SECONDS)

object_count()#

The object_count() method returns the amount of detected objects in the last use of the take_snapshot method.

Returns: An integer as the amount of detected objects in the last use of the take_snapshot method.

tag_detection()#

The tag_detection(enable) method enables or disables AprilTag detection.

Parameters

Description

enable

True to enable AprilTag detection. False to disable it.

Returns: None.

color_detection()#

The color_detection(enable, merge) method enables or disables color and code object detection.

Parameters

Description

enable

True to enable color and color code detection. False to disable it.

merge

Optional. A boolean value which enables or disables the merging of adjacent color detections.

Returns: None.

model_detection(enable)#

The model_detection(enable) method enables or disables AI model object detection.

Parameters

Description

enable

True to enable AI model object detection. False to disable it.

Returns: None.

start_awb()#

The start_awb() method runs auto white balance.

Returns: None.

set()#

The set() method sets a new Color Signature or Color Code.

Returns: None.

installed()#

The installed() method checks for device connection.

Returns: True if the AI Vision Sensor is connected. False if it is not.

timestamp()#

The timestamp() method requests the timestamp of the last received status packet from the AI Vision Sensor.

Returns: Timestamp of the last received status packet in milliseconds.