艾维斯#

AI 视觉传感器必须连接到您的 V5 大脑,并在 VEXcode V5 中进行配置后才能使用。点击此处了解有关 VEX V5 AI 视觉传感器入门 的信息。

有关使用 AI 视觉传感器的更多信息,请参阅这些文章。

有关在 VEXcode V5 中使用带有块的 AI 视觉传感器的更多详细信息,请阅读使用 VEXcode V5 C++ 中的 AI 视觉传感器进行编码

初始化 avision 类#

使用以下构造函数之一创建 AI 视觉传感器:

aivision(port) 构造函数在指定端口创建一个 aiVision 对象。

范围

描述

端口

与 AI 视觉传感器连接的有效 智能端口

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

aivision(port, desc, ...) 构造函数使用两个或多个参数:

范围

描述

端口

与 AI 视觉传感器连接的有效 智能端口

描述

一个或多个 colordesccodedesctagdescaiobjdesc 对象的名称。

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

当引用 AiVision 类方法时,此 aiVision 对象和 aiVision__Red colordesc 对象将在本 API 文档的所有后续示例中使用。

类方法#

拍摄快照()#

takeSnapshot 方法拍摄 AI 视觉传感器当前看到的内容,并从该快照中提取数据,然后可以在项目中使用。

拍摄快照会存储您在 AI 视觉传感器实例中指定的所有检测到的对象。例如,如果您想检测“蓝色”颜色特征,而 AI 视觉传感器检测到了 3 个不同的蓝色对象,则这 3 个对象的数据都会被存入数组。

takeSnapshot(desc, count) 方法获取 AI 视觉传感器可见的当前快照并检测指定对象描述的对象。

参数

描述

描述

colordesccodedesctagdescaiobjdesc 对象。

数数

**可选。**要获取的对象的最大数量。默认值为 8。

**返回:**一个整数,表示与作为参数传递的描述匹配的对象的数量。

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

对象#

objects 方法允许您访问上次拍摄的快照中对象的存储属性。

可用属性:

  • id

  • centerXcenterY

  • originXoriginY

  • widthheight

  • 角度

  • 存在

  • 分数

要访问对象的属性,请使用 AI 视觉传感器的名称,后跟对象方法,然后是对象的索引。例如:aiVision.objects[0].width

ID#

id 属性仅适用于 AprilTagsAI Classifications

对于 AprilTagid 属性代表检测到的 AprilTag(s) ID 号。

对于AI 分类id 属性表示检测到的AI 分类的具体类型。如需详细了解AI 分类的 ID,请前往此文

要调用 id 属性,必须使用 aiVision.takeSnapshot 命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

注意:AprilTags 按其唯一 ID 升序排序,而不是按大小排序。例如,如果检测到 AprilTags 1、15 和 3:

  • AprilTag 1 位于索引 0。

  • AprilTag 3 位于索引 1。

  • AprilTag 15 位于索引 2。

要调用此属性,请使用“objects”方法,后接要提取属性的已检测到对象的索引。例如:“aiVision.objects[0].id”。

centerX 和 centerY#

centerXcenterY 属性报告检测到的物体的中心坐标(以像素为单位)。

要调用“centerX”或“centerY”属性,必须使用“aiVision.takeSnapshot”命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

要调用属性,请使用 objects 方法,后接要提取属性的已检测到对象的索引。例如:aiVision.objects[0].centerX

在此示例中,由于 AI 视觉传感器视图的中心是 (160, 120),因此机器人将向右转弯,直到检测到的物体的中心 X 坐标大于 150 像素,但小于 170 像素。

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 和 originY#

originXoriginY 属性报告对象边界框左上角的坐标(以像素为单位)。

要调用 originXoriginY 属性,必须使用 aiVision.takeSnapshot 命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

要调用属性,请使用 objects 方法,后接要提取属性的已检测到对象的索引。例如:aiVision.objects[0].originX

在这个例子中,将在 Brain 的屏幕上绘制一个矩形,其尺寸与指定对象的边界框完全相同。

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

  Brain.Screen.clearScreen();

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

    Brain.Screen.drawRectangle(aiVision.objects[0].originX, aiVision.objects[0].originY, aiVision.objects[0].width, aiVision.objects[0].height);
  }
  wait(5, msec);
}

宽度和高度#

“width”和“height”属性以像素为单位报告对象的宽度或高度。

要调用“width”或“height”属性,必须使用“aiVision.takeSnapshot”命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

要调用属性,请使用 objects 方法,后接要提取属性的已检测到对象的索引。例如:aiVision.objects[0].width

本例中,我们利用物体的宽度进行导航。机器人会逐渐靠近物体,直到宽度达到特定尺寸后才停止。

while (true) {
  // Get a snapshot of all Blue 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].width < 250.0) {
      Drivetrain.drive(forward);
    } else {
      Drivetrain.stop();
    }
  }
  wait(5, msec);
}

角度#

angle 属性仅适用于 Color CodesAprilTags

此属性报告检测到的颜色代码AprilTag的角度。

要调用“angle”属性,必须使用“aiVision.takeSnapshot”命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

要调用此属性,请使用 objects 方法,后接要提取属性的已检测到对象的索引。例如:aiVision.objects[0].angle

在这个例子中,AprilTag 的角度被打印到 Brain 的屏幕上。

while (true) {
  // Get a snapshot of all AprilTags.
  aiVision.takeSnapshot(aivision::ALL_TAGS);

  Brain.Screen.clearScreen();

  // Check to make sure an object was detected in the
  // snapshot before pulling data.
  if (aiVision.objects[0].exists == true) {
    Brain.Screen.print(aiVision.objects[0].angle);
  }
  wait(5, msec);
}

存在#

此属性返回一个布尔值,表示指定对象是否存在。

要调用 exists 属性,必须使用 aiVision.takeSnapshot 命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

要调用此属性,请使用“objects”方法,后接要提取属性的已检测到对象的索引。例如:“aiVision.objects[0].exists”。

在这个例子中,机器人在将其角度打印到 Brain 的屏幕上之前会检查是否检测到 AprilTag。

while (true) {
  // Get a snapshot of all AprilTags.
  aiVision.takeSnapshot(aivision::ALL_TAGS);

  Brain.Screen.clearScreen();

  // Check to make sure an object was detected in the
  // snapshot before pulling data.
  if (aiVision.objects[0].exists == true) {
    Brain.Screen.print(aiVision.objects[0].angle);
  }
  wait(5, msec);
}

分数#

score 属性仅适用于 AI 分类

此属性返回指定 AI 分类的置信度分数。该分数的范围为 0% 到 100%,表示 AI 视觉传感器对其检测准确度的确定性。

要调用 score 属性,必须使用 aiVision.takeSnapshot 命令拍摄快照。该数组按对象面积(以像素为单位)从大到小排序,索引从 0 开始。

要调用此属性,请使用“objects”方法,后接要提取属性的已检测到对象的索引。例如:“aiVision.objects[0].score”。

对象数量#

objectCount 方法返回在最近的快照中找到的对象的数量。

**返回:**一个整数,表示在最近的快照中找到的对象的数量。

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(enable) 方法启用或禁用 apriltag 检测。

参数

描述

使能够

true 表示启用 AprilTag 检测。false 表示禁用它。

**返回:**无。

颜色检测()#

colorDetection(enable, merge) 方法启用或禁用颜色和代码对象检测。

参数

描述

使能够

true 表示启用颜色和颜色代码检测。false 表示禁用它。

合并

一个布尔值,用于启用或禁用相邻颜色检测的合并。默认值为 false。

**返回:**无。

模型检测()#

modelDetection(enable) 方法启用或禁用 AI 模型对象,也称为 AI 分类检测。

参数

描述

使能够

true 表示启用 AI 模型对象检测。false 表示禁用它。

**返回:**无。

启动Awb()#

startAwb() 方法运行自动白平衡。

**返回:**无。

放()#

set(desc) 方法设置新的颜色签名或颜色代码。

参数

描述

描述

colordesccodedesc 对象。

**返回:**无。

时间戳()#

timestamp() 方法请求 AI 视觉传感器最后接收到的状态包的时间戳。

**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。

安装()#

installed() 方法检查设备连接。

返回: 如果 AI 视觉传感器已连接,则返回 true。如果没有连接,则返回 false