AI视觉传感器#

介绍#

aivision 类)用于控制和访问来自 V5 AI 视觉传感器的数据。AI 视觉传感器可以检测:

  • AI分类(例如游戏对象)

  • AprilTag IDs

  • 自定义颜色签名

  • 自定义颜色代码

它提供对象数据,包括位置、大小、方向、分类 ID 和置信度分数。

The sensor processes visual information using an onboard AI model selected in the AI Vision Utility within VEXcode. The selected model determines which AI Classifications the sensor can detect. When using VS Code, the AI model must first be configured in VEXcode before it can be used in your program. Detected objects are returned through the objects array after takeSnapshot is called.

类构造函数#

aivision(
  int32_t index,
  Args&... sigs );

参数#

范围

类型

描述

index

int32_t

AI视觉传感器连接的智能端口,记为 PORTx,其中 x 是端口号(例如, PORT1)。

sigs

colordesc, codedesc, or AI constants

要向传感器注册的一种或多种检测类型:

  • aivision::ALL_AIOBJS — 所有 AI 分类
  • aivision::ALL_TAGS — 所有 AprilTag ID
  • aivision::colordesc — 自定义颜色签名
  • aivision::codedesc — 自定义颜色代码

人工智能模型和分类#

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

课堂要素

身份证号码

人工智能分类

0

blueBall

1

greenBall

2

redBall

3

blueRing

4

greenRing

5

redRing

6

blueCube

7

greenCube

8

redCube

VEX V5 机器人大赛 高风险赛

身份证号码

人工智能分类

0

mobileGoal

1

redRing

2

blueRing

VEX V5 机器人竞赛延期

身份证号码

人工智能分类

0

blueBlock

1

redBlock

VEX V5 机器人竞赛规则变更

身份证号码

人工智能分类

0

redBluePin

1

blueYellowPin

2

redYellowPin

3

yellowPin

4

cup

示例#

// Create Color Signatures
aivision::colordesc AIVision1__greenBox(
    1,      // index
    85,     // red
    149,    // green
    46,     // blue
    23,     // hangle
    0.23 ); // hdsat

aivision::colordesc AIVision1__blueBox(
    2,      // index
    77,     // red
    135,    // green
    125,    // blue
    27,     // hangle
    0.29 ); // hdsat

// Create a Color Code from two color signatures
aivision::codedesc AIVision1__greenBlue(
    1,                    // code index
    AIVision1__greenBox,  // first color signature
    AIVision1__blueBox ); // second color signature

// Create the AI Vision Sensor instance
aivision AIVision1(
    PORT11,                  // Smart Port
    AIVision1__greenBlue,    // color code
    aivision::ALL_AIOBJS );  // enable AI Classifications

成员功能#

第 aivision 部分包含以下成员函数:

  • takeSnapshot — 捕获特定颜色签名、颜色代码、AI 分类组或 AprilTag ID 组的数据。

  • installed — 返回 AI 视觉传感器是否连接到 V5 大脑。

调用 takeSnapshot 后,要访问检测到的对象数据,请使用可用的 Properties。

在调用任何 aivision 成员函数之前,必须创建一个 aivision 实例,如下所示:

/* This constructor is required when using VS Code.
AI Vision Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */


// Create Color Signatures
aivision::colordesc AIVision1__greenBox(
    1,      // index
    85,     // red
    149,    // green
    46,     // blue
    23,     // hangle
    0.23 ); // hdsat

aivision::colordesc AIVision1__blueBox(
    2,      // index
    77,     // red
    135,    // green
    125,    // blue
    27,     // hangle
    0.29 ); // hdsat

// Create a Color Code from two color signatures
aivision::codedesc AIVision1__greenBlue(
    1,                    // code index
    AIVision1__greenBox,  // first color signature
    AIVision1__blueBox ); // second color signature

// Create the AI Vision Sensor instance
aivision AIVision1(
    PORT1,                   // Smart Port
    AIVision1__greenBlue,    // color code
    aivision::ALL_AIOBJS );  // enable AI Classifications

takeSnapshot#

从 AI 视觉传感器捕获图像,使用选定的 AI 模型或配置的颜色签名对其进行处理,并更新 objects 数组。

Each call refreshes the objects array with the most recent detection results. Objects are ordered from largest to smallest (by width), beginning at index 0. If no objects are detected objectCount will be 0 and objects[i].exists will be false.

1 — 使用对象 ID 和对象类型拍摄快照。

int32_t takeSnapshot(
  uint32_t id,
  objectType type,
  uint32_t count );

2 — 使用 颜色签名 拍摄快照。

int32_t takeSnapshot(
  const colordesc &desc,
  int32_t count = 8 );

3 — 使用 颜色代码 拍摄快照。

int32_t takeSnapshot(
  const codedesc &desc,
  int32_t count = 8 );

4 — 使用 AprilTag ID 拍摄快照。

int32_t takeSnapshot(
  const tagdesc &desc,
  int32_t count = 8 );

5 — 使用 AI 分类 拍摄快照。

int32_t takeSnapshot(
  const aiobjdesc &desc,
  int32_t count = 8 );

6 — 使用对象描述符拍摄快照。

int32_t takeSnapshot(
  const objdesc &desc,
  int32_t count = 8 );

Parameters

范围

类型

描述

id

uint32_t

使用 (id, type, count) 函数时要检测的对象标识符。其含义取决于 type:

  • colorObject — 颜色描述符索引 (1–7)
  • codeObject — 颜色代码索引 (1–8)
  • modelObject — AI 分类 ID
  • tagObject — AprilTag ID (0–36)

注意: 在 VEXcode 中,可以直接使用 AI 分类名称(例如 blueBall)。在 VS Code 中,必须使用数字 ID。

type

objectType

指定与 id关联的对象类别:

  • objectType::tagObject — 检测 AprilTag ID
  • objectType::colorObject — 检测 颜色签名
  • objectType::codeObject — 检测 颜色代码
  • objectType::modelObject — 检测 AI 分类
  • objectType::allObject — 检测传感器可以识别的所有内容

desc

colordesc &, codedesc &, tagdesc &, aiobjdesc &, or objdesc &

用于检测特定对象的描述符。直接传递给 takeSnapshot,例如:

  • AIVision1.takeSnapshot(AIVision1__greenBox);
  • AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
预定义描述符常量:
  • aivision::ALL_TAGS — 检测所有 AprilTag ID
  • aivision::ALL_COLORS — 检测所有已配置的颜色签名
  • aivision::ALL_CODES — 检测所有已配置的颜色代码
  • aivision::ALL_AIOBJS — 检测所有AI 分类
  • aivision::ALL_OBJECTS — 检测传感器可以识别的所有内容

count

uint32_t / int32_t

从快照中存储的最大对象数。默认为 8。

返回值

返回 int32_t ,表示检测到的与指定签名或检测类型匹配的对象数量。

条笔记

  • AI视觉传感器必须先拍摄快照才能访问物体数据。

  • 每次调用时都会刷新包含 objects 对象的数组。

  • 当指定 count ,仅存储检测到的最大对象(最多指定数量)。

AI 分类取决于在 VEXcode 的 AI 视觉实用程序中选择的模型。

示例

// 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);
}

installed#

返回 AI 视觉传感器是否已连接到 V5 大脑。

可用功能

bool installed();

参数

此函数不接受任何参数。

返回值

返回一个布尔值,指示 AI 视觉传感器是否已连接:

  • true — AI 视觉传感器已连接。

  • false — AI视觉传感器未连接。

// Display a message if the AI Vision Sensor is connected
if (AIVision1.installed()){
  Brain.Screen.print("Installed!");
}

特性#

调用 takeSnapshot 会更新 AI 视觉传感器的检测结果。每次快照都会刷新包含 objects 元素的数组,该数组包含根据请求的 AI 分类、颜色签名、颜色代码或 AprilTag ID 检测到的对象。

AI Vision 数据通过存储在 AIVisionSensor.objects[index]中的对象的属性来访问,其中 index 从 0开始。

物体按面积从大到小排序。

AI视觉传感器图像分辨率为 320 × 240像素物体位置和大小值以像素单位表示,相对于传感器的当前视图。

以下房源可供选择:

  • objectCount — 返回从最近快照中检测到的对象数量。

  • largestObject — 从最近的快照中选择检测到的最大对象。

  • objects — 包含由 takeSnapshot 更新的检测到的对象数组。

  • .exists — 对象条目是否包含有效数据。

  • .width — 检测到的对象宽度(以像素为单位)。

  • .height — 检测到的物体的高度(以像素为单位)。

  • .centerX — 对象中心的 X 坐标(以像素为单位)。

  • .centerY — 对象中心的 Y 坐标(以像素为单位)。

  • .originX — 对象左上角的 X 坐标(以像素为单位)。

  • .originY — 对象左上角的 Y 坐标(以像素为单位)。

  • .angle — 颜色代码或 AprilTag ID 的方向(以度为单位)。

  • .id — 分类 ID 或 AprilTag ID。

  • .score — AI 分类的置信度得分。

objectCount#

objectCount 返回 objects 数组中的项目数,以整数形式返回。

Syntax
AIVisionSensor.objectCount

Components

成分

描述

AIVisionSensor

您的AI视觉传感器实例的名称。

示例

// 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#

largestObject 从 objects 数组中检索检测到的最大对象。

此方法可用于从 objects 而无需指定索引。

Syntax
AIVisionSensor.largestObject

Components

成分

描述

AIVisionSensor

您的AI视觉传感器实例的名称。

示例

// Display the closest AprilTag 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);
}

objects#

objects 返回检测到的对象属性数组。使用该数组可以访问各个对象的特定属性值。

使用 takeSnapshot objects 存储在 takeSnapshot 数组中的每个对象都包含十个属性。

除 id 和 score 之外的所有属性值均描述了在执行 takeSnapshot 操作时,AI 视觉传感器视野中检测到的物体的位置和大小。这些值以像素为单位,基于传感器的 320 x 240 像素分辨率。

Syntax
AIVisionSensor.objects[index].property

Components

成分

描述

AIVisionSensor

您的AI视觉传感器实例的名称。

index

数组中的对象索引。索引从 0开始。

property

可用的对象属性。

.存在#

.exists 返回指定的对象索引是否包含有效的检测到的对象。

访问

SensorName.objects[index].exists

返回值

返回一个布尔值,指示指定的对象索引是否包含有效的检测到的对象:

  • true — A valid object exists at the specified index.
  • false — No object exists at the specified index.

示例

// 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 返回检测到的对象的宽度。

访问

SensorName.objects[index].width

返回值

返回一个 int16_t 表示检测到的对象以像素为单位的宽度。该值的范围从 1 到 320。

示例

// 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 返回检测到的物体的高度。

访问

SensorName.objects[index].height

返回值

返回一个 int16_t 表示检测到的对象的高度(以像素为单位)。该值的范围从 1 到 240。

示例

// 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 返回检测到的对象中心的 x 坐标。

访问

SensorName.objects[index].centerX

返回值

返回一个 int16_t 表示对象中心的 x 坐标(以像素为单位)。该值的范围从 0 到 320。

示例

// 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 返回检测到的对象中心的 y 坐标。

访问

SensorName.objects[index].centerY

返回值

返回一个 int16_t 表示对象中心的 y 坐标(以像素为单位)。该值的范围从 0 到 240。

示例

// 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 返回检测到的颜色代码或 AprilTag ID 的方向。

访问

SensorName.objects[index].angle

返回值

返回一个 float 表示检测到的颜色代码或 AprilTag ID 的旋转角度(以度为单位)。该值的范围从 0 到 359.9。

示例

// 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 返回检测到的对象边界框左上角的 x 坐标。

访问

SensorName.objects[index].originX

返回值

返回一个 int16_t 表示对象边界框原点的 x 坐标(以像素为单位)。该值的范围从 0 到 320。

示例

// 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 返回检测到的对象边界框左上角的 y 坐标。

访问

SensorName.objects[index].originY

返回值

返回一个 int16_t 值,表示对象边界框原点的 y 坐标(以像素为单位)。该值的范围从 0 到 240。

示例

// 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 返回检测到的 AprilTag ID 或 AI 分类 ID。

访问

SensorName.objects[index].id

返回值

返回一个 int32_t ,表示检测到的对象的 ID:

示例

// 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 返回检测到的 AI 分类的置信度分数。

访问

SensorName.objects[index].score

返回值

返回 int16_t ,表示检测到的 AI 分类的置信度得分,介于 1 到 100 之间。

得分越低,表示传感器对检测到的物体信心越低;得分越高,表示传感器对检测到的物体信心越高。

示例

// 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);
}