เซ็นเซอร์ AI วิชั่น#

การแนะนำ#

เซ็นเซอร์ตรวจจับภาพ IQ AI สามารถตรวจจับและติดตามวัตถุ สี และรหัส AprilTag ได้ ทำให้หุ่นยนต์สามารถวิเคราะห์สภาพแวดล้อม ติดตามวัตถุ และตอบสนองตามข้อมูลภาพที่ตรวจพบได้

สำหรับตัวอย่างด้านล่าง เซ็นเซอร์ IQ AI Vision ที่กำหนดค่าไว้จะถูกตั้งชื่อว่า ai_vision_1และออบเจ็กต์ลายเซ็นสีที่กำหนดค่าไว้ เช่น RED_BOXจะถูกใช้ในตัวอย่างต่อๆ ไปทั้งหมดในเอกสาร API นี้เมื่ออ้างถึงเมธอดคลาส AiVision

ด้านล่างนี้คือรายการวิธีการทั้งหมด:

ตัวรับข้อมูล — รับข้อมูลจากเซ็นเซอร์ตรวจจับภาพ IQ AI

  • take_snapshot — บันทึกข้อมูลสำหรับลายเซ็นเฉพาะ

  • installed — เซ็นเซอร์ IQ AI Vision เชื่อมต่อกับ IQ (รุ่นที่ 2) Brain หรือไม่

คุณสมบัติ — ข้อมูลวัตถุที่ส่งคืนจาก take_snapshot

  • .exists — วัตถุนั้นมีอยู่ในการตรวจจับปัจจุบันหรือไม่

  • .width — ความกว้างของวัตถุที่ตรวจพบ

  • .height — ความสูงของวัตถุที่ตรวจพบ

  • .centerX — ตำแหน่ง X ของจุดศูนย์กลางของวัตถุ

  • .centerY — ตำแหน่ง Y ของจุดศูนย์กลางของวัตถุ

  • .angle — การวางแนวของรหัสสีหรือรหัส AprilTag

  • .originX — ตำแหน่ง X ของมุมบนซ้ายของวัตถุ

  • .originY — ตำแหน่ง Y ของมุมบนซ้ายของวัตถุ

  • .id — รหัสการจำแนกหรือแท็กของวัตถุ

  • .score — คะแนนความเชื่อมั่นสำหรับการจำแนกประเภท AI

ตัวสร้าง — เริ่มต้นและกำหนดค่าเซ็นเซอร์ด้วยตนเอง

  • AiVision — สร้างเซ็นเซอร์ AI Vision IQ

  • Colordesc — สร้างลายเซ็นสี

  • Codedesc — สร้างรหัสสี

ผู้รับ#

take_snapshot#

take_snapshot กรองข้อมูลจากเฟรมของเซ็นเซอร์ IQ AI Vision ให้เป็นลายเซ็นเดียว — คำอธิบายที่บันทึกไว้ของสิ่งที่เซ็นเซอร์สามารถจดจำได้ เช่น วัตถุที่ฝึกฝนไว้ล่วงหน้า รหัส AprilTag หรือลายเซ็นสีหรือรหัสสีที่กำหนดค่าไว้ — และส่งคืน tuple

ลายเซ็นสี และ รหัสสี จะต้องได้รับการกำหนดค่าใน Vision Utility ก่อนจึงจะสามารถใช้กับวิธีนี้ได้

The tuple stores objects ordered from largest to smallest by width, starting at index 0. Each object’s properties can be accessed using its index. An empty tuple is returned if no matching objects are detected.

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)

หมายเหตุ: ทูเปิลที่ส่งคืนสามารถเรียงลำดับตาม คุณสมบัติ ใดก็ได้ ไม่ใช่แค่ความกว้าง โดยใช้ฟังก์ชัน sorted ของ Python ตัวอย่างเช่น การเรียงลำดับตาม 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)

การจำแนกประเภท AI#

เซ็นเซอร์ตรวจจับภาพ IQ AI สามารถตรวจจับวัตถุต่างๆ ภายใต้การจำแนกประเภท 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 แบบผสมผสาน

หมายเลขประจำตัว

การจำแนกประเภท AI

0

GameElementsMixAndMatch.BEAM

1

GameElementsMixAndMatch.BLUE_PIN

2

GameElementsMixAndMatch.RED_PIN

3

GameElementsMixAndMatch.ORANGE_PIN

ยกระดับการแข่งขันหุ่นยนต์ VEX IQ

หมายเลขประจำตัว

การจำแนกประเภท AI

0

GameElementsLevelUp.BLUE_BEANBAG

1

GameElementsLevelUp.RED_BEANBAG

2

GameElementsLevelUp.YELLOW_BEANBAG

ลายเซ็นสี#

ลายเซ็นสีคือสีเฉพาะที่เซ็นเซอร์ IQ AI Vision สามารถจดจำได้ ลายเซ็นเหล่านี้ช่วยให้เซ็นเซอร์ IQ AI Vision ตรวจจับและติดตามวัตถุตามสีของวัตถุได้ เมื่อกำหนดค่าลายเซ็นสีแล้ว เซ็นเซอร์จะสามารถระบุวัตถุที่มีสีเฉพาะนั้นในขอบเขตการมองเห็นได้ ลายเซ็นสีใช้ร่วมกับ 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 Brain (รุ่นที่ 2) อยู่ในขณะนี้หรือไม่

  • True — เซ็นเซอร์ตรวจจับภาพ IQ AI เชื่อมต่อกับหน่วยประมวลผล IQ (รุ่นที่ 2)

  • False — เซ็นเซอร์ตรวจจับภาพ IQ AI ไม่ได้เชื่อมต่อกับหน่วยประมวลผล IQ (รุ่นที่ 2)

Usage:
ai_vision_1.installed()

พารามิเตอร์

คำอธิบาย

เมธอดนี้ไม่มีพารามิเตอร์

# Display a message if the AI Vision Sensor is connected
if ai_vision_1.installed():
    brain.screen.print("Installed!")

คุณสมบัติ#

มีคุณสมบัติสิบประการที่รวมอยู่ในแต่ละวัตถุที่จัดเก็บใน tuple หลังจากใช้ take_snapshot แล้ว

ค่าคุณสมบัติทั้งหมด ยกเว้น .id และ .score อธิบายตำแหน่งและขนาดของวัตถุที่ตรวจพบในมุมมองของเซ็นเซอร์ IQ AI Vision ในขณะที่ take_snapshot ถูกใช้งาน ค่าเหล่านี้วัดเป็นพิกเซล โดยอิงจากความละเอียด 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 ของวัตถุที่ตรวจพบในหน่วยพิกเซล ซึ่งเป็นจำนวนเต็มระหว่าง 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 ที่ตรวจพบในหน่วยพิกเซล ซึ่งเป็นจำนวนเต็มระหว่าง 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 ที่ตรวจพบในหน่วยองศา ซึ่งเป็นจำนวนเต็มระหว่าง 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 วัตถุที่ตรวจพบในหน่วยพิกเซล ซึ่งเป็นจำนวนเต็มระหว่าง 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 พบในหน่วยพิกเซล ซึ่งเป็นจำนวนเต็มระหว่าง 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 ส่งคืน ID ของ การจำแนกประเภท AI ที่ตรวจพบหรือ AprilTag ID เป็นจำนวนเต็ม การจำแนกประเภท AI ขึ้นอยู่กับโมเดลที่กำหนดค่าไว้ใน AI Vision Utility

สำหรับ AprilTag ID คุณสมบัติ .id แสดงถึงหมายเลข AprilTag ID ที่ตรวจพบในช่วง 0 ถึง 36 สำหรับการจำแนกประเภท AI คุณสมบัติ .id สอดคล้องกับ ID ที่กำหนดไว้ล่วงหน้าในส่วน การจำแนกประเภท AI

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

ผู้สร้าง#

Constructors are used to manually create AiVision, Colordesc, and Codedesc objects, which are necessary for configuring the IQ AI Vision Sensor outside of VEXcode.

AI Vision Sensor#

AiVision สร้างเซ็นเซอร์ตรวจจับภาพ AI IQ

วิธีใช้

Vision(port, sigs)

พารามิเตอร์

คำอธิบาย

port

เซ็นเซอร์ IQ AI Vision เชื่อมต่อกับ Smart Port ใด ตั้งแต่ 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 Vision

วิธีใช้:

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

พารามิเตอร์

คำอธิบาย

index

ดัชนีของอ็อบเจ็กต์ Colordesc ตั้งแต่ 1 ถึง 7 หมายเหตุ: การสร้างอ็อบเจ็กต์ Colordesc สองตัวที่มีหมายเลขดัชนีเดียวกัน จะทำให้ตัวที่สองที่สร้างขึ้นมานั้นแทนที่ตัวแรก

uMin

ค่าตั้งแต่ uMin ใน Vision Utility

uMax

ค่าตั้งแต่ uMax ใน Vision Utility

uMean

ค่าตั้งแต่ uMean ใน Vision Utility

vMin

ค่าตั้งแต่ vMin ใน Vision Utility

vMax

ค่าตั้งแต่ vMax ใน Vision Utility

vMean

ค่าตั้งแต่ vMean ใน Vision Utility

rgb

ค่าตั้งแต่ rgb ใน Vision Utility

type

ค่าตั้งแต่ type ใน Vision Utility

เพื่อให้ได้ค่าสำหรับการสร้างลายเซ็นสี ให้ไปที่ 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)