人工智能视觉传感器#

介绍#

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

For the examples below, the configured AI Vision Sensor will be named AIVision1, and the configured Color Signature objects, such as redBox, will be used in all subsequent examples throughout this API documentation when referring to aivision class methods.

以下是所有方法的列表:

吸气剂

  • takeSnapshot – 捕获特定颜色特征或颜色代码的数据。

  • objectCount – 以整数返回检测到的物体的数量。

  • largestObject – 立即从快照中选择最大的对象。

属性 – 从 takeSnapshot 返回的对象数据。

  • .exists – 以布尔值表示该对象是否存在于当前检测中。

  • .width – 检测到的物体的宽度(以像素为单位)。

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

  • .centerX – 对象中心的 X 位置(以像素为单位)。

  • .centerY – 对象中心的 Y 位置(以像素为单位)。

  • .angle – 颜色代码的方向(以度为单位)。

  • .originX – 对象左上角的 X 位置(以像素为单位)。

  • .originY – 对象左上角的 Y 位置(以像素为单位)。

  • .id – 对象的分类或标签 ID。

  • .score – AI 分类的置信度得分 (1-100)。

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

拍摄快照#

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.

必须先在 Vision Utility 中配置 颜色签名颜色代码,然后才能使用此方法。

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

What signature to get data of.

  • 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 balls.
  • aivision::ALL_TAGS – All AprilTags.

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

Color Signatures#

颜色特征是 AI 视觉传感器能够识别的独特颜色。这些特征使 AI 视觉传感器能够根据物体的颜色进行检测和跟踪。配置颜色特征后,传感器可以识别其视野范围内具有该特定颜色的物体。颜色特征与 takeSnapshot 结合使用,可以实时处理和检测彩色物体。

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

最大对象#

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

对象数量#

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

已安装#

installed returns an integer indicating whether the AI Vision Sensor is currently connected to the IQ (2nd gen) Brain.

  • 1 – The AI Vision Sensor is connected to the IQ (2nd gen) Brain.

  • 0 – The AI Vision Sensor is not connected to the IQ (2nd gen) 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 Vision Sensor’s view at the time that takeSnapshot was used. The AI Vision Sensor has a resolution of 320 by 240 pixels.

.exists#

.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 at least 150 pixels tall
  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 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 as an integer.

人工智能分类

ID

蓝球

0

绿球

1

红球

2

蓝环

3

绿环

4

红环

5

蓝色立方体

6

绿色立方体

7

红色立方体

8

对于 AprilTag,.id 属性表示检测到的 AprilTag 的 ID 号,范围是 0 到 36。对于 AI 分类,id 对应于预定义的 id,如下所示。

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Move forward when AprilTag 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) {
      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, colordesc, and codedesc objects, which are necessary for configuring the AI Vision Sensor outside of VEXcode. If fewer arguments are provided, default arguments or function overloading should be used in the constructor definition.

AI Vision Sensor#

aivision creates an AI Vision Sensor.

Default Usage:
aivision( int32_t index, uint8_t bright, Args &… sigs )

参数

描述

port

AI 视觉传感器连接到哪个智能端口,从 1 到 12。

sigs

Optional. The name of one or more signatures:

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

Color Signature#

colordesc creates a Color Signature. Up to seven different Color Signatures can be stored on an AI Vision Sensor at once.

Default Usage:
colordesc(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)

范围

描述

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.

uMin

The value from uMin in the Vision Utility.

uMax

The value from uMax in the Vision Utility.

uMean

The value from uMean in the Vision Utility.

vMin

The value from vMin in the Vision Utility.

vMax

The value from vMax in the Vision Utility.

vMean

The value from vMean in the Vision Utility.

rgb

The value from rgb in the Vision Utility.

type

The value from type in the Vision Utility.

要获取创建颜色签名的值,请转到 Vision Utility。配置颜色签名后,从配置窗口复制参数值。

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

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display if a green object is detected
  while (true) {
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.clearLine(1);
    // Change to any configured Color Signature
    AIVision1.takeSnapshot(AIVision1__greenBox);
    if (AIVision1.objects[0].exists) {
      Brain.Screen.print("Color detected!");
    }
    wait(50, msec);
  }
}

Color Code#

codedesc creates a Color Code. It requires at least two already defined Color Signatures in order to be used. Up to eight different Color Codes can be stored on an AI Vision Sensor at once.

Default Usage:
codedesc(sig1, sig2)

重载:

  • codedesc(sig1, sig2, sig3)

  • codedesc(sig1, sig2, sig3, sig4)

  • codedesc(sig1, sig2, sig3, sig4, sig5)

参数

描述

sig1

A previously created signature object, or the int32_t index of a previously created signature object.

sig2

A previously created signature object, or the int32_t index of a previously created signature object.

sig3

A previously created signature object, or the int32_t index of a previously created signature object.

sig4

A previously created signature object, or the int32_t index of a previously created signature object.

sig5

A previously created signature object, or the int32_t index of a previously created signature object.

aivision::colordesc AIVision1__greenBox(1, 85, 149, 46, 23, 0.23);
aivision::colordesc AIVision1__blueBox(2, 77, 135, 125, 27, 0.29);
aivision::codedesc AIVision1__greenBlue(1, AIVision1__greenBox, AIVision1__blueBox);
vex::aivision AIVision1(PORT1, AIVision1__greenBox, AIVision1__blueBox, AIVision1__greenBlue);

int main() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // Display if greenBlue code is detected
  while (true) {
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.clearLine(1);
    // Change to any configured Color Signature
    AIVision1.takeSnapshot(AIVision1__greenBlue);
    if (AIVision1.objects[0].exists) {
      Brain.Screen.print("Code detected!");
    }
    wait(50, msec);
  }
}