艾维斯#
AI 视觉传感器必须连接到您的 V5 大脑,并在 VEXcode V5 中进行配置后才能使用。点击此处了解有关 VEX V5 AI 视觉传感器入门 的信息。
有关使用 AI 视觉传感器的更多信息,请参阅这些文章。
有关在 VEXcode V5 中使用带有块的 AI 视觉传感器的更多详细信息,请阅读使用 VEXcode V5 C++ 中的 AI 视觉传感器进行编码。
初始化 avision 类#
使用以下构造函数之一创建 AI 视觉传感器:
The aivision(int32_t port)
constructor creates an aiVision
object in the specified port.
范围 |
描述 |
---|---|
|
与 AI 视觉传感器连接的有效 智能端口。 |
// Create a new AI Vision Sensor "aiVision" with the aivision class.
aivision aiVision = aivision(PORT1);
The aivision(port, desc, …)
constructor uses two or more parameters:
范围 |
描述 |
---|---|
|
与 AI 视觉传感器连接的有效 智能端口。 |
|
// Create a new Color Signature "Red" with the colordesc class.
aivision::colordesc Red = aivision::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.
aivision aiVision = aivision(Ports.Port1, aiVision__Red);
This aiVision
AiVision object and aiVision__Red
Colordesc object will be used in all subsequent examples throughout this API documentation when referring to AiVision class methods.
类方法#
takeSnapshot()#
The takeSnapshot
method takes a picture of what the AI Vision Sensor is currently seeing and pulls data from that snapshot that can then be used in a project.
拍摄快照会存储您在 AI 视觉传感器实例中指定的所有检测到的对象。例如,如果您想检测“蓝色”颜色特征,而 AI 视觉传感器检测到了 3 个不同的蓝色对象,则这 3 个对象的数据都会被存入数组。
The takeSnapshot(desc, count)
method takes the current snapshot visible to the AI Vision Sensor and detects the objects of a specified object description.
参数 |
描述 |
---|---|
描述 |
|
数数 |
**可选。**要获取的对象的最大数量。默认值为 8。 |
**返回:**一个整数,表示与作为参数传递的描述匹配的对象的数量。
while (true){
// Take a snapshot of the red objects detected by
// the AI Vision Sensor.
aiVision.takeSnapshot(Red);
// Clear the screen/reset so that we can display
// new information.
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
// Print the largest detected object's CenterX
// coordinate to the Brain's screen.
Brain.Screen.print("Object Count: %d", aiVision.objectCount);
// Wait 0.5 seconds before repeating the loop and
// taking a new snapshot.
wait(0.5, seconds);
}
objects#
The objects
method allows you to access stored properties of objects from the last taken snapshot.
可用属性:
id
centerX
andcenterY
originX
andoriginY
width
andheight
angle
exists
score
To access an object’s property, use the name of the AI Vision Sensor, followed by the objects method, and then the object’s index. For example: aiVision.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 snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
注意:AprilTags 按其唯一 ID 升序排序,而不是按大小排序。例如,如果检测到 AprilTags 1、15 和 3:
AprilTag 1 位于索引 0。
AprilTag 3 位于索引 1。
AprilTag 15 位于索引 2。
To call this property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].id
.
centerX 和 centerY#
The centerX
and centerY
properties report the center coordinates of the detected object in pixels.
To call the centerX
or centerY
property, a snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
To call a property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].centerX
.
在此示例中,由于 AI 视觉传感器视图的中心是 (160, 120),因此机器人将向右转弯,直到检测到的物体的中心 X 坐标大于 150 像素,但小于 170 像素。
while (true) {
// Get a snapshot of all Blue Color objects.
aiVision.takeSnapshot(aiVision__Blue);
// Check to make sure an object was detected in the snapshot before pulling data.
if (aiVision.objectCount > 0) {
if (aiVision.objects[0].centerX > 150.0 && 170.0 > aiVision.objects[0].centerX) {
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 the originX
or originY
property, a snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
To call a property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].originX
.
在这个例子中,将在 Brain 的屏幕上绘制一个矩形,其尺寸与指定对象的边界框完全相同。
while (true) {
// Get a snapshot of all Blue objects.
aiVision.takeSnapshot(aiVision__Blue);
Brain.Screen.clearScreen();
// Check to make sure an object was detected in the snapshot before pulling data.
if (aiVision.objectCount > 0) {
Brain.Screen.drawRectangle(aiVision.objects[0].originX, aiVision.objects[0].originY, aiVision.objects[0].width, aiVision.objects[0].height);
}
wait(5, msec);
}
宽度和高度#
The width
and height
properties report the width or height of the object in pixels.
To call the width
or height
property, a snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
To call a property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].width
.
本例中,我们利用物体的宽度进行导航。机器人会逐渐靠近物体,直到宽度达到特定尺寸后才停止。
while (true) {
// Get a snapshot of all Blue objects.
aiVision.takeSnapshot(aiVision__Blue);
// Check to make sure an object was detected in the snapshot before pulling data.
if (aiVision.objectCount > 0) {
if (aiVision.objects[0].width < 250.0) {
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 snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
To call this property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].angle
.
在这个例子中,AprilTag 的角度被打印到 Brain 的屏幕上。
while (true) {
// Get a snapshot of all AprilTags.
aiVision.takeSnapshot(aivision::ALL_TAGS);
Brain.Screen.clearScreen();
// Check to make sure an object was detected in the
// snapshot before pulling data.
if (aiVision.objects[0].exists == true) {
Brain.Screen.print(aiVision.objects[0].angle);
}
wait(5, msec);
}
存在#
此属性返回一个布尔值,表示指定对象是否存在。
To call the exists
property, a snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
To call this property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].exists
.
在这个例子中,机器人在将其角度打印到 Brain 的屏幕上之前会检查是否检测到 AprilTag。
while (true) {
// Get a snapshot of all AprilTags.
aiVision.takeSnapshot(aivision::ALL_TAGS);
Brain.Screen.clearScreen();
// Check to make sure an object was detected in the
// snapshot before pulling data.
if (aiVision.objects[0].exists == true) {
Brain.Screen.print(aiVision.objects[0].angle);
}
wait(5, msec);
}
分数#
The score
property is only available for AI Classifications.
此属性返回指定 AI 分类的置信度分数。该分数的范围为 0% 到 100%,表示 AI 视觉传感器对其检测准确度的确定性。
To call the score
property, a snapshot must be taken using the aiVision.takeSnapshot
command. The array is sorted by object area in pixels, from largest to smallest, with indices starting at 0.
To call this property, use the objects
method followed by the index of the detected object to pull the property from. For example: aiVision.objects[0].score
.
objectCount#
The objectCount
property contains the number of objects found in the most recent snapshot.
**返回:**一个整数,表示在最近的快照中找到的对象的数量。
while (true){
// Take a snapshot of the red objects detected by
// the AI Vision Sensor.
aiVision.takeSnapshot(Red);
// Clear the screen/reset so that we can display
// new information.
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
// Print the largest detected object's CenterX
// coordinate to the Brain's screen.
Brain.Screen.print("Object Count: %d", aiVision.objectCount);
// Wait 0.5 seconds before repeating the loop and
// taking a new snapshot.
wait(0.5, seconds);
}
tagDetection()#
The tagDetection(enable)
method enables or disables apriltag detection.
参数 |
描述 |
---|---|
使能够 |
|
**返回:**无。
colorDetection()#
The colorDetection(enable, merge)
method enables or disables color and code object detection.
参数 |
描述 |
---|---|
使能够 |
|
合并 |
A boolean value which enables or disables the merging of adjacent color detections. The default is |
**返回:**无。
modelDetection()#
The modelDetection(enable)
method enables or disables AI model object, also known as AI Classification detection.
参数 |
描述 |
---|---|
使能够 |
|
**返回:**无。
startAwb()#
The startAwb()
method runs auto white balance.
**返回:**无。
set()#
The set(desc)
method sets a new Color Signature or Color Code.
参数 |
描述 |
---|---|
描述 |
**返回:**无。
timestamp()#
The timestamp()
method requests the timestamp of the last received status packet from the AI Vision Sensor.
**返回:**最后一个状态包的时间戳,以毫秒为单位的无符号 32 位整数。
installed()#
The installed()
method checks for device connection.
Returns: true
if the AI Vision Sensor is connected. false
if it is not.