人工智能视觉传感器#

介绍#

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

对于以下示例,配置的 IQ AI 视觉传感器将被命名为 ai_vision_1,配置的颜色签名对象(例如 RED_BOX)将在本 API 文档的所有后续示例中引用 AiVision 类方法时使用。

以下是所有方法的列表:

数据获取器 — 从 IQ AI 视觉传感器获取数据。

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

  • installed — IQ AI 视觉传感器是否连接到 IQ(第二代)大脑。

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

  • .exists — 对象是否存在于当前检测中。

  • .width — 检测到的对象的宽度。

  • .height — 检测到的物体的高度。

  • .centerX — 物体中心的 X 坐标。

  • .centerY — 物体中心的 Y 坐标。

  • .angle — 颜色代码或 AprilTag ID 的方向。

  • .originX — 对象左上角的 X 坐标。

  • .originY — 对象左上角的 Y 坐标。

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

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

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

获取器#

take_snapshot#

take_snapshot 将 IQ AI 视觉传感器帧中的数据过滤为单个签名——传感器可以识别的事物的保存描述,例如预训练对象、AprilTag ID 或配置的颜色签名或颜色代码——并返回一个 元组。

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

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

Usage:
ai_vision_1.take_snapshot(SIGNATURE)

参数

描述

SIGNATURE

Filters the dataset to only include data of the given signature. Available signatures are:

  • Color Signatures or Color Codes - The name of the IQ AI Vision Sensor, two underscores, and then the Color Signature’s or Color Code’s name. For example: ai_vision_1__RED_BOX.
  • AiVision.ALL_AIOBJS - All detected AI Classifications depending on the model configured in the AI Vision Utility.
  • AiVision.ALL_TAGS - All AprilTag IDs.

count

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

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

Note: The returned tuple can be sorted by any property, not just width, using Python’s sorted function. For example, sorting by AprilTag ID:

# Print each detected AprilTag ID in ascending order by ID
while True:
    apriltags = sorted(ai_vision_1.take_snapshot(AiVision.ALL_TAGS), key=id)
    for tag in apriltags:
        print(tag.id)
    wait(1, SECONDS)

或者改用高度排序:

# Print each detected AprilTag ID in ascending order by height
while True:
    apriltags_by_height = sorted(ai_vision_1.take_snapshot(AiVision.ALL_TAGS), key=lambda tag: tag.height)
    for tag in apriltags_by_height:
        print(tag.height)
    wait(1, SECONDS)

人工智能分类#

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

教室物品

身份证号码

人工智能分类

0

ClassroomElements.BLUE_BALL

1

ClassroomElements.GREEN_BALL

2

ClassroomElements.RED_BALL

3

ClassroomElements.BLUE_RING

4

ClassroomElements.GREEN_RING

5

ClassroomElements.RED_RING

6

ClassroomElements.BLUE_CUBE

7

ClassroomElements.GREEN_CUBE

8

ClassroomElements.RED_CUBE

VEX IQ 机器人竞赛混合搭配

身份证号码

人工智能分类

0

GameElementsMixAndMatch.BEAM

1

GameElementsMixAndMatch.BLUE_PIN

2

GameElementsMixAndMatch.RED_PIN

3

GameElementsMixAndMatch.ORANGE_PIN

VEX IQ 机器人竞赛升级版

身份证号码

人工智能分类

0

GameElementsLevelUp.BLUE_BEANBAG

1

GameElementsLevelUp.RED_BEANBAG

2

GameElementsLevelUp.YELLOW_BEANBAG

色彩签名#

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

要在项目中使用已配置的颜色签名,其名称必须为传感器名称,后跟两个下划线,再后跟颜色签名的名称。例如: 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[0].exists:
        brain.screen.print("Color detected!")
        wait(100, MSEC)

颜色代码#

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

要在项目中使用已配置的颜色代码,其名称必须为传感器名称,后跟两个下划线,再后跟颜色代码的名称。例如: 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[0].exists:
        brain.screen.print("Code detected!")
        wait(100, MSEC)

installed#

installed 返回一个布尔值,指示 IQ AI 视觉传感器当前是否连接到 IQ(第二代)大脑。

  • True — IQ AI 视觉传感器与 IQ(第二代)大脑相连。

  • False ​​— IQ AI 视觉传感器未连接到 IQ(第二代)大脑。

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 后,存储在 tuple 中的每个对象都包含十个属性。

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

.exists#

.exists 返回一个布尔值,指示索引是否存在于元组中。

  • True — 索引存在。

  • False ​​— 索引不存在。

# Check if 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[0].exists:
        if objects[1].exists:
            brain.screen.print("At least 2")

        else:
            brain.screen.print("Less than 2")
    wait(50, MSEC)

.width#

.width 返回检测到的对象在像素中的宽度,该值是 1 到 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[0].exists:
        if objects[0].width < 100:
            drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(50, MSEC)

.height#

.height 返回检测到的对象的高度(以像素为单位),该值是 1 到 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[0].exists:
        if objects[0].height < 90:
            drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(50, MSEC)

.centerX#

.centerX 返回检测到的对象中心的 x 坐标(以像素为单位),该值是 0 到 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[0].exists:
        if 140 < objects[0].centerX < 180:
            drivetrain.stop()
    wait(10,MSEC)

.centerY#

.centerY 返回检测到的对象中心的 y 坐标(以像素为单位),该值是 0 到 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[0].exists:
        if objects[0].centerY < 150:
            drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()
    wait(50, MSEC)

.angle#

.angle 返回检测到的颜色代码或 AprilTag ID 的方向(以度为单位),该值是 0 到 359 之间的整数。

# 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[0].exists:
        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 返回检测到的对象边界框左上角的 x 坐标(以像素为单位),该值是 0 到 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[0].exists:
        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 返回检测到的对象边界框左上角的 y 坐标(以像素为单位),该值是 0 到 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[0].exists:
        if objects[0].originY < 110:
            brain.screen.print("Close")
        else:
            brain.screen.print("Far")

.id#

.id 返回检测到的AI 分类或 AprilTag ID 的整数值。AI 分类取决于 AI 视觉实用程序中配置的模型。

对于 AprilTag ID, .id 属性表示检测到的 AprilTag ID 编号,范围为 0 到 36。对于 AI 分类, .id 属性对应于 AI 分类 部分中预定义的 ID。

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

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

    wait(50, MSEC)

.score#

.score 返回检测到的 AI 分类的置信度分数,该分数为 1 到 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[0].exists:
        if objects[0].score > 95:
            brain.screen.print("Confident")
    else:
        brain.screen.print("Not confident")

    wait(50, MSEC)

构造函数#

构造函数用于手动创建 AiVision Colordesc和 Codedesc 对象,这些对象对于在 VEXcode 之外配置 IQ AI 视觉传感器是必要的。

AI Vision Sensor#

AiVision 创建了一个 IQ AI 视觉传感器。

用法

Vision(port, sigs)

参数

描述

port

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

sigs

可选。一个或多个签名的名称:

例子

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[0].exists:
        drivetrain.drive_for(FORWARD, 50, MM)
    wait(50, MSEC)

Color Signature#

Colordesc 创建颜色签名。IQ AI 视觉传感器最多可同时存储七个不同的颜色签名。

用法:

Colordesc(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)

范围

描述

index

Colordesc 对象的索引,范围为 1 - 7。注意:创建两个具有相同索引号的 Colordesc 对象会导致第二个创建的对象覆盖第一个对象。

uMin

Vision Utility 中的值介于 uMin 之间。

uMax

Vision Utility 中的值介于 uMax 之间。

uMean

视觉实用工具中的 uMean 值介于 uMean 之间。

vMin

Vision Utility 中的值从 vMin。

vMax

Vision Utility 中的值介于 vMax 之间。

vMean

视觉实用程序中的值从 vMean。

rgb

视觉实用程序中的值从 rgb 开始。

type

Vision Utility 中的 type 的值。

要获取创建颜色签名的值,请转到 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[0].exists:
        drivetrain.drive_for(FORWARD, 10, MM)
    wait(5, MSEC)

Color Code#

Codedesc 创建一个颜色代码。它需要至少两个已定义的颜色签名才能使用。视觉传感器最多可以同时存储八个不同的颜色代码。

用法:

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, 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[0].exists:
        brain.screen.print("Code detected!")
        wait(100, MSEC)