智能视觉#

AI 视觉传感器必须连接到您的 V5 大脑,并在 VEXcode V5 中进行配置后才能使用。点击此处了解有关 VEX V5 AI 视觉传感器入门 的信息。

有关使用 AI 视觉传感器的更多信息,请参阅这些文章。

有关在 VEXcode V5 中使用带有块的 AI 视觉传感器的更多详细信息,请阅读使用 VEXcode V5 Python 中的 AI 视觉传感器进行编码

初始化 AiVision 类#

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

AiVision(port, colors, codes)

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

范围

描述

port

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

colors

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

codes

**可选。**一个或多个 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)

This ai_vision AiVision object and red Colordesc object will be used in all subsequent examples throughout this API documentation when referring to AiVision class methods.

类方法#

take_snapshot()#

The take_snapshot(type, count) method takes the current snapshot visible to the AI Vision Sensor and detects the objects of a given signature, code, or signature id.

参数

描述

类型

ColordescCodedescTagdescAiObjdesc 对象。

数数

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

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

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

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

  • id

  • centerX and centerY

  • originX and originY

  • width and height

  • angle

  • exists

  • score

To access an object’s property, use the name of the tuple, and then the object’s index. For example, if the tuple is stored in a variable named vision_objects you would call the width property as: vision_objects[0].width

ID#

The id property is only available for AprilTags and AI Classifications.

For an AprilTag, the id property represents the detected AprilTag(s) ID number.

For AI Classifications, the id property represents the specific type of AI Classification detected. For more information on what IDs AI Classifications have, go to this article.

To call the id property, a tuple must be created first using the ai_vision.take_snapshot command.

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

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

  • AprilTag 1 的索引为 0。

  • AprilTag 3 的索引为 1。

  • AprilTag 15 的索引为 2。

To call this property, use the tuple followed by the index of the detected object to pull the property from. For example: vision_objects[0].id.

centerX 和 centerY#

The centerX and centerY properties report the center coordinates of the detected object in pixels.

To call these properties, a tuple must be created first using the ai_vision.take_snapshot command.

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

To call this property, use the tuple followed by the index of the detected object to pull the property from. For example: 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#

The originX and originY properties report the coordinates, in pixels, of the top-left corner of the object’s bounding box.

To call these properties, a tuple must be created first using the ai_vision.take_snapshot command.

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

To call a property, use the objects method followed by the index of the detected object to pull the property from. For example: 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)

宽度和高度#

The width and height properties report the width or height of the object in pixels.

To call these properties, a tuple must be created first using the ai_vision.take_snapshot command.

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

To call a property, use the objects method followed by the index of the detected object to pull the property from. For example: 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)

角度#

The angle property is only available for Color Codes and AprilTags.

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

To call the angle property, a tuple must be created first using the ai_vision.take_snapshot command.

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

To call this property, use the tuple followed by the index of the detected object to pull the property from. For example: 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)

存在#

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

To call the exists property, a tuple must be created first using the ai_vision.take_snapshot command.

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

To call this property, use the tuple followed by the index of the detected object to pull the property from. For example: 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)

分数#

The score property is only available for AI Classifications.

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

To call the exists property, a tuple must be created first using the ai_vision.take_snapshot command.

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

To call this property, use the tuple followed by the index of the detected object to pull the property from. For example: vision_objects[0].score.

object_count()#

The object_count() method returns the amount of detected objects in the last use of the take_snapshot method.

Returns: An integer as the amount of detected objects in the last use of the take_snapshot method.

tag_detection()#

The tag_detection(enable) method enables or disables AprilTag detection.

参数

描述

使能够

True to enable AprilTag detection. False to disable it.

**返回:**无。

color_detection()#

The color_detection(enable, merge) method enables or disables color and code object detection.

参数

描述

使能够

True to enable color and color code detection. False to disable it.

合并

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

**返回:**无。

model_detection(enable)#

The model_detection(enable) method enables or disables AI model object detection.

参数

描述

使能够

True to enable AI model object detection. False to disable it.

**返回:**无。

start_awb()#

The start_awb() method runs auto white balance.

**返回:**无。

set()#

The set() method sets a new Color Signature or Color Code.

**返回:**无。

installed()#

The installed() method checks for device connection.

Returns: True if the AI Vision Sensor is connected. False if it is not.

timestamp()#

The timestamp() method requests the timestamp of the last received status packet from the AI Vision Sensor.

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