人工智能视觉传感器#

介绍#

AI 视觉传感器可以检测并追踪物体、颜色和 AprilTag。这使得机器人能够分析周围环境、跟踪物体,并根据检测到的视觉数据做出反应。

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

以下是所有方法的列表:

Getters – 从 AI 视觉传感器获取数据。

  • take_snapshot – 捕获特定签名的数据。

  • 已安装 – AI 视觉传感器是否连接到 IQ(第二代)大脑。

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

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

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

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

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

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

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

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

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

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

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

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

拍摄快照#

take_snapshot filters the data from the AI Vision Sensor’s frame to return a tuple. The AI Vision Sensor can detect configured Color Signatures and Color Codes, AI Classifications, and AprilTags.

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

该元组存储的对象按宽度从大到小排序,从索引 0 开始。每个对象的 属性 可以通过其索引访问。如果未检测到匹配的对象,则返回一个空元组。

Usage:
ai_vision_1.take_snapshot(SIGNATURE)

参数

描述

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: vision_1__RED_BOX.
  • AiVision.ALL_AIOBJS – Cubes, rings, and balls.
  • AiVision.ALL_TAGS – All AprilTags.

count

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

# Move forward if an object is detected
while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)
    if objects:
        drivetrain.drive_for(FORWARD, 50, MM)
    wait(50, MSEC)

Color Signatures#

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

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: ai_vision_1__RED_BOX.

# Display if any objects match the RED_BOX signature
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_row(1)
    # Change to any configured Color Signature
    red_box = ai_vision_1.take_snapshot(ai_vision_1__RED_BOX)
    if red_box:
        brain.screen.print("Color detected!")
        wait(100, MSEC)

Color Codes#

颜色代码是由按特定顺序排列的颜色特征组成的结构化模式。这些代码使AI视觉传感器能够识别预定义的颜色模式。颜色代码可用于识别复杂物体或为自主导航创建独特的标记。

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

# Display if any objects match the BOX_CODE code
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_row(1)
    # Change to any configured Color Code
    box_code = ai_vision_1.take_snapshot(ai_vision_1__BOX_CODE)
    if box_code:
        brain.screen.print("Code detected!")
        wait(100, MSEC)

已安装#

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

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

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

Usage:
ai_vision_1.installed()

参数

描述

该方法没有参数。

# Display a message if the AI Vision Sensor is connected
if ai_vision_1.installed():
    brain.screen.print("Installed!")

特性#

使用 take_snapshot 后,元组中存储的每个对象包含十个属性。

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

.exists#

.exists returns a Boolean indicating if the index exists in the tuple or not.

  • True – The index exists.

  • False – The index does not exist.

# Check if at least at least two objects are detected
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)

    if objects:
        if objects[1].exists:
            brain.screen.print("At least 2")
            
        else:
            brain.screen.print("Less than 2")
    wait(50, MSEC)

.width#

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

# Approach an object until it's at least 100 pixels wide
while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)

    if objects:
        if objects[0].width < 100:
            drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(50, MSEC)

.height#

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

# Approach an object until it's at least 90 pixels tall
while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)

    if objects:
        if objects[0].height < 90:
            drivetrain.drive(FORWARD)
    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.

# Turn until an object is directly in front of the sensor
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)

while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)

    if objects:
        if 140 < 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.

# Approach an object until the object's center is
# high enough in the field of view
while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)

    if objects:
        if objects[0].centerY < 150:
            drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(50, MSEC)

.angle#

.angle returns the orientation of the detected Color Code or AprilTag in degrees, which is an integer between 0 and 360.

# Turn left or right depending on how a
# configured Color Code is rotated
while True: 
    box_code = ai_vision_1.take_snapshot(ai_vision_1__BOX_CODE)

    if box_code: 
        if 50 < box_code[0].angle < 100:
            drivetrain.turn(RIGHT)
            
        elif 270 < box_code[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.

# Display if an object is to the left or the right
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1,1)
    
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)
    
    if objects:
        if 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.

# Display if an object is close or far
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1,1)
    
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)

    if objects:
        if objects[0].originY < 110:
            brain.screen.print("Close")
        else: 
            brain.screen.print("Far")

.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,如下所示。

# Move forward when AprilTag 1 is detected
while True:
    apriltags = ai_vision_1.take_snapshot(AiVision.ALL_TAGS)

    if apriltags:
        if apriltags[0].id == 1:
            robot.drive(FORWARD)
    else:
        drivetrain.stop()

    wait(50, MSEC)

.score#

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

# Display if a score is confident
while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    if objects:
        if 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.

AI Vision Sensor#

AiVision creates an AI Vision Sensor.

用法

Vision(port, sigs)

参数

描述

port

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

sigs

Optional. The name of one or more signatures:

例子

ai_vision_1 = AiVision(Ports.PORT1, AiVision.ALL_AIOBJS)

# Move forward if an object is detected
while True:
    objects = ai_vision_1.take_snapshot(AiVision.ALL_AIOBJS)
    if objects:
        drivetrain.drive_for(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.

用法:

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。配置颜色签名后,从配置窗口复制参数值。

例子

# Create a new Signature RED_BOX with the Colordesc class
RED_BOX = Colordesc(1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1)
# Create a new AI Vision Sensor "ai_vision_1" with the AiVision
# class, with the RED_BOX Signature.
ai_vision_1 = AiVision(Ports.PORT1, 100, RED_BOX)

# Move forward if a red object is detected
while True:
    red_object = ai_vision_1.take_snapshot(RED_BOX)
    if red_object:
        drivetrain.drive_for(FORWARD, 10, MM)
    wait(5, 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 a Vision Sensor at once.

用法:

Codedesc(sig1, sig2, sig3, sig4, sig5)

范围

描述

sig1

先前创建的 颜色签名

sig2

先前创建的 颜色签名

sig3

可选。 之前创建的 颜色签名

sig4

可选。 之前创建的 颜色签名

sig5

可选。 之前创建的 颜色签名

例子

# Create two new Signatures for a red and blue box
RED_BOX = Colordesc(1, 10121, 10757, 10439,-1657, -1223, -1440, 2.5, 1)
BLUE_BOX = Colordesc(2, -4443, -3373, -3908,6253, 7741, 6997, 2.5, 1)
# Create a Color Code for a red box to the left of a blue box
RED_BLUE = Codedesc(RED_BOX, BLUE_BOX)
# Create a new AI Vision Sensor "ai_vision_1" with the AiVision
# class, with the RED_BOX and BLUE_BOX Signatures.
ai_vision_1 = AiVision(Ports.PORT1, 100, RED_BOX, BLUE_BOX)

# Display a message if Color Code is detected
while True:
    brain.screen.set_cursor(1, 1)
    brain.screen.clear_row(1)
    # Change to any configured Color Code
    box_code = ai_vision_1.take_snapshot(RED_BLUE)
    if box_code:
        brain.screen.print("Code detected!")
        wait(100, MSEC)