비전 센서#

소개#

비전 센서는 색상 특징과 색상 코드를 감지하고 추적할 수 있는 카메라 기반 센서입니다. 이를 통해 로봇은 주변 환경을 분석하고 시각적 입력에 반응할 수 있습니다.

아래 예시에서는 구성된 비전 센서의 이름 vision_1로 지정되고, 구성된 색상 시그니처 객체(예: RED_BOX)는 이 API 문서 전체의 후속 예시에서 Vision 클래스 메서드를 참조할 때 사용됩니다.

다음은 모든 방법 목록입니다.

데이터 가져오기 — 비전 센서에서 데이터를 가져옵니다.

  • take_snapshot — 특정 서명에 대한 데이터를 캡처합니다.

  • installed — 비전 센서가 IQ(2세대) 브레인에 연결되어 있는지 여부.

속성 — take_snapshot에서 반환된 객체 데이터.

  • largest_object — 비전 센서 스냅샷에서 가장 큰 객체를 즉시 선택합니다.

  • .exists — 현재 감지에 객체가 존재하는지 여부.

  • .width — 감지된 객체의 너비.

  • .height — 감지된 객체의 높이.

  • .centerX — 객체 중심의 X 위치.

  • .centerY — 객체 중심의 Y 위치.

  • .angle — 색상 코드의 방향.

  • .originX — 객체의 왼쪽 상단 모서리의 X 위치.

  • .originY — 객체의 왼쪽 상단 모서리의 Y 위치.

생성자 — 센서를 수동으로 초기화하고 구성합니다.

  • Vision — 비전 센서를 생성합니다.

  • Signature — 색상 서명을 생성합니다.

  • Code — 색상 코드를 생성합니다.

게터스#

take_snapshot#

take_snapshot 비전 센서 프레임의 데이터를 단일 시그니처(센서가 인식할 수 있는 항목에 대한 저장된 설명, 예를 들어 구성된 색상 시그니처 또는 색상 코드)로 필터링하고 튜플을 반환합니다.

색상 서명 및 색상 코드는 이 방법을 사용하기 전에 먼저 Vision Utility에서 구성해야 합니다.

튜플은 인덱스 0부터 시작하여 너비가 큰 순서부터 작은 순서로 정렬된 객체를 저장합니다. 각 객체의 속성은 해당 인덱스를 사용하여 접근할 수 있습니다. 일치하는 객체가 감지되지 않으면 빈 튜플이 반환됩니다.

Usage:
vision_1.take_snapshot(SIGNATURE)

매개변수

설명

SIGNATURE

지정된 서명에 해당하는 데이터만 포함하도록 데이터 세트를 필터링합니다. 사용 가능한 서명은 다음과 같습니다.

  • 색상 서명 또는 색상 코드 - 비전 센서 이름, 두 개의 밑줄, 그리고 색상 서명 또는 색상 코드 이름입니다. 예: vision_1__RED_BOX.

# Move forward if a red object is detected
while True:
    red_box = vision_1.take_snapshot(vision_1__RED_BOX)
    if red_box[0].exists:
        drivetrain.drive_for(FORWARD, 10, MM)
    wait(5, MSEC)

색상 특징#

컬러 시그니처는 비전 센서가 인식할 수 있는 고유한 색상입니다. 이러한 시그니처를 통해 비전 센서는 색상을 기반으로 객체를 감지하고 추적할 수 있습니다. 컬러 시그니처가 구성되면 센서는 시야 내에서 해당 특정 색상의 객체를 식별할 수 있습니다. 컬러 시그니처는 take_snapshot와 함께 사용하여 실시간으로 색상이 있는 객체를 처리하고 감지합니다.

프로젝트에서 구성된 컬러 시그니처를 사용하려면 해당 이름은 비전 센서 이름, 두 개의 밑줄, 그리고 컬러 시그니처 이름이어야 합니다. 예를 들어: 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 = vision_1.take_snapshot(vision_1__RED_BOX)
    if red_box[0].exists:
        brain.screen.print("Color signature detected!")
        wait(100, MSEC)

색상 코드#

컬러 코드는 특정 순서로 배열된 색상 특징으로 구성된 구조화된 패턴입니다. 이러한 코드를 통해 비전 센서는 미리 정의된 색상 패턴을 인식할 수 있습니다. 컬러 코드는 복잡한 물체를 식별하거나 자율 주행을 위한 고유한 마커를 생성하는 데 유용합니다.

프로젝트에서 구성된 색상 코드를 사용하려면 해당 이름은 센서 이름, 두 개의 밑줄, 그리고 색상 코드 이름이어야 합니다. 예를 들어: 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 = vision_1.take_snapshot(vision_1__BOX_CODE)
    if box_code[0].exists:
        brain.screen.print("Color code detected!")
        wait(100, MSEC)

installed#

installed 비전 센서가 현재 IQ(2세대) 브레인에 연결되어 있는지 여부를 나타내는 부울 값을 반환합니다.

  • True — 비전 센서가 IQ(2세대) 브레인에 연결되어 있습니다.

  • False — 비전 센서가 IQ(2세대) 브레인에 연결되어 있지 않습니다.

Usage:
vision_1.installed()

매개변수

설명

이 메서드에는 매개변수가 없습니다.

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

속성#

largest_object#

largest_object 은 take_snapshot의 최신 사용에서 반환된 튜플에서 데이터를 가져오기 위해 감지된 가장 큰 객체를 검색합니다.

이 방법을 사용하면 인덱스를 지정하지 않고도 튜플에서 항상 가장 큰 객체를 가져올 수 있습니다.

Usage:
vision_1.largest_object()

# Turn slowly until the largest object is centered in
# front of the Vision Sensor
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)

while True:
    red_box = vision_1.take_snapshot(vision_1__RED_BOX)

    if red_box[0].exists:
        if 140 < vision_1.largest_object().centerX < 180: 
            drivetrain.stop()

    wait(10,MSEC)

Tuple Properties#

take_snapshot를 사용한 후 튜플에 저장된 각 객체에는 8개의 속성이 포함됩니다.

모든 속성 값은 take_snapshot 사용된 시점에서 비전 센서의 시야 내에서 감지된 객체의 위치와 크기를 나타냅니다. 이러한 값은 센서의 316 x 212 픽셀 해상도를 기준으로 픽셀 단위로 측정됩니다.

.존재합니다#

.exists 튜플에 해당 인덱스가 존재하는지 여부를 나타내는 부울 값을 반환합니다.

  • True — 인덱스가 존재합니다.

  • False — 인덱스가 존재하지 않습니다.

# Check if at least one red objects is detected
# You will receive an error if no objects are detected

while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1, 1)
    red_objects = vision_1.take_snapshot(vision_1__RED_BOX)

    if red_objects[0].exists:
        brain.screen.print("At least 1")
            
    else:
        brain.screen.print("No red objects")

    wait(0.5, SECONDS)

.너비#

.width 감지된 객체의 너비를 픽셀 단위로 반환하며, 이는 1에서 316 사이의 정수입니다.

# Move towards a blue object until its width is
# larger than 100 pixels
while True:
    blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)

    if blue_box[0].exists:
        if blue_box[0].width < 100:
            drivetrain.drive_for(FORWARD, 10, MM)
    else:
        drivetrain.stop()

    wait(50, MSEC)

.키#

.height 감지된 객체의 높이를 픽셀 단위로 반환하며, 이는 1에서 212 사이의 정수입니다.

# Move towards a blue object until its height is
# larger than 100 pixels
while True:
    blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)

    if blue_box[0].exists:
        if blue_box[0].height < 100:
            drivetrain.drive_for(FORWARD, 10, MM)
    else:
        drivetrain.stop()

    wait(50, MSEC)

.centerX#

.centerX 감지된 객체 중심의 x 좌표를 픽셀 단위로 반환하며, 이 값은 0에서 316 사이의 정수입니다.

# Turn slowly until the largest blue object is centered
# in front of the Vision Sensor.
drivetrain.set_turn_velocity(10, PERCENT)
drivetrain.turn(RIGHT)

while True:
    blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)

    if blue_box[0].exists:
        if 140 < vision_1.largest_object().centerX < 180: 
            drivetrain.stop()

    wait(10,MSEC)

.센터Y#

.centerY 감지된 객체 중심의 y 좌표를 픽셀 단위로 반환하며, 이는 0에서 212 사이의 정수입니다.

# Move towards a blue object until its
# center y-coordinate is more than 140 pixels
while True:
    blue_box = vision_1.take_snapshot(vision_1__BLUE_BOX)

    if blue_box[0].exists:
        if blue_box[0].centerY < 140:
            drivetrain.drive(FORWARD)
    else:
        drivetrain.stop()

    wait(50, MSEC)

.각도#

.angle 감지된 객체의 방향을 도 단위로 반환하며, 이는 0에서 359 사이의 정수입니다.

# Turn left or right depending on how a
# configured box code is rotated
while True: 
    box_code = vision_1.take_snapshot(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)

.오리진X#

.originX 감지된 객체의 경계 상자의 왼쪽 상단 모서리의 x 좌표를 픽셀 단위로 반환하며, 이는 0에서 316 사이의 정수입니다.

# Display if a red object is to the
# left or the right
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1,1)
    
    red_box = vision_1.take_snapshot(vision_1__RED_BOX)
    
    if red_box[0].exists:
        if red_box[0].originX < 160:
            brain.screen.print("To the left!")
        else: 
            brain.screen.print("To the right!")

    wait(50, MSEC)

.오리진Y#

.originY 감지된 객체의 경계 상자의 왼쪽 상단 모서리의 y 좌표를 픽셀 단위로 반환하며, 이는 0에서 212 사이의 정수입니다.

# Display if a red object is close or far
# from the robot
while True:
    brain.screen.clear_screen()
    brain.screen.set_cursor(1,1)
    
    red_box = vision_1.take_snapshot(vision_1__RED_BOX)

    if red_box[0].exists:
        if red_box[0].originY < 80:
            brain.screen.print("Far")
        else: 
            brain.screen.print("Close")

    wait(50, MSEC)

건설업자#

생성자는 VEXcode 외부에서 센서를 구성하는 데 필요한 Vision, Signature및 Code 객체를 수동으로 생성하는 데 사용됩니다.

Vision Sensor#

Vision 비전 센서를 생성합니다.

용법

Vision(port, brightness, sigs)

매개변수

설명

port

비전 센서가 연결된 스마트 포트는 1번부터 12번까지입니다.

brightness

선택 사항입니다. 비전 센서의 밝기 값은 1에서 100까지입니다.

sigs

선택 사항. 하나 이상의 색상 시그니처 또는 색상 코드 객체의 이름입니다.

예

# Create a new Signature "RED_BOX" with the Signature class
RED_BOX = Signature(1, -3911, -3435, -3673,10879, 11421, 11150,2.5, 0)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the "RED_BOX" Signature.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX)

# Move forward if a red object is detected
while True:
    red_object = vision_1.take_snapshot(RED_BOX)
    if red_object[0].exists:
        drivetrain.drive_for(FORWARD, 10, MM)
    wait(5, MSEC)

Color Signature#

Signature 컬러 시그니처를 생성합니다. 비전 센서에는 최대 7개의 서로 다른 컬러 시그니처를 동시에 저장할 수 있습니다.

용법:

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

매개변수

설명

index

Signature 객체의 인덱스는 1부터 7까지입니다. 참고: 동일한 인덱스 번호를 가진 Signature 객체를 두 개 생성하면 두 번째로 생성된 객체가 첫 번째 객체를 덮어씁니다.

uMin

비전 유틸리티에서 uMin 부터 시작하는 값입니다.

uMax

비전 유틸리티에서 uMax 부터 시작하는 값입니다.

uMean

비전 유틸리티에서 uMean 의 값은 .

vMin

비전 유틸리티에서 vMin 부터 시작하는 값입니다.

vMax

비전 유틸리티에서 vMax 부터 시작하는 값입니다.

vMean

Vision Utility에서 vMean 의 값입니다.

rgb

비전 유틸리티에서 rgb 의 값(rgb)입니다.

type

비전 유틸리티에서 type 의 값입니다.

컬러 시그니처를 생성하기 위한 값을 얻으려면 Vision Utility로 이동하십시오. 컬러 시그니처가 구성되면 구성 창에서 매개변수 값을 복사합니다.

예

# Create a new Signature RED_BOX with the Signature class
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440,2.5, 1)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the RED_BOX Signature.
vision_1 = Vision(Ports.PORT1, 100, RED_BOX)

# Move forward if a red object is detected
while True:
    red_object = vision_1.take_snapshot(RED_BOX)
    if red_object[0].exists:
        drivetrain.drive_for(FORWARD, 10, MM)
    wait(5, MSEC)

Color Code#

Code creates a Color Code. It requires at least two already defined Color Signatures in order to be used. Up to eight different Color Codes can be stored on a Vision Sensor at once.

용법:

Code(sig1, sig2, sig3, sig4, sig5)

매개변수

설명

sig1

이전에 생성된 색상 서명.

sig2

이전에 생성된 색상 서명.

sig3

선택 사항. 이전에 생성된 색상 서명.

sig4

선택 사항. 이전에 생성된 색상 서명.

sig5

선택 사항. 이전에 생성된 색상 서명.

예

# Create two new Signatures for a red and blue box
RED_BOX = Signature(1, 10121, 10757, 10439,-1657, -1223, -1440, 2.5, 1)
BLUE_BOX = Signature(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 = Code(RED_BOX, BLUE_BOX)
# Create a new Vision Sensor "vision_1" with the Vision
# class, with the red_box and blue_box Signatures.
vision_1 = Vision(Ports.PORT1, 100, 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 = vision_1.take_snapshot(RED_BLUE)
    if box_code[0].exists:
        brain.screen.print("Color code detected!")
        wait(100, MSEC)