Coding with the AI Vision Sensor in VEXcode EXP C++

Make sure you have Color Signatures and Color Codes configured with your AI Vision Sensor so they can be used with your blocks. To learn more about how to configure them, you can read the articles below:

The AI Vision Sensor can also detect AI Classifcations and AprilTags. To learn how to enable these detection modes, go here:

To learn more detail about these individual commands and how to use them in VEXcode, go to the API site.


Obtain Visual Data with the AI Vision Sensor

Every AI Vision Sensor command will start with the name of the configured AI Vision Sensor. For all the examples in this article, the name of the AI Vision Sensor used will be AIVision.

takeSnapshot

The takeSnapshot method takes a picture of what the AI Vision Sensor is currently seeing and pulls data from that snapshot that can then be used in a project. When a snapshot is taken, you need to specify what type of object the AI Vision Sensor should collect data of:

  • Color Signature or Color Code
    • These Visual Signatures start with the name of the AI Vision Sensor, double underscore, and then the name of the Visual Signature, for example: AIVision1__Blue.
  • AI Classifications - aivision::ALL_AIOBJS
  • AprilTags - aivision::ALL_TAGS

Taking a snapshot will create a array of all of the detected objects that you specified. For instance, if you wanted to detect a "Blue" Color Signature, and the AI Vision Sensor detected 3 different blue objects, data from all three would be put in the array.

In this example, a snapshot is taken of the "Blue" Color Signature from the AI Vision Sensor named AIVision1. It displays the number of objects detected in the array and captures a new snapshot every 0.5 seconds.

while (true) {
// Get a snapshot of all Blue Color objects.
AIVision.takeSnapshot(AIVision1__Blue);

// Check to make sure an object was detected in the snapshot before pulling data.
if (AIVision.objectCount > 0) {

Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
Brain.Screen.print(AIVision1.objectCount);
}
wait(5, msec);
}

objects

Every object from a snapshot has different properties that can be used to report information about that object. The objects method allows you to access these properties.

The available properties are as follows:

  • id
  • centerX and centerY
  • originX and originY
  • width
  • height
  • angle
  • exists
  • score

To access an object's property, use the name of the AI Vision Sensor, followed by the objects method, and then the object's index.

The object index indicates which specific object's property you want to retrieve. After taking a snapshot, the AI Vision Sensor automatically sorts objects by size. The largest object is assigned index 0, with smaller objects receiving higher index numbers.

For example, calling the largest's object's width would be AIVision1.objects[0].width.

id

The id property is only available for AprilTags and AI Classifications.

Three square identification markers labeled with IDs 0, 9, and 3, each with corresponding coordinates and size measurements displayed in white text. ID 0 is on the left, ID 9 is on the right, and ID 3 is at the bottom center. Each marker has a unique black-and-white pattern inside a square.

For an AprilTag, the id property represents the detected AprilTag(s) ID number.

Identifying specific AprilTags allows for selective navigation. You can program your robot to move towards certain tags while ignoring others, effectively using them as signposts for automated navigation.

Two balls and two rings identified in the image, with labels indicating their position, size, and score. The red ball is on the left, the blue ball is on the right, the green ring is at the bottom left, and the red ring is at the bottom right. Each object is outlined with a white box, and details such as X, Y coordinates, width, height, and a score of 99% are displayed in white text.

For AI Classifications, the id property represents the specific type of AI Classification detected.

Identifying specific AI Classifications allows the robot to only focus on specific objects, such as only wanting to navigate towards a red Buckyball, not a blue one.

Go to these articles for more information on AprilTags and AI Classifications and how to enable their detection in the AI Vision Utility.

centerX and centerY

This is the center coordinates of the detected object in pixels.

A blue Buckyball being tracked by a computer vision system. The object is outlined with a white square, and inside the outline is a smaller red square surrounding a centered white cross. In the top-left corner of the image, a label indicates the object is blue, with coordinates X:176, Y:117, and dimensions W:80, H:78.

CenterX and CenterY coordinates help with navigation and positioning. The AI Vision Sensor has a resolution of 320 x 240 pixels.

Two blue cubic objects tracked by a vision system. The upper object is labeled with coordinates X:215, Y:70, and dimensions W:73, H:84, with a white outline and a centered white cross. The lower object is labeled with coordinates X:188, Y:184, and dimensions W:144, H:113, also outlined in white with a centered white cross.

You can see that an object closer to the AI Vision Sensor will have a lower CenterY coordinate than an object that is farther away.

In this example, because the center of the AI Vision Sensor's view is (160, 120), the robot will turn right until a detected object's centerX coordinate is greater than 150 pixels, but less than 170 pixels.

while (true) {
  // Get a snapshot of all Blue Color objects.
  AIVision.takeSnapshot(AIVision__Blue);

  // Check to make sure an object was detected in the snapshot before pulling data.
  if (AIVision.objectCount > 0) {

    if (AIVision.objects[0].centerX > 150.0 && 170.0 > AIVision.objects[0].centerX) {
      Drivetrain.turn(right);
    } else {
      Drivetrain.stop();
    }
  }
  wait(5, msec);
}

originX and originY

OriginX and OriginY is the coordinate at the top-left corner of the detected object in pixels.

A blue Buckyball being tracked by a vision system. A white outline surrounds the object, with a centered white cross inside the outline. The top-left label indicates the object's color as blue, along with coordinates X:176, Y:117, and dimensions W:80, H:78. A small red square highlights the object's top-left corner.

OriginX and OriginY coordinates help with navigation and positioning. By combining this coordinate with the object's Width and Height, you can determine the size of the object's bounding box. This can help with tracking moving objects or navigating between objects.

width and height

This is the width or height of the detected object in pixels.

The image shows a blue Buckyball with a white square outline tracking it. The top left corner has a label indicating it is a blue object, with coordinates X:176, Y:117, and dimensions W:80, H:78. Red arrows highlight the width and height of the object.

The width and height measurements help identify different objects. For example, a Buckyball will have a larger height than a Ring.

Two blue cubic objects being tracked by a visual recognition system. The upper cube has a white outline with a label indicating its position as X:215, Y:70 and dimensions W:73, H:84. The lower cube has a similar white outline with the label displaying X:188, Y:184 and dimensions W:144, H:113. Each cube has a centered white cross, likely indicating the focal point for tracking. The labels highlight the measurements and tracking data for each object.

Width and height also indicate an object's distance from the AI Vision Sensor. Smaller measurements usually mean the object is farther away, while larger measurements suggest it's closer.

In this example, the width of the object is used for navigation. The robot will approach the object until the width has reached a specific size before stopping.

while (true) {
  // Get a snapshot of all Blue objects.
  AIVision.takeSnapshot(AIVision1__Blue);

  // Check to make sure an object was detected in the snapshot before pulling data.
  if (AIVision.objectCount > 0) {

    if (AIVision.objects[0].width < 250.0) {
      Drivetrain.drive(forward);
    } else {
      Drivetrain.stop();
    }
  }
  wait(5, msec);
}

angle

A rotating gif showing red and green blocks. When the blocks are positioned perfectly horizontal from red to green, they are shown to be 0 degrees. If the red block is on top of the green block vertically, it is at 90 degrees. If the blocks are horizontally green to red, it is 180 degrees. If the green block is on top of the red block vertically, it is 20 degrees.

The angle property is only available for Color Codes and AprilTags.

This represents if the detected Color Code or AprilTag is orientated differently.

A stack of two cubes, one green on top and one blue on the bottom, being tracked by a vision system. A white outline surrounds both cubes, with a white cross centered on the green cube. The label at the bottom of the image displays Green_Blue A:87°, indicating the detected colors and an angle measurement. Below that, the coordinates are arrayed as X:117, Y:186, with dimensions W:137, H:172, representing the position and size of the stacked cubes in the frame.

You can see if the robot is orientated differently in relation to the Color Code or AprilTag and make navigation decisions according to that.

Two cubes, one green and one blue, placed side by side and tracked by a vision system. A white outline surrounds both cubes with a white cross at the center. The top-left label indicates Green_Blue A:0°, referencing the detected colors and an angle measurement. Below that, the coordinates are shown as X:150, Y:102, with dimensions W:179, H:109, representing the position and size of the cubes within the frame.

For instance, if a Color Code isn't detected at a proper angle, then the object it represents may not be able to be picked up properly by the robot.

score

The score property is used when detecting AI Classifications with the AI Vision Sensor.

The image shows four objects being tracked by a vision system: two balls and two rings. The red ball is labeled with coordinates X:122, Y:84, W:67, H:66, and a score of 99%. The blue ball has X:228, Y:86, W:70, H:68, with a score of 99%. The green ring has coordinates X:109, Y:186, W:98, H:92, and a score of 99%. The red ring is labeled X:259, Y:187, W:89, H:91, with a score of 99%. Each object is outlined in white, indicating tracking accuracy.

The confidence score indicates how certain the AI Vision Sensor is about its detection. In this image, it's 99% confident in identifying these four objects' AI Classifications. You can use this score to ensure your robot only focuses on highly confident detections.

exists

The exists property is used to detect if a specified Visual Signature has been detected in the last taken snapshot.

This lets you check if any detected objects were detected in the previous snapshot. This property will return a True when an object exists, and a False when the object does not exist.


objectCount

The objectCount method returns the amount of detected objects in the last snapshot.

The AI Vision Utility interface with two blue cubes detected on the left side, each marked with their X and Y coordinates and dimensions. The system is connected, and AprilTags are toggled on, while AI Classification is off. On the right, the Blue color settings are displayed with adjustable hue and saturation ranges, set at 22 and 0.34, respectively. There is an option to add or set color and freeze video. The firmware is up to date, running version 1.0.0.b16, and a close button is available at the bottom.

In this example, two objects have been detected with the Color Signature "Blue". They both will be put in the array when the takeSnapshot method is used.

This code snippet continuously updates the EXP Brain with the number of detected objects. Based on the example provided, it will repeatedly send the value 2, indicating that two objects have been detected.

while (true) {
// Get a snapshot of all Blue objects.
AIVision.takeSnapshot(AIVision__Blue);

Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);

// Check to make sure an object was detected in the snapshot before pulling data.
if (AIVision.objectCount > 0) {
Brain.Screen.print(AIVision1.objectCount);
}
wait(5, msec);
}

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: