视觉传感器#
介绍#
视觉传感器是一种基于摄像头的传感器,可以检测和追踪颜色特征和颜色代码。它允许机器人分析周围环境并对视觉输入做出响应。
对于以下示例,配置的视觉传感器将被命名为 vision_1,配置的颜色签名对象(例如 RED_BOX)将在本 API 文档的所有后续示例中引用 Vision 类方法时使用。
以下是所有方法的列表:
数据获取器 — 从视觉传感器获取数据。
take_snapshot— 捕获特定签名的数据。installed— 视觉传感器是否连接到 IQ(第二代)大脑。
属性 — 从 take_snapshot 返回的对象数据。
largest_object— 立即从视觉传感器快照中选择最大的对象。.exists— 对象是否存在于当前检测中。.width— 检测到的对象的宽度。.height— 检测到的物体的高度。.centerX— 物体中心的 X 坐标。.centerY— 物体中心的 Y 坐标。.angle— 颜色代码的方向。.originX— 对象左上角的 X 坐标。.originY— 对象左上角的 Y 坐标。
构造函数——手动初始化和配置传感器。
获取器#
take_snapshot#
take_snapshot 将来自视觉传感器帧的数据过滤为单个签名(传感器可以识别的事物的保存描述,例如配置的颜色签名或颜色代码),并返回一个元组。
必须先在 Vision Utility 中配置 颜色签名 和 颜色代码,然后才能使用此方法。
该元组存储的对象按宽度从大到小排序,从索引 0 开始。每个对象的 属性 可以通过其索引访问。如果未检测到匹配的对象,则返回一个空元组。
Usage:
vision_1.take_snapshot(SIGNATURE)
参数 |
描述 |
|---|---|
|
Filters the dataset to only include data of the given signature. Available signatures are:
|
# 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(第二代)大脑。
True— 视觉传感器与 IQ(第二代)大脑相连。False— 视觉传感器未连接到 IQ(第二代)大脑。
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 后,存储在元组中的每个对象都包含八个属性。
所有属性值描述了在拍摄 take_snapshot 时,视觉传感器视野中检测到的物体的位置和大小。这些值以像素为单位进行测量,基于传感器的 316 x 212 像素分辨率。
.exists#
.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)
.centerY#
.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)
.originX#
.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)
.originY#
.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)
构造函数#
构造函数用于手动创建 Vision Signature和 Code 对象,这些对象对于在 VEXcode 之外配置传感器是必要的。
Vision Sensor#
Vision 创建视觉传感器。
用法
Vision(port, brightness, sigs)
参数 |
描述 |
|---|---|
|
视觉传感器连接到哪个智能端口,从 1 到 12。 |
|
可选。视觉传感器的亮度值,范围为 1 到 100。 |
|
例子
# 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 创建颜色签名。视觉传感器最多可同时存储七种不同的颜色签名。
用法:
Signature(index, uMin, uMax, uMean, vMin, vMax, vMean, rgb, type)
范围 |
描述 |
|---|---|
|
|
|
Vision Utility 中的值介于 |
|
Vision Utility 中的值介于 |
|
视觉实用工具中的 uMean 值介于 |
|
Vision Utility 中的值从 |
|
Vision Utility 中的值介于 |
|
视觉实用程序中的值从 |
|
视觉实用程序中的值从 |
|
Vision Utility 中 |
要获取创建颜色签名的值,请转到 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 分别用于创建颜色代码。使用颜色代码需要至少两个已定义的颜色签名。视觉传感器最多可以同时存储八个不同的颜色代码。
用法:
Code(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)