智能视觉#

初始化 AiVision 类#

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

AiVision(端口、颜色、代码)

该构造函数使用三个参数:

范围

描述

端口

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

颜色

**可选。**一个或多个 Colordesc 对象的名称。

代码

**可选。**一个或多个 Codedesc 对象的名称。

# Create a new Color Signature "red" with the Colordesc class.
red = 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.
ai_vision = AiVision(Ports.PORT1, red)

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

类方法#

拍摄快照()#

take_snapshot(type, count) 方法获取 AI 视觉传感器可见的当前快照并检测给定签名、代码或签名 ID 的对象。

参数

描述

类型

ColordescCodedescTagdescAiObjdesc 对象。

数数

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

拍摄快照会创建一个包含您指定的所有检测到的对象的元组。例如,如果您想检测“蓝色”颜色特征,而 AI 视觉传感器检测到了 3 个不同的蓝色物体,那么这三个物体的数据都会被放入这个元组中。

**返回:**检测到的对象元组,如果没有检测到任何对象,则返回空元组。

可以从元组中调用十种不同的属性来从指定的对象中获取数据。

  • id

  • centerXcenterY

  • originXoriginY

  • widthheight

  • 角度

  • 存在

  • 分数

要访问对象的属性,请使用元组的名称,然后使用对象的索引。例如,如果元组存储在名为“vision_objects”的变量中,则可以这样调用“width”属性:‘vision_objects[0].width’

ID#

id 属性仅适用于 AprilTagsAI Classifications

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

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

要调用“id”属性,必须先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

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

  • AprilTag 1 的索引为 0。

  • AprilTag 3 的索引为 1。

  • AprilTag 15 的索引为 2。

要调用此属性,请使用元组,后跟要从中提取属性的检测到的对象索引。例如:“vision_objects[0].id”。

centerX 和 centerY#

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

要调用这些属性,必须首先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

要调用此属性,请使用元组,后跟要从中提取属性的检测到的对象索引。例如:“vision_objects[0].centerX”。

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

while True:
    # Get a snapshot of all Blue Color Signatures and store
    # it in vision_objects.
    vision_objects = ai_vision.take_snapshot(ai_vision_1__Blue)

    # Check to make sure an object was detected in the
    # snapshot before pulling data.
    if vision_objects[0].exists == True

        # Check if the object isn't in the center of the
        # AI Vision Sensor's view.
        if vision_objects[0].centerX > 150 and 170 > vision_objects[0].centerX:

            # Keep turning right until the object is in the
            # center of the view.
            drivetrain.turn(RIGHT)
        else:
            drivetrain.stop()
    wait(5, MSEC)

originX 和 originY#

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

要调用这些属性,必须首先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

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

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

while True:
    # Get a snapshot of all Blue Color Signatures and store
    # it in vision_objects.
    vision_objects = ai_vision.take_snapshot(ai_vision_1__Blue)

    brain.screen.clear_screen()

    # Check to make sure an object was detected in the
    # snapshot before pulling data.
    if len(vision_objects) > 0:
        brain.screen.draw_rectangle(vision_objects[0].originX, vision_objects[0].originY, vision_objects[0].width, vision_objects[0].height)

    wait(5, MSEC)

宽度和高度#

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

要调用这些属性,必须首先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

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

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

while True:
    # Get a snapshot of all Blue Color Signatures and store
    # it in vision_objects.
    vision_objects = ai_vision.take_snapshot(ai_vision_1__Blue)

    # Check to make sure an object was detected in the
    # snapshot before pulling data.
    if vision_objects[0].exists == True

        # Check if the largest object is close to the
        # AI Vision Sensor by measuring its width.
        if vision_objects[0].width < 250:

            # Drive closer to the object until it's wider
            # than 250 pixels.
            drivetrain.drive(FORWARD)
        else:
            drivetrain.stop()

    wait(5, MSEC)

角度#

angle 属性仅适用于 Color CodesAprilTags

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

要调用“angle”属性,必须先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

要调用此属性,请使用元组,后跟要从中提取属性的检测到的对象索引。例如:“vision_objects[0].angle”。

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

while True:
    # Get a snapshot of all Blue Color Signatures and store
    # it in vision_objects.
    vision_objects = ai_vision.take_snapshot(AiVision.ALL_TAGS)

    brain.screen.clear_screen()

    # Check to make sure an object was detected in the
    # snapshot before pulling data.
    if len(vision_objects) > 0:
        brain.screen.print(vision_objects[0].angle)

    wait(5, MSEC)

存在#

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

要调用“exists”属性,必须先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

要调用此属性,请使用元组,后跟要从中提取属性的检测到的对象索引。例如:“vision_objects[0].exists”。

在这个例子中,在尝试从对象中提取数据进行导航之前,会检查该对象是否存在。

while True:
    # Get a snapshot of all Blue Color Signatures and store
    # it in vision_objects.
    vision_objects = ai_vision.take_snapshot(ai_vision_1__Blue)

    # Check to make sure an object was detected in the
    # snapshot before pulling data.
    if vision_objects[0].exists == True

        # Check if the largest object is close to the
        # AI Vision Sensor by measuring its width.
        if vision_objects[0].width < 250:

            # Drive closer to the object until it's wider
            # than 250 pixels.
            drivetrain.drive(FORWARD)
        else:
            drivetrain.stop()

    wait(5, MSEC)

分数#

score 属性仅适用于 AI 分类

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

要调用“exists”属性,必须先使用“ai_vision.take_snapshot”命令创建一个元组。

创建元组后,可以使用索引访问特定对象及其属性。元组按对象区域从大到小排序,索引从 0 开始。

要调用此属性,请使用元组,后跟要从中提取属性的检测到的对象索引。例如:“vision_objects[0].score”。

对象计数()#

object_count() 方法返回上次使用 take_snapshot 方法时检测到的对象数量。

返回: 一个整数,表示最后一次使用 take_snapshot 方法检测到的对象的数量。

标签检测()#

tag_detection(enable) 方法启用或禁用 AprilTag 检测。

参数

描述

使能够

设置为“True”则启用 AprilTag 检测,设置为“False”则禁用。

**返回:**无。

颜色检测()#

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

参数

描述

使能够

True 表示启用颜色和颜色代码检测。False 表示禁用。

合并

**可选。**一个布尔值,用于启用或禁用相邻颜色检测的合并。

**返回:**无。

模型检测(启用)#

model_detection(enable) 方法启用或禁用 AI 模型对象检测。

参数

描述

使能够

True 表示启用 AI 模型对象检测。False 表示禁用。

**返回:**无。

启动_awb()#

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

**返回:**无。

放()#

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

**返回:**无。

安装()#

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

返回: 如果 AI 视觉传感器已连接,则返回 True。如果未连接,则返回 False

时间戳()#

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

**返回:**最后接收的状态包的时间戳(以毫秒为单位)。