人工智能视觉#

介绍#

AI视觉传感器使机器人能够检测和跟踪环境中的视觉信息。通过识别物体、颜色和图案,该传感器使机器人能够分析周围环境并对所见内容做出反应。

This page uses AIVision1 as the example AI Vision Sensor name. Color Signature objects (such as redBox) and Color Code objects (such as redBlue) are also used in examples. Replace these names with your own configured names as needed.

以下是所有方法的列表:

Getters

  • takeSnapshot — Captures data for a specific Color Signature or Color Code.

  • objectCount — Returns the number of detected objects as an integer.

  • largestObject — Immediately select the largest object from the snapshot.

  • installed — Whether the AI Vision Sensor is connected to the V5 Brain.

Properties — Object data returned from takeSnapshot.

  • .exists — Whether the object exists in the current detection as a Boolean.

  • .width — Width of the detected object in pixels.

  • .height — Height of the detected object in pixels.

  • .centerX — X position of the object’s center in pixels.

  • .centerY — Y position of the object’s center in pixels.

  • .angle — Orientation of the Color Code in degrees.

  • .originX — X position of the object’s top-left corner in pixels.

  • .originY — Y position of the object’s top-left corner in pixels.

  • .id — Classification ID or AprilTag ID of the object.

  • .score — Confidence score for AI Classifications.

构造函数——手动初始化和配置传感器。

拍摄快照#

takeSnapshot captures an image from the AI Vision Sensor, processes it based on the signature, and updates the objects array. This method can also limit the amount of objects captured in the snapshot.

要使用此方法,必须先在视觉实用程序中配置颜色签名颜色代码

The objects array stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. objects is an empty array if no matching objects are detected.

Default Usage:
AIVision1.takeSnapshot(signature)

Overloads:
AIVision1.takeSnapshot(signature, count)

参数

描述

signature

The signature to retrieve data for.

  • Color Signatures or Color Codes — The name of the AI Vision Sensor, two underscores, and then the Color Signature’s or Color Code’s name.For example: AIVision1__redBox.
  • aivision::ALL_AIOBJS — Cubes, Rings, and Buckyballs.
  • aivision::ALL_TAGS — All AprilTag IDs.

count

可选。设置可返回对象的最大数量,范围为 1 到 24(默认值:8)。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Move forward if an object is detected
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
    if (AIVision1.objects[0].exists) {
      Drivetrain.driveFor(forward, 50, mm);
    }
    wait(50, msec);
  }
}

AI Classifications#

AI视觉传感器可以根据特定的AI分类检测不同的物体。根据在“设备”窗口中配置AI视觉传感器时选择的AI分类模型,可以检测不同的物体。目前可用的模型有:

课堂要素

身份证号码

人工智能分类

0

blueBall

1

greenBall

2

redBall

3

blueRing

4

greenRing

5

redRing

6

blueCube

7

greenCube

8

redCube

V5RC 高风险赛事

身份证号码

人工智能分类

0

mobileGoal

1

redRing

2

blueRing

V5RC 推力反馈

身份证号码

人工智能分类

0

blueBlock

1

redBlock

Color Signatures#

A color signature is a unique color that the AI Vision Sensor can recognize. These signatures allow the AI Vision Sensor to detect and track objects based on their color. Once a color signature is configured, the sensor can identify objects with that specific color in its field of view. Color signatures are used with takeSnapshot to process and detect colored objects in real-time.

In order to use a configured color signature in a project, its name must be the name of the sensor, two underscores, and then the color signature’s name. For example: AIVision1__redBox.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  while (true) {
    // Display when the color signature is detected
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.clearLine(1);
    // Change to any configured Color Signature
    AIVision1.takeSnapshot(AIVision1__redBox);
    if (AIVision1.objects[0].exists) {
      Brain.Screen.print("Color detected!");
    }
    wait(50, msec);
  }
}

Color Codes#

颜色编码是一种由特定顺序排列的颜色符号构成的结构化模式。这些编码使人工智能视觉传感器能够识别预定义的颜色模式。颜色编码可用于识别复杂物体或为自主导航创建独特的标记。

To use a configured color code in a project, its name must be passed as a string in the format: the AI Vision Sensor’s name, followed by two underscores, and then the color code’s name. For example: AIVision1__redBlue.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  while (true) {
    // Display when the color code is detected
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.clearLine(1);
    // Change to any configured Color Code
    AIVision1.takeSnapshot(AIVision1__redBlue);
    if (AIVision1.objects[0].exists) {
      Brain.Screen.print("Code detected!");
    }
    wait(50, msec);
  }
}

对象计数#

objectCount returns the number of items inside the objects array as an integer.

Default Usage:
AIVision1.objectCount

参数

描述

此方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the number of detected objects
  while (true) {
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.clearLine(1);
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
    if (AIVision1.objects[0].exists) {
      Brain.Screen.print("%d", AIVision1.objectCount);
    }
    wait(50, msec);
  }
}

最大对象#

largestObject retrieves the largest detected object from the objects array.

This method can be used to always get the largest object from objects without specifying an index.

Default Usage:
AIVision1.largestObject

参数

描述

此方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display the closest AprilTag's ID
  while (true) {
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.clearLine(1);
    AIVision1.takeSnapshot(aivision::ALL_TAGS);
    if (AIVision1.objects[0].exists) {
      Brain.Screen.print("%d", AIVision1.largestObject.id);
    }
    wait(50, msec);
  }
}

已安装#

installed returns an integer indicating whether the AI Vision Sensor is currently connected to the V5 Brain.

  • 1 — The AI Vision Sensor is connected to the V5 Brain.

  • 0 — The AI Vision Sensor is not connected to the V5 Brain.

参数

描述

此方法没有参数。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display a message if the AI Vision Sensor is detected
  if (AIVision1.installed()){
    Brain.Screen.print("Installed!");
  }
}

对象#

objects returns an array of detected object properties. Use the array to access specific property values of individual objects.

Default Usage:
AIVision1.objects

Properties#

There are ten properties that are included with each object stored in the objects array after takeSnapshot is used.

Some property values are based off of the detected object’s position in the AI Vision Sensor’s view at the time that takeSnapshot was used. The AI Vision Sensor has a resolution of 320 by 240 pixels.

存在#

.exists returns an integer indicating if the index exists in the objects array or not.

  • 1: The index exists.

  • 0: The index does not exist.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Move forward if an object is detected
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
    if (AIVision1.objects[0].exists) {
      Drivetrain.driveFor(forward, 50, mm);
    }
    wait(50, msec);
  }
}

。宽度#

.width returns the width of the detected object in pixels, which is an integer between 1 and 320.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Approach an object until it's at least 100 pixels wide
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);

    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].width < 100) {
        Drivetrain.drive(forward);
      } else {
        Drivetrain.stop();
      }
    } else {
      Drivetrain.stop();
    }
    wait(50, msec);
  }
}

。高度#

.height returns the height of the detected object in pixels, which is an integer between 1 and 240.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Approach an object until it's at least 90 pixels tall
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);

    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].height < 90) {
        Drivetrain.drive(forward);
      } else {
        Drivetrain.stop();
      }
    } else {
      Drivetrain.stop();
    }
    wait(50, msec);
  }
}

.centerX#

.centerX returns the x-coordinate of the detected object’s center in pixels, which is an integer between 0 and 320.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Turn until an object is directly in front of the sensor
  Drivetrain.setTurnVelocity(10, percent);
  Drivetrain.turn(right);
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);

    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].centerX > 140 && AIVision1.objects[0].centerX < 180) {
        Drivetrain.stop();
      }
    }

    wait(10, msec);
  }
}

.centerY#

.centerY returns the y-coordinate of the detected object’s center in pixels, which is an integer between 0 and 240.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Approach an object until it's close to the sensor
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);

    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].centerY < 150) {
        Drivetrain.drive(forward);
      } else {
        Drivetrain.stop();
      }
    } else {
      Drivetrain.stop();
    }
    wait(50, msec);
  }
}

。角度#

.angle returns the orientation of the detected color code or AprilTag ID in degrees, which is a double between 0 and 360.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Turn left or right depending on how a configured
  // Color Code is rotated
  while (true) {
    AIVision1.takeSnapshot(AIVision1__redBlue);
    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].angle > 50 && AIVision1.objects[0].angle < 100) {
        Drivetrain.turn(right);
      }
      else if (AIVision1.objects[0].angle > 270 && AIVision1.objects[0].angle < 330) {
        Drivetrain.turn(left);
      }
      else {
        Drivetrain.stop();
      }
    } else {
      Drivetrain.stop();
    }
    wait(50, msec);
  }
}

.originX#

.originX returns the x-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 320.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display if an object is to the left or the right
  while (true) {
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].originX < 120) {
        Brain.Screen.print("To the left!");
      } else {
        Brain.Screen.print("To the right!");
      }
    } else {
      Brain.Screen.print("No objects");
    }
    wait(100, msec);
  }
}

.originY#

.originY returns the y-coordinate of the top-left corner of the detected object’s bounding box in pixels, which is an integer between 0 and 240.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display if an object is close or far
  while (true) {
    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].originY < 110) {
        Brain.Screen.print("Close");
      } else {
        Brain.Screen.print("Far");
      }
    }
    wait(100, msec);
  }
}

。ID#

.id returns the ID of the detected AI Classification or AprilTag ID as an integer.

有关 AI 分类,请参阅对应 ID 的 AI 分类表。

For an AprilTag ID, the .id property represents the detected AprilTag’s ID number in the range of 0 to 36. For an AI Classification, the ID corresponds to the predefined ID as shown below.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Move forward when AprilTag ID 1 is detected
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_TAGS);
    if (AIVision1.objects[0].exists) {
      if (AIVision1.objects[0].id == 1) {
        Drivetrain.drive(forward);
      }
    } else {
      Drivetrain.stop();
    }
    wait(50, msec);
  }
}

。分数#

.score returns the confidence score of the detected AI Classification as an integer between 1 and 100.

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display if a score is confident
  while (true) {
    AIVision1.takeSnapshot(aivision::ALL_AIOBJS);

    if (AIVision1.objects[0].exists) {
      Brain.screen.clearScreen();
      Brain.screen.setCursor(1, 1);

      if (AIVision1.objects[0].score > 95) {
        Brain.Screen.print("Confident");
      } else {
        Brain.Screen.print("Not confident");
      }
    }

    wait(50, msec);
  }
}

构造函数#

Constructors are used to manually create aivision, aivision::colordesc, and aivision::codedesc objects, which are necessary for configuring the AI Vision Sensor outside of VEXcode.

aivision#

aivision creates an AI Vision Sensor.

Default Usage:
aivision(port, sigs)

参数

描述

port

AI视觉传感器连接到哪个智能端口(1至21)。

sigs

Optional. The name of one or more signatures:

// Create the Color Signatures
aivision::colordesc AIVision1__greenBox(1, 85, 149, 46, 23, 0.23);
aivision::colordesc AIVision1__blueBox(2, 77, 135, 125, 27, 0.29);

// Create a Color Code
aivision::codedesc AIVision1__greenBlue(1, AIVision1__greenBox, AIVision1__blueBox);

/*
Create a Vision Sensor with the following values:
port: Port 1
sigs: greenBlue and AI Classifications
*/
aivision AIVision1(PORT11, AIVision1__greenBlue, aivision::ALL_AIOBJS);

aivision::colordesc#

aivision::colordesc creates a color signature. Up to seven different color signatures can be stored on an AI Vision Sensor at once.

Default Usage:
aivision::colordesc(index, red, green, blue, hangle, hdsat)

范围

描述

index

The colordesc object’s index, from 1 - 7.
Note: Creating two colordesc objects with the same index number will cause the second created object to override the first.

red

颜色的红色值,范围从 0 到 255。

green

颜色的绿色值,范围从0到255。

blue

颜色的蓝色值,范围从 0 到 255。

hangle

色相允许的变化范围以色轮上的角度范围(1-40)表示。数值越高,允许的旋转偏差越大,偏离基准色相的幅度也越大。

hdsat

允许的饱和度偏差范围为 0.10 至 1.00。该值表示颜色可以在完整饱和度范围内偏移的比例(例如,0.10 = ±10% 饱和度)。

/*
Create a Color Signature with the following values:
index: 1
red: 81
green: 149
blue: 44
hangle: 17
hsat: 0.23
*/

aivision::colordesc AIVision1__greenBox(1, 81, 149, 44, 17, 0.23);

aivision::codedesc#

aivision::codedesc creates a color code. It requires at least two already created aivision::colordesc in order to be used. Up to eight different color codes can be stored on an AI Vision Sensor at once.

Default Usage:
aivision::codedesc(sigs)

参数

描述

sigs

Two or more previously created aivision::colordesc, named using the format AIVisionSensor__colorSignature (for example, AIVision1__blueBox), separated by commas. A color code can include up to five different color signatures.

// Create the Color Signatures
aivision::colordesc AIVision1__greenBox(1, 85, 149, 46, 23, 0.23);
aivision::colordesc AIVision1__blueBox(2, 77, 135, 125, 27, 0.29);

// Create a Color Code with the Color Signatures
aivision::codedesc AIVision1__greenBlue(1, AIVision1__greenBox, AIVision1__blueBox);