aivision#

Initializing the aivision Class#

An AI Vision Sensor is created by using one of the following constructors:

The aivision(port) constructor creates an aivision object in the specified port.

Parameter

Description

port

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

// Create a new AI Vision Sensor "aiVision" with the aivision class.
aivision aiVision = aivision(PORT1);

The aivision(port, colors, codes) constructor creates an AI Vision object.

Parameter

Description

port

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

colors

The name of one or more Colordesc objects.

codes

The name of one or more Codedesc objects.

// Create a new Color Signature "Red" with the colordesc class.
aivision::colordesc Red = aivision::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.
aivision aiVision = aivision(PORT1, Red);

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

Class Methods#

takeSnapshot()#

This method is called in the following ways:

The takeSnapshot(id, 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

id

The id of the object to look for.

type

A valid ObjectType.

count

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

Returns: An integer representing the number of objects found mathing the description passed as a parameter.

The takeSnapshot(desc, 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

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

Returns: An integer representing the number of objects found mathing the description passed as a parameter.

while (true){ 
  // Take a snapshot of the red objects detected by 
  // the AI Vision Sensor.
  aiVision.takeSnapshot(Red);

  // Clear the screen/reset so that we can display 
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // Print the largest detected object's CenterX
  // coordinate to the Brain's screen.
  Brain.Screen.print("Object Count: %d", aiVision.objectCount);
  // Wait 0.5 seconds before repeating the loop and
  // taking a new snapshot.
  wait(0.5, seconds);
}

largestObject#

The largestObject property contains the largest object detected by the AI Vision Sensor in the latest snapshot.

The object contained in largestObject has fifteen properties that can be accessed to obtain various information about the detected object.

AI Vision Object Properties

Description

id

An integer representing the object’s ID.

type

An objectType representing the object’s type.

originX

An integer representing the top left x position of the object.

originY

An integer representing the top left y position of the object.

centerX

An integer representing the center x position of the object.

centerY

An integer representing the center y position of the object.

width

An integer representing the object’s width in pixels.

height

An integer representing the object’s height in pixels.

angle

A float representing 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

A tagcoords object representing the coordinates of an AprilTag.

className

A char representing the class name of the object.

color

A color representing 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

An integer representing the area of the object`s bounding box in pixels.

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

while (true){ 
    // Take a snapshot of the red objects detected by 
    // the AI Vision Sensor.
    aiVision.takeSnapshot(Red);
    
    // Clear the screen/reset so that we can display 
    // new information.
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);

    // Print the largest detected object's CenterX
    // coordinate to the Brain's screen.
    Brain.Screen.print("Center X: %d", aiVision.largestObject.centerX);
    // Wait 0.5 seconds before repeating the loop and
    // taking a new snapshot.
    wait(0.5, seconds);
  }

objectCount#

The objectCount property contains the number of objects found in the most recent snapshot.

Returns: An integer representing the number of objects found in the most recent snapshot.

while (true){ 
  // Take a snapshot of the red objects detected by 
  // the AI Vision Sensor.
  aiVision.takeSnapshot(Red);

  // Clear the screen/reset so that we can display 
  // new information.
  Brain.Screen.clearScreen();
  Brain.Screen.setCursor(1, 1);

  // Print the largest detected object's CenterX
  // coordinate to the Brain's screen.
  Brain.Screen.print("Object Count: %d", aiVision.objectCount);
  // Wait 0.5 seconds before repeating the loop and
  // taking a new snapshot.
  wait(0.5, seconds);
}

tagDetection()#

The tagDetection(enable) method enables or disables apriltag detection.

Parameters

Description

enable

true to enable AprilTag detection. false to disable it.

Returns: None.

colorDetection()#

The colorDetection(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

A boolean value which enables or disables the merging of adjacent color detections. The default is false.

Returns: None.

modelDetection(enable)#

The modelDetection(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.

startAwb()#

The startAwb() method runs auto white balance.

Returns: None.

set()#

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

Parameters

Description

desc

A Colordesc or Codedesc object.

Returns: None.

timestamp()#

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

Returns: Timestamp of the last status packet as an unsigned 32-bit integer in milliseconds.

installed()#

The installed() method checks for device connection.

Returns: true if the AI Vision Sensor is connected. false if it is not.