AI視覺感測器#

介紹#

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 或配置的顏色簽章或色彩代碼-並傳回 元組。

要使用此方法,必須先在視覺實用程式中配置顏色簽名和顏色代碼。

该元组存储的对象按宽度从大到小排序,从索引 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 的值。

若要取得建立顏色簽名所需的值,請前往視覺實用程式。配置顏色簽名後,從配置視窗複製參數值。

例子

# 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)