AI視覺感測器#
介紹#
aivision 類)用於控制和存取來自 EXP AI 視覺感測器的資料。 AI 視覺感測器可以偵測:
AI分類(例如遊戲物件)
AprilTag IDs
自訂顏色簽名
自訂顏色代碼
它提供物件數據,包括位置、大小、方向、分類 ID 和置信度分數。
The sensor processes visual information using an onboard AI model selected in the AI Vision Utility within VEXcode. The selected model determines which AI Classifications the sensor can detect. When using VS Code, the AI model must first be configured in VEXcode before it can be used in your program. Detected objects are returned through the objects array after takeSnapshot is called.
類別建構函數#
aivision(
int32_t index,
Args&... sigs );
參數#
範圍 |
類型 |
描述 |
|---|---|---|
|
|
AI視覺感測器連接的智慧型端口,記為 |
|
|
要向传感器注册的一种或多种检测类型:
|
人工智慧模型和分類#
AI視覺感測器能夠透過特定的AI分類來偵測不同的物體。根據在「設備」視窗中配置AI視覺感測器時所選擇的AI分類模型,可以偵測到不同的物體。目前可用的模型包括:
課堂要素
身分證號碼 |
人工智慧分類 |
|---|---|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
VEX V5 機器人大賽 高風險賽
身分證號碼 |
人工智慧分類 |
|---|---|
0 |
|
1 |
|
2 |
|
VEX V5 機器人競賽延期
身分證號碼 |
人工智慧分類 |
|---|---|
0 |
|
1 |
|
VEX V5 機器人競賽規則變更
身分證號碼 |
人工智慧分類 |
|---|---|
0 |
|
1 |
|
2 |
|
3 |
|
4 |
|
範例#
// Create Color Signatures
aivision::colordesc AIVision1__greenBox(
1, // index
85, // red
149, // green
46, // blue
23, // hangle
0.23 ); // hdsat
aivision::colordesc AIVision1__blueBox(
2, // index
77, // red
135, // green
125, // blue
27, // hangle
0.29 ); // hdsat
// Create a Color Code from two color signatures
aivision::codedesc AIVision1__greenBlue(
1, // code index
AIVision1__greenBox, // first color signature
AIVision1__blueBox ); // second color signature
// Create the AI Vision Sensor instance
aivision AIVision1(
PORT11, // Smart Port
AIVision1__greenBlue, // color code
aivision::ALL_AIOBJS ); // enable AI Classifications
成員功能#
第 aivision 部分包含以下成員函數:
takeSnapshot— 擷取特定顏色簽章、顏色代碼、AI 分類組或 AprilTag ID 群組的資料。installed— 返回 AI 視覺感測器是否連接到 EXP 大腦。
在呼叫 takeSnapshot 後,要存取偵測到的物件數據,請使用可用的 Properties。
在呼叫任何 aivision 成員函數之前,必須建立一個 aivision 實例,如下所示:
/* This constructor is required when using VS Code.
AI Vision Sensor configuration is generated automatically
in VEXcode using the Device Menu. Replace the values
as needed. */
// Create Color Signatures
aivision::colordesc AIVision1__greenBox(
1, // index
85, // red
149, // green
46, // blue
23, // hangle
0.23 ); // hdsat
aivision::colordesc AIVision1__blueBox(
2, // index
77, // red
135, // green
125, // blue
27, // hangle
0.29 ); // hdsat
// Create a Color Code from two color signatures
aivision::codedesc AIVision1__greenBlue(
1, // code index
AIVision1__greenBox, // first color signature
AIVision1__blueBox ); // second color signature
// Create the AI Vision Sensor instance
aivision AIVision1(
PORT1, // Smart Port
AIVision1__greenBlue, // color code
aivision::ALL_AIOBJS ); // enable AI Classifications
takeSnapshot#
從 AI 視覺感測器捕捉影像,使用選定的 AI 模型或配置的顏色簽名對其進行處理,並更新 objects 陣列。
Each call refreshes the objects array with the most recent detection results. Objects are ordered from largest to smallest (by width), beginning at index 0. If no objects are detected objectCount will be 0 and objects[i].exists will be false.
1 — 使用物件 ID 和物件類型拍攝快照。
int32_t takeSnapshot( uint32_t id, objectType type, uint32_t count );
2 — 使用 顏色簽名 拍攝快照。
int32_t takeSnapshot( const colordesc &desc, int32_t count = 8 );
3 — 使用 顏色代碼 拍攝快照。
int32_t takeSnapshot( const codedesc &desc, int32_t count = 8 );
4 — 使用 AprilTag ID 拍攝快照。
int32_t takeSnapshot( const tagdesc &desc, int32_t count = 8 );
5 — 使用 AI 分類 拍攝快照。
int32_t takeSnapshot( const aiobjdesc &desc, int32_t count = 8 );
Parameters6 — 使用物件描述符拍攝快照。
int32_t takeSnapshot( const objdesc &desc, int32_t count = 8 );
範圍 |
類型 |
描述 |
|---|---|---|
|
|
使用
注意: 在 VEXcode 中,可以直接使用 AI 分类名称(例如 blueBall)。在 VS Code 中,必须使用数字 ID。 |
|
|
指定与 |
|
|
用于检测特定对象的描述符。直接传递给
|
|
|
從快照中儲存的最大物件數。預設為 8。 |
傳回值
傳回 int32_t ,表示偵測到的與指定簽章或偵測類型相符的物件數量。
條筆記
AI視覺感應器必須先拍攝快照才能存取物體資料。
每次呼叫時都會刷新包含
objects物件的陣列。當指定
count,僅儲存偵測到的最大物件(最多指定數量)。
AI 分類取決於在 VEXcode 的 AI 視覺實用程式中選擇的模型。
範例
// Move forward if an object is detected
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Drivetrain.driveFor(forward, 50, mm);
}
wait(50, msec);
}
installed#
返回 AI 視覺感測器是否已連接到 EXP 大腦。
可用功能
bool installed();
參數
此函數不接受任何參數。
傳回值
傳回布林值,指示 AI 視覺感測器是否已連接:
true— AI 視覺感測器已連接。false— AI視覺感測器未連接。
// Display a message if the AI Vision Sensor is connected
if (AIVision1.installed()){
Brain.Screen.print("Installed!");
}
特性#
呼叫 takeSnapshot 會更新 AI 視覺感測器的偵測結果。每次快照都會刷新包含 objects 元素的數組,該數組包含根據請求的 AI 分類、顏色簽名、顏色代碼或 AprilTag ID 偵測到的物件。
AI Vision 資料透過儲存在 AIVisionSensor.objects[index]中的物件的屬性來訪問,其中 index 從 0開始。
物體依面積由大到小排序。
AI視覺感光元件影像解析度為 320 × 240像素物體位置和大小值以像素單位表示,相對於感測器的當前視圖。
以下房源可供選擇:
objectCount— 傳回從最近快照偵測到的物件數量。largestObject— 從最近的快照中選擇偵測到的最大物件。objects— 包含由takeSnapshot更新的偵測到的物件陣列。.exists— 物件條目是否包含有效資料。.width— 偵測到的物件寬度(以像素為單位)。.height— 偵測到的物體的高度(以像素為單位)。.centerX— 物件中心的 X 座標(以像素為單位)。.centerY— 物件中心的 Y 座標(以像素為單位)。.originX— 物件左上角的 X 座標(以像素為單位)。.originY— 物件左上角的 Y 座標(以像素為單位)。.angle— 顏色代碼或 AprilTag ID 的方向(以度為單位)。.id— 分類 ID 或 AprilTag ID。.score— AI 分類的信心分數。
objectCount#
objectCount 傳回 objects 數組中的項目數,以整數形式傳回。
AIVisionSensor.objectCount
成分 |
描述 |
|---|---|
|
您的AI視覺感測器實例的名稱。 |
範例
// Display the number of detected objects
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.clearLine(1);
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Brain.Screen.print("%d", AIVision1.objectCount);
}
wait(50, msec);
}
largestObject#
largestObject 從 objects 數組中檢索偵測到的最大物件。
此方法可用於始終從 objects 而無需指定索引。
AIVisionSensor.largestObject
成分 |
描述 |
|---|---|
|
您的AI視覺感測器實例的名稱。 |
範例
// Display the closest AprilTag ID
while (true) {
Brain.Screen.setCursor(1, 1);
Brain.Screen.clearLine(1);
AIVision1.takeSnapshot(aivision::ALL_TAGS);
if (AIVision1.objects[0].exists) {
Brain.Screen.print("%d", AIVision1.largestObject.id);
}
wait(50, msec);
}
objects#
objects 傳回偵測到的物件屬性陣列。使用此陣列可以存取各個物件的特定屬性值。
使用 takeSnapshot objects 儲存在 takeSnapshot 數組中的每個物件都包含十個屬性。
除 id 和 score 之外的所有屬性值均描述了在執行 takeSnapshot 操作時,AI 視覺感測器視野中偵測到的物體的位置和大小。這些值以像素為單位,基於感測器的 320 x 240 像素解析度。
AIVisionSensor.objects[index].property
成分 |
描述 |
|---|---|
|
您的AI視覺感測器實例的名稱。 |
|
數組中的物件索引。索引從 |
|
可用的物件屬性。 |
存在#
.exists 傳回指定的物件索引是否包含有效的偵測到的物件。
訪問
SensorName.objects[index].exists
傳回值
傳回布林值,指示指定的物件索引是否包含有效的偵測到的物件:
-
true— A valid object exists at the specified index. -
false— No object exists at the specified index.
範例
// Move forward if an object is detected
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Drivetrain.driveFor(forward, 50, mm);
}
wait(50, msec);
}
。寬度#
.width 傳回偵測到的物件的寬度。
訪問
SensorName.objects[index].width
傳回值
傳回一個 int16_t 表示偵測到的物件以像素為單位的寬度。該值的範圍從 1 到 320。
範例
// Approach an object until it's at least 100 pixels wide
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].width < 100) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
。高度#
.height 傳回偵測到的物件的高度。
訪問
SensorName.objects[index].height
傳回值
傳回一個 int16_t 表示偵測到的物件的高度(以像素為單位)。該值的範圍從 1 到 240。
範例
// Approach an object until it's at least 90 pixels tall
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].height < 90) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
.centerX#
.centerX 傳回偵測到的物件中心的 x 座標。
訪問
SensorName.objects[index].centerX
傳回值
傳回一個 int16_t 表示物件中心的 x 座標(以像素為單位)。該值的範圍從 0 到 320。
範例
// Turn until an object is directly in front of the sensor
Drivetrain.setTurnVelocity(10, percent);
Drivetrain.turn(right);
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].centerX > 140 && AIVision1.objects[0].centerX < 180) {
Drivetrain.stop();
}
}
wait(10, msec);
}
.centerY#
.centerY 傳回偵測到的物件的中心的 y 座標。
訪問
SensorName.objects[index].centerY
傳回值
傳回一個 int16_t 表示物件中心的 y 座標(以像素為單位)。該值的範圍從 0 到 240。
範例
// Approach an object until it's close to the sensor
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].centerY < 150) {
Drivetrain.drive(forward);
} else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
。角度#
.angle 傳回偵測到的顏色代碼或 AprilTag ID 的方向。
訪問
SensorName.objects[index].angle
傳回值
傳回一個 float 表示偵測到的顏色代碼或 AprilTag ID 的旋轉角度(以度為單位)。該值的範圍從 0 到 359.9。
範例
// Turn left or right depending on how a configured
// Color Code is rotated
while (true) {
AIVision1.takeSnapshot(AIVision1__redBlue);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].angle > 50 && AIVision1.objects[0].angle < 100) {
Drivetrain.turn(right);
}
else if (AIVision1.objects[0].angle > 270 && AIVision1.objects[0].angle < 330) {
Drivetrain.turn(left);
}
else {
Drivetrain.stop();
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
.originX#
.originX 傳回偵測到的物件邊界框左上角的 x 座標。
訪問
SensorName.objects[index].originX
傳回值
傳回一個 int16_t 表示物件邊界框原點的 x 座標(以像素為單位)。該值的範圍從 0 到 320。
範例
// Display if an object is to the left or the right
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.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 座標。
訪問
SensorName.objects[index].originY
傳回值
傳回一個 int16_t 值,表示物件邊界框原點的 y 座標(以像素為單位)。該值的範圍從 0 到 240。
範例
// Display if an object is close or far
while (true) {
Brain.Screen.clearScreen();
Brain.Screen.setCursor(1, 1);
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].originY < 110) {
Brain.Screen.print("Close");
} else {
Brain.Screen.print("Far");
}
}
wait(100, msec);
}
。 ID#
.id 傳回偵測到的 AprilTag ID 或 AI 分類 ID。
訪問
SensorName.objects[index].id
傳回值
傳回一個 int32_t ,表示偵測到的物件的 ID:
範例
// Move forward when AprilTag ID 1 is detected
while (true) {
AIVision1.takeSnapshot(aivision::ALL_TAGS);
if (AIVision1.objects[0].exists) {
if (AIVision1.objects[0].id == 1) {
Drivetrain.drive(forward);
}
} else {
Drivetrain.stop();
}
wait(50, msec);
}
。分數#
.score 傳回偵測到的 AI 分類的置信度分數。
訪問
SensorName.objects[index].score
傳回值
傳回 int16_t ,表示偵測到的 AI 分類的置信度得分,介於 1 到 100 之間。
分數越低,表示感測器對偵測到的物體信心越低;分數越高,表示感測器對偵測到的物體信心越高。
範例
// Display if a score is confident
while (true) {
AIVision1.takeSnapshot(aivision::ALL_AIOBJS);
if (AIVision1.objects[0].exists) {
Brain.screen.clearScreen();
Brain.screen.setCursor(1, 1);
if (AIVision1.objects[0].score > 95) {
Brain.Screen.print("Confident");
} else {
Brain.Screen.print("Not confident");
}
}
wait(50, msec);
}
