人工智能视觉#

介绍#

VEX AIM 编程机器人的 AI 视觉传感器可检测并追踪物体、颜色和 AprilTag。这使得机器人能够分析周围环境、跟踪物体并根据检测到的视觉数据做出反应。以下是所有可用方法和属性的列表:

操作——显示或隐藏 AI Vision 摄像头馈送。

Getters——检测机器人是否拿着物体。

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

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

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

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

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

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

  • 方位角 – 相对于机器人前方的水平角度(以度为单位)。

  • 旋转 – 物体的方向(以度为单位)。

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

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

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

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

  • type – 返回对象的类型(AI、标签、颜色或代码)。

构造函数——定义颜色签名和代码。

行动#

show_aivision#

show_aivision 在机器人屏幕上显示 AI 视觉传感器的实时数据。实时数据将覆盖屏幕上的任何其他图像或文本。

**注意:**除非使用 hide_aivision 隐藏提要,否则屏幕不会显示任何其他图像或文本。

用法:
robot.screen.show_aivision()

参数

描述

该方法没有参数。

# Watch the AI Vision Sensor detect AI Classifications
# and AprilTags as you move the robot
robot.screen.show_aivision()

hide_aivision#

hide_aivision 从机器人的屏幕上删除 AI 视觉传感器的实时数据馈送。

用法:
robot.screen.hide_aivision()

参数

描述

该方法没有参数。

# View the AI Vision Sensor's feed for five seconds
robot.screen.show_aivision()
wait(5, SECONDS)
robot.screen.hide_aivision()

tag_detection#

tag_detection 启用或禁用 AprilTag 检测,其中状态是布尔值。

该传感器可以检测 Circle21h7 系列的 AprilTag ID 0 至 36。

用法:
robot.vision.tag_detection(state)

参数

描述

state

  • True
AprilTag 检测。4
  • FalseFalse
  • robot.screen.set_font(PROP30)
    
    # Cut off APrilTag detection after 5 seconds
    while True:
        if robot.timer.time(SECONDS) > 5:
            robot.vision.tag_detection(False)
    
        robot.screen.clear_screen()
        robot.screen.set_cursor(3, 1)
        apriltags = robot.vision.get_data(ALL_TAGS)
        
        if apriltags:
            robot.screen.print("AprilTag detected!")
        else:
            robot.screen.print("Nothing detected!")
    
        wait(0.1, SECONDS)
    
    

    吸气剂#

    get_data#

    get_data 过滤来自 AI 视觉传感器帧的数据并返回一个元组。AI 视觉传感器可以检测包含预训练对象、AprilTag 或已配置颜色和颜色代码的签名。

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

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

    用法:
    robot.vision.get_data(signature, count)

    参数

    描述

    signature

    获取什么签名的数据。可用的签名有:

    • ALL_VISION - 运动球、桶、VEX AIM 编码机器人、AprilTag、颜色和颜色代码
    • ALL_CARGO - 运动球和桶
    • ALL_TAGS - 所有 AprilTag
    • ALL_COLORS - 所有配置的颜色签名和颜色代码
    • SPORTS_BALL - 仅限运动球
    • BLUE_BARREL - 仅限蓝色桶
    • ORANGE_BARREL - 仅限橙色桶
    • AIM_ROBOT - 所有 VEX AIM 编码机器人
    • TAG0TAG37 - 单个 AprilTag,ID 来自0 到 37
    • NAME - 颜色签名或颜色代码
    其中 NAME 是在 AI Vision Utility 中配置的名称。44

    count

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

    **注意:**可以使用 AIM Printables 中打印的 AprilTags 获取 AprilTags 5 至 37。

    # Move forward if a sports ball is detected
    while True:
        ball = robot.vision.get_data(SPORTS_BALL)
        if ball:
            robot.move_for(10, 0)
        wait(50, MSEC)
    
    

    色彩签名#

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

    AI 视觉实用程序展示了一个连接的视觉传感器正在检测两个彩色物体。左侧显示实时摄像头画面,左侧为蓝色框,右侧为红色框,每个框都带有白色边框。黑色标签显示其各自的名称、坐标和尺寸。右侧包含颜色签名设置,红色和蓝色框的色调和饱和度范围均有滑块。底部是添加颜色、冻结视频、复制和保存图像的按钮,右下角有一个关闭按钮。

    # Display if any objects match the Red_Box signature
    while True:
        robot.screen.set_cursor(1, 1)
        robot.screen.clear_row(1)
        # Change to any configured Color Signature
        ai_objects = robot.vision.get_data(Red_Box)
        if ai_objects:
            robot.screen.print("Color signature detected!")
    
    

    颜色代码#

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

    AI Vision Utility 界面显示一个已连接的视觉传感器正在检测两个相邻的物体,左侧为蓝色框,右侧为红色框,它们被组合成一个标记为 BlueRed 的白色边界框。检测信息包括角度 (A:11°)、坐标 (X:143, Y:103)、宽度 (W:233) 和高度 (H:108)。右侧面板列出了三个颜色特征:Red_Box、Blue_Box 和 BlueRed,并可调整色调和饱和度范围。BlueRed 特征结合了 Blue_Box 和 Red_Box。视频源下方是标有“冻结视频”、“复制图像”、“保存图像”和“关闭”的按钮。

    # Display if any objects match the BlueRed code
    while True:
        robot.screen.set_cursor(1, 1)
        robot.screen.clear_row(1)
        # Change to any configured Color Code
        ai_objects = robot.vision.get_data(BlueRed)
        if ai_objects:
            robot.screen.print("Color code detected!")
    
    

    has_sports_ball#

    has_sports_ball 返回一个布尔值,指示机器人当前是否有运动球。

    • True – 机器人有一个运动球。

    • False – 机器人没有运动球。

    用法:
    robot.has_sports_ball()

    参数

    描述

    该方法没有参数。

    # Kick when the robot has a sports ball
    while True:
        if robot.has_sports_ball():
            robot.kicker.kick(MEDIUM)
    
        wait(50, MSEC)
    
    

    has_any_barrel#

    has_any_barrel 返回一个布尔值,指示机器人当前是否有任何类型的桶。

    • True – 机器人有一个桶。

    • False – 机器人没有枪管。

    用法:
    robot.has_any_barrel()

    参数

    描述

    该方法没有参数。

    # Push a barrel away when detected
    while True:
        if robot.has_any_barrel():
            robot.kicker.place()
    
        wait(50, MSEC)
    
    

    has_blue_barrel#

    has_blue_barrel 返回一个布尔值,指示机器人当前是否有蓝色桶。

    • True – 机器人有一个蓝色桶。

    • False – 机器人没有蓝色桶。

    用法:
    robot.has_blue_barrel()

    参数

    描述

    该方法没有参数。

    # Push a blue barrel away when detected
    while True:
        if robot.has_blue_barrel():
            robot.kicker.place()
    
        wait(50, MSEC)
    
    

    has_orange_barrel#

    has_orange_barrel 返回一个布尔值,指示机器人当前是否有一个橙色桶。

    • True – 机器人有一个橙色的桶。

    • False – 机器人没有橙色桶。

    用法:
    robot.has_orange_barrel()

    参数

    描述

    该方法没有参数。

    # Push an orange barrel away when detected
    while True:
        if robot.has_orange_barrel():
            robot.kicker.place()
    
        wait(50, MSEC)
    
    

    特性#

    使用 robot.vision.get_data 方法后,每个对象都包含十二个属性,这些属性存储在元组中。

    某些属性值基于使用 robot.vision.get_data robot.vision.get_data 检测到的物体在 AI 视觉传感器视野中的位置。AI 视觉传感器的分辨率为 320 x 240 像素。

    带有橙色条纹的蓝色桶位于框架的中心,周围环绕着黑色边界框。

    .exists#

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

    • True:索引存在。

    • False:索引不存在。

    # Check if at least two objects are detected
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
        ai_objects = robot.vision.get_data(ALL_CARGO)
    
        if ai_objects:
            if ai_objects[1].exists:
                robot.screen.print("More than 2")
            else:
                robot.screen.print("Less than 2")
        wait(0.1, SECONDS)
    
    

    .width#

    .width 返回检测到的对象的宽度(以像素为单位),它是 1 到 320 之间的整数。

    带有橙色条纹的蓝色桶位于框架中央,周围环绕着黑色边界框。两条垂直的红色虚线从框架顶部延伸至边界框的左右边缘。两条线之间的双向黑色箭头表示宽度测量值。

    # Move towards a Blue Barrel until its width is
    # larger than 100 pixels
    while True:
        barrel = robot.vision.get_data(BLUE_BARREL)
    
        if barrel:
            if barrel[0].width < 100:
                robot.move_at(0)
        else:
            robot.stop_all_movement()
    
        wait(50, MSEC)
    
    

    .height#

    .height 返回检测到的物体的高度(以像素为单位),它是 1 到 240 之间的整数。

    带有橙色条纹的蓝色桶位于框架中央,周围环绕着黑色边界框。两条水平红色虚线从框架左侧延伸至边界框的顶部和底部边缘。两条线之间的双向黑色箭头表示高度测量值。

    # Move towards a Blue Barrel until its height is
    # larger than 100 pixels
    while True:
        barrel = robot.vision.get_data(BLUE_BARREL)
    
        if barrel:
            if barrel[0].height < 100:
                robot.move_at(0)
        else:
            robot.stop_all_movement()
    
        wait(50, MSEC)
    
    

    .centerX#

    .centerX 返回检测到的物体中心的 x 坐标(以像素为单位),它是 0 到 320 之间的整数。

    带有橙色条纹的蓝色桶位于框架中央,周围环绕着黑色边界框。一条垂直的红色虚线从框架顶部延伸至边界框的中心,指示中心的 X 坐标。

    # Turn slowly until a Blue Barrel is centered in
    # front of the robot
    robot.set_turn_velocity(30)
    robot.turn(RIGHT)
    
    while True:
        barrel = robot.vision.get_data(BLUE_BARREL)
    
        if barrel:
            if 140 < barrel[0].centerX < 180: 
                robot.stop_all_movement()
    
        wait(10,MSEC)
    
    

    .centerY#

    .centerY 返回检测到的物体中心的 y 坐标(以像素为单位),它是 0 到 240 之间的整数。

    带有橙色条纹的蓝色桶位于框架中央,周围环绕着黑色边界框。一条水平红色虚线从框架左侧延伸至边界框中心,表示中心的 Y 坐标。

    # Move towards a Blue Barrel until its
    # center y-coordinate is more than 140 pixels
    while True:
        barrel = robot.vision.get_data(BLUE_BARREL)
    
        if barrel:
            if barrel[0].centerY < 140:
                robot.move_at(0)
        else:
            robot.stop_all_movement()
    
        wait(50, MSEC)
    
    

    .bearing#

    .bearing 返回一个浮点数,表示物体距离 AI 视觉传感器视野中心左侧或右侧的距离,以度为单位。0 表示位于中心,正值表示位于右侧,负值表示位于左侧。

    设置 设置
    # Turn to keep the Blue Barrel directly in front
    robot.set_turn_velocity(40)
    while True:
        vision_data = robot.vision.get_data(BLUE_BARREL)
    
        if vision_data:
    
            if vision_data[0].bearing > 5:
                robot.turn(RIGHT)
            elif vision_data[0].bearing < -5:
                robot.turn(LEFT)
            else:
                robot.stop_all_movement()
    
        else:
            robot.stop_all_movement()
    
        wait(0.1, SECONDS)
    
    

    .rotation#

    .rotation 返回物体在 AI 视觉传感器视平面上的朝向,以 0 到 359 度为单位。当颜色代码的颜色排列在水平线上时(例如,红色-蓝色),其角度为 0°。如果颜色旋转,角度会发生变化(例如,蓝色-红色排列返回 180°)。

    对于下面的示例,颜色代码已配置为左侧为红色,右侧为蓝色。

    在此图像中,将蓝色放在红色上方相当于将原始颜色代码向右旋转 270 度。

    相机界面显示一个垂直堆叠的物体,上半部分为蓝色,下半部分为红色,位于画面中央。物体下方的标签为“Red_Blue A:270° CX:185 CY:81 W:93 H:150”,指示物体的名称、旋转角度、中心 X 和 Y 坐标、宽度和高度。网格线和数字轴标记图像尺寸,X 轴范围为 0 到 320,Y 轴范围为 0 到 240。

    # Turn the robot with the rotation of the Color Code
    while True: 
        code = robot.vision.get_data(RedBlue)
    
        if code: 
            # Rotate the Color Code clockwise
            if 50 < code[0].rotation < 100 :
                robot.move_at(90)
                
            # Rotate the Color Code counterclockwise
            elif 270 < code[0].rotation < 330 :
                robot.move_at(270)
            
            else:
                robot.stop_all_movement()
        else:
            robot.stop_all_movement()
    
        wait(50, MSEC)
    
    

    .originX#

    .originX 返回检测到的对象边界框左上角的 x 坐标(以像素为单位),它是 0 到 320 之间的整数。

    带有橙色条纹的蓝色桶位于框架中央,周围环绕着黑色边界框。一条垂直的红色虚线从框架顶部延伸到边界框的左边缘,表示边界框原点的 X 坐标。

    # Display if an Orange Barrel is to the
    # left or the right
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1,1)
        
        orange_barrel = robot.vision.get_data(ORANGE_BARREL)
        
        if orange_barrel:
            if orange_barrel[0].originX < 160:
                robot.screen.print("To the left!")
            else: 
                robot.screen.print("To the right!")
    
        wait(50, MSEC)
    
    

    .originY#

    .originY 返回检测到的对象边界框左上角的 y 坐标(以像素为单位),它是 0 到 240 之间的整数。

    带有橙色条纹的蓝色桶位于框架中央,周围环绕着黑色边界框。一条水平红色虚线从框架左侧延伸至边界框顶部边缘,表示边界框原点的 Y 坐标。

    # Display if an Orange Barrel is close or far
    # from the robot
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
    
        orange_barrel = robot.vision.get_data(ORANGE_BARREL)
        
        if orange_barrel:
            if orange_barrel[0].originY < 80:
                robot.screen.print("Far")
            else: 
                robot.screen.print("Close")
    
        wait(50, MSEC)
    
    

    .id#

    .id 以整数形式返回检测到的 AI 分类或 AprilTag 的 ID。

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

    人工智能分类

    ID

    签名

    0

    SPORTS_BALL

    蓝桶

    1

    BLUE_BARREL

    橙色桶

    2

    ORANGE_BARREL

    AIM机器人

    3

    AIM_ROBOT

    # Move forward when AprilTag 1 is detected
    while True:
        apriltags = robot.vision.get_data(ALL_TAGS)
    
        if apriltags:
            if apriltags[0].id == 1:
                robot.move_at(0)
        else:
            robot.stop_all_movement()
    
        wait(50, MSEC)
    
    

    .score#

    .score 将检测到的 AI 分类的置信度分数返回为 1 到 100 之间的整数。

    # Look confident if an Orange Barrel is detected
    while True:
        barrel = robot.vision.get_data(ORANGE_BARREL)
    
        if barrel:
            if barrel[0].score > 95: 
                robot.screen.show_emoji(CONFIDENT)
            else:
                robot.screen.hide_emoji()
    
        wait(50, MSEC)
    
    

    .type#

    .type 返回检测到的物体类型。它将返回以下之一:

    对象类型

    包含的对象

    AiVision.AI_OBJECT

    - Sports balls
    - Blue barrels
    - Orange barrels

    AiVision.TAG_OBJECT

    - AprilTags

    AiVision.COLOR_OBJECT

    - Color signatures

    AiVision.CODE_OBJECT

    - Color codes

    # Display if an AprilTag or AI Classification
    # is detected
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
        vision_data = robot.vision.get_data(ALL_VISION)
        if vision_data:
            if vision_data[0].type == AiVision.AI_OBJECT:
                robot.screen.print("AI Object!")
            elif vision_data[0].type == AiVision.TAG_OBJECT:
                robot.screen.print("AprilTag!")
        wait(0.1, SECONDS)
    
    
    # Display a list of all detected objects
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
        vision_data = robot.vision.get_data(ALL_VISION)
        
        if vision_data:
            for obj in vision_data:
                if obj.type == AiVision.AI_OBJECT:
                    robot.screen.print("- AI Object")
                elif obj.type == AiVision.TAG_OBJECT:
                    robot.screen.print("- AprilTag")
                robot.screen.next_row()
        
        wait(0.1, SECONDS)
    
    

    构造函数#

    Creating a Color Signature#

    使用 Colordesc 构造函数创建一个新的颜色签名,然后使用 color_description 方法将其注册到 Colordesc 对象定义了传感器最多 7 个可检测颜色签名中的 1 个,但必须使用 color_description 明确设置才能生效。

    Colordesc 用法:
    Colordesc(index, red, green, blue, hangle, hdsat)

    范围

    描述

    index

    表示颜色签名索引的 1 到 7 之间的整数。如果两个颜色签名使用相同的索引,则第二个颜色签名将覆盖第一个。

    red

    表示颜色红色成分的 0 至 255 之间的整数。

    green

    表示颜色的绿色成分的 0 至 255 之间的整数。

    blue

    表示颜色蓝色成分的 0 至 255 之间的整数。

    hangle

    1 至 40 之间的浮点数,表示色调范围(以度为单位)。

    hdsat

    表示饱和度范围从 0.10 到 1.00 的浮点数。

    color_description 用法:
    robot.vision.color_description(object)

    范围

    描述

    object

    Colordesc 对象设置为可检测的颜色签名。

    # Detect a red object
    red_box = Colordesc(1, 207, 19, 25, 10.00, 0.20)
    robot.vision.color_description(red_box)
    
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
    
        red_boxes = robot.vision.get_data(red_box)
    
        if red_boxes:
            robot.screen.print("Red detected!")
        else:
            robot.screen.print("No red detected.")
    
        wait(0.2, SECONDS)
    
    

    Creating a Color Code#

    使用 Codedesc 构造函数创建一个新的颜色代码,然后使用 code_description 方法激活它。颜色代码将 2 到 4 个现有的 Colordesc 对象组合成一个标识符,AI 视觉传感器可以将其作为序列进行检测,但必须使用 code_description 明确设置才能生效。

    Codedesc 用法:
    Codedesc(index, c1, c2, c3, c4, c5)

    范围

    描述

    index

    颜色代码的索引,从 1 到 8。注意:如果创建两个具有相同索引的 Codedesc 对象,则第二个对象将覆盖第一个。

    c1

    代码中第一个 Colordesc 对象。

    c2

    代码中的第二个 Colordesc 对象。

    c3

    可选。第三个 Colordesc 对象。

    c4

    可选。第四个 Colordesc 对象。

    code_description 用法:
    robot.vision.code_description(object)

    范围

    描述

    object

    Codedesc 对象注册为可检测的颜色代码。

    # Create Color Signatures
    red_box = Colordesc(1, 207, 19, 25, 10.00, 0.20)
    purple_box = Colordesc(2, 98, 18, 227, 10.00, 0.20)
    
    robot.vision.color_description(red_box)
    robot.vision.color_description(purple_box)
    
    # Detect a red_purple Color Code
    red_purple = Codedesc(1, red_box, purple_box)
    robot.vision.code_description(red_purple)
    
    while True:
        robot.screen.clear_screen()
        robot.screen.set_cursor(1, 1)
    
        code_objects = robot.vision.get_data(red_purple)
    
        if code_objects:
            robot.screen.print("Code detected!")
        else:
            robot.screen.print("No code detected.")
    
        wait(0.2, SECONDS)