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 |
---|---|
|
A valid Smart Port that the AI Vision Sensor is connected to. |
|
Optional. The name of one or more Colordesc objects. |
|
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 |
|
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 |
---|---|
|
The object’s ID. |
|
The object’s type. |
|
The top left x position of the object. |
|
The top left y position of the object. |
|
The center x position of the object. |
|
The center y position of the object. |
|
The object’s width in pixels. |
|
The object’s height in pixels. |
|
The object’s angle in pixels. Note: This is only usable with Color Codes/ |
|
If the AI Vision camera detects the object. |
|
The coordinates of an AprilTag. |
|
The class name of the object. |
|
The object’s color. |
|
The confidence score for this object ranges from 0% - 100%. It represents how confident the AI Vision Sensor is in accurately identifying the object. |
|
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 |
|
Returns: None.
color_detection()#
The color_detection(enable, merge)
method enables or disables color and code object detection.
Parameters |
Description |
---|---|
enable |
|
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 |
|
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.