Using the AI Vision Sensor in VEXcode V5 C++

This article provides an overview of the functions available to use the AI Vision Sensor in Python, along with simple instructions on how to use them. You will learn the four steps in the process of using the AI Vision Sensor, obtaining data, seeing the amount of objects obtained, selecting what object from the data to use, and what data from the object to pull.

Obtain Visual Data with the AI Vision Sensor

To gather information from its surroundings, the AI Vision Sensor takes an image of what it's currently looking at. Any objects that it identifies with the specified Visual Signature are stored internally within the AI Vision Sensor, which can then be used by other functions.

.takeSnapshot

Any Visual Signatures will need to be configured before they can be used as a parameter. When using the method, specify the signature parameter as the sensor's name, double underscore, followed by the Visual Signature's name.

utility exm.PNG

AIVision6.takeSnapshot(AIVision6__Blue)
AIVision6.takeSnapshot(AIVision6__Red)
AIVision6.takeSnapshot(AIVision6__Green)

Example

In this example, the .takeSnapshot method is employed to search for any objects corresponding to the "Blue" Visual Signature. Then, the code will display the count of objects found for that Visual Signature.

int main() {
while (true) {
// Take a snapshot to check for detected objects
AIVision6.takeSnapshot(Blue);

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

// Print how many objects were detected
Brain.Screen.print("object count: %d", AIVision6.objectCount);

wait(0.5, seconds);
}
}

Determine How Many Objects were Detected

Because the .takeSnapshot method can detect multiple objects with the same Visual Signature at a time, you can find out how many objects have been detected with the .objectCount property.

Brain.Screen.Print(“%d”, AIVision1.objectCount)

Example

In this example, the .objectCount property is being used to check if at least 1 Visual Signature was detected in the last snapshot, then printing either true or false if an object was detected. If an object was detected, it then prints the count of how many objects were detected total.

int main() {
while (true) {
// Take a snapshot to check for detected objects
AIVision6.takeSnapshot(Blue);

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

// if objects were found, print how many
if (AIVision6.objectCount > 0) {
Brain.Screen.newLine();
Brain.Screen.print("object count: %d", AIVision6.objectCount);
}

wait(0.5, seconds);
}
}

Select Which Object To Get Data From

There are two methods to extract data from the AI Vision Sensor. One approach is to directly retrieve information from the largest object detected in the most recent use of the .takeSnapshot method using the AIVision.largestObject property. The other method uses the AIVision.objects[] property and specifying an object index.

AIVision.largestObject

ai.vision.largest_object() is a property to access the largest object.

int main() {
while (true) {
// Take a snapshot to check for detected objects
AIVision6.takeSnapshot(BlueBox);

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

// If objects were found, print the location
if (AIVision6.objects[0].exists) {
Brain.Screen.print("Center X: %d, AIVision6.largestObject[0].centerX);
} else {
Brain.Screen.print("no object");
}

wait(0.5, seconds);
}
}

AIVision.objects

Another method to pull data is by specifying the index of the object you want to extract data from. The snapshot returns the detected objects with the largest object first at index 0 and orders the objects from largest to smallest.

int main() {
while (true) {
// Take a snapshot to check for detected objects
AIVision6.takeSnapshot(BlueBox);

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

// If objects were found, print the location
if (AIVision6.objects[0].exists) {
Brain.Screen.print("Center X: %d, AIVision6.objects[0].centerX);
} else {
Brain.Screen.print("no object");
}

wait(0.5, seconds);
}
}

Select What Data to Pull From an Object

From each object, there are eight properties that can be called.

  • .height
  • .width
  • .centerX
  • .centerY
  • .originX
  • .originY
  • .angle
  • .exists

.height

The .height property returns how tall the detected object is in pixels.

Use this code to return the .height property:

AIVision6.objects[0].height

The .height property will return a value between 0 and 240 pixels.

.width

The .width property returns how wide the detected object is in pixels.

Use this code to return the .width property:

AIVision6.objects[0].width

The .width property will return a value between 0 and 320.

.centerX

The .centerX property reports the X coordinate of the center of the detected Visual Signature.

Use this code to return the .centerX property:

AIVision6.objects[0].centerX

The .centerX property will return a number between 0 and 320.

.centerY

The .centerY property reports the Y coordinate of the center of the detected Visual Signature.

Use this code to return the .centerY property:

AIVision6.objects[0].centerY

The .centerYproperty will return a number between 0 and 240.

.originX

The .originX property reports the X coordinate of the top-leftmost corner of the detected Visual Signature.

Use this code to return the .originX property:

AIVision6.objects[0].originX

The .originXproperty will return a number between 0 and 320.

.originY

The .originY property reports the Y coordinate of the top-leftmost corner of the detected Visual Signature.

Use this code to return the .originY property:

AIVision6.objects[0].originY

The .originYproperty will return a number between 0 and 240.

.angle

The .angle property returns the value of the Color Code's angle.

Use this code to return the .angle property:

AIVision6.objects[0].angle

The .angle property returns the detected Color Code's orientation, ranging from 0 to 359.99 degrees.

.exists

.exists is a Boolean property that return true if it is a valid object. 

It will return false if the object is invalid, such as if a negative integer is used. If an object is invalid, all other properties will return 0s as their value.

int main() {
while (true) {
// Take a snapshot to check for detected objects
AIVision6.takeSnapshot(BlueBox);

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

// If objects were found, print the location
if (AIVision6.objects[0].exists) {
Brain.Screen.print("Center X: %d, AIVision6.objects[0].centerX);
} else {
Brain.Screen.print("no object");
}

wait(0.5, seconds);
}
}

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

Last Updated: