Vision#

Initializing the Vision Class#

A Vision Sensor is created by using the following constructor:

Vision(port, brightness, sigs)

This constructor uses three parameter:

Parameter

Description

port

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

brightness

Optional. The brightness value for the Vision Sensor, from 1 - 100.

sigs

Optional. The name of one or more Signature objects.

# Create a new Signature "dark_blue" with the Colordesc class.
dark_blue = Signature(1, -3911, -3435, -3673,10879, 11421, 11150,2.5, 0)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the "dark_blue" Signature.
vision_1 = Vision(Ports.PORT1, 100, dark_blue)

This vision_1 AiVision object and dark_blue Signature object will be used in all subsequent examples throughout this API documentation when referring to Vision class methods.

Related Classes

Class Methods#

take_snapshot()#

The take_snapshot(index, count) method takes the current snapshot visible to the Vision Sensor and detects the objects of a given Signature or Code.

Before taking a snapshot you must configure signatures for the Vision Sensor to look for.

Parameters

Description

index

A Signature or Code.

count

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

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

while True: 
    # Create a tuple named "darkblue_objects" of detected objects
    # matching the "dark_blue" Signature.
    darkblue_objects = vision_1.take_snapshot(dark_blue)

    # 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 "dark_blue" Signature using the len command.
    brain.screen.print("object count:", len(vision_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 Vision Sensor in the latest snapshot.

Vision Object Properties

Description

id

The unique ID of the object.

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 width of the object.

height

The height of the object.

angle

The angle of the object.

exists

If the Vision camera detects the object.

Returns: A Vision object or None if it does not exist.

while True: 
    # Create a tuple named "darkblue_objects" of detected objects
    # matching the "dark_blue" Signature.
    darkblue_objects = vision_1.take_snapshot(dark_blue)
    
    # 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: ", vision_1.largest_object.centerX)
    # Wait 0.5 seconds before repeating the loop and
    # taking a new snapshot.
    wait(0.5, SECONDS)

installed()#

The installed() method checks if the Vision Sensor is connected.

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

timestamp()#

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

Returns: The timestamp of the last received status packet in milliseconds.