Tag Recognition Example
Tag recognition on HuskyLens 2 reads AprilTag IDs and content, then displays them on the UNIHIKER K10 screen in real time. It’s like a smart barcode scanner that also gives position and size. The sample code shows how to fetch latest results, count all tags, get a center tag ID, and query specific IDs for coordinates, width, height and content.
1. Tag Recognition Code Example
Before You Start:
1.1 Recognizing Tag Output Data
HUSKYLENS 2 can recognize AprilTag labels in the image, and you can retrieve the detected tag-related data through programming. The readable tag data includes: specified tag data, including tag ID, tag content, tag width, tag height, the X and Y coordinates of the tag's center point, and the total number of detected tags.
The example program as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: tag recognition; UNIHIKER K10: screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_TAG_RECOGNITION); // Set to tag recognition mode
}
void loop() {
// Fix: Get data FIRST before checking availability (ensure latest data is used)
huskylens.getResult(ALGORITHM_TAG_RECOGNITION);
if (huskylens.available(ALGORITHM_TAG_RECOGNITION)) {
// Simplify index: 1-1 → 0 (more intuitive, same meaning)
k10.canvas->canvasText(String("Center tag ID: ") + String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, ID)), 1, 0x0000FF);
k10.canvas->canvasText(String("Total tags: ") + String(huskylens.getCachedResultNum(ALGORITHM_TAG_RECOGNITION)), 3, 0x0000FF);
k10.canvas->canvasText(String("First tag ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_TAG_RECOGNITION, 0), Result, ID)), 5, 0x0000FF);
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data reading
}
After uploading the program, wait for the upload to complete.
Point the HUSKYLENS 2 camera at the tag in the image to learn it. For detailed operation on how to learn tags, please refer to the Tag Recognition function:Tag Recognition.
Point the HUSKYLENS 2 camera at the tag code and observe the result displayed on the K10 screen.
Running result: As shown, it can output the number of detected tag codes (whether the tag code has been learned or not) and the specified tag ID. Unlearned tag codes will be 0.
16.2 Acquire Relevant Data of the Specified Tag
After HuskyLens 2 recognizes a tag, it can obtain relevant data of the specified tag in the image. For example, you can determine if a tag with a specified ID exists in the image, and you can get the number of tags with the same ID in the image. When multiple tags with the same ID appear in the image, you can specify to get one of them, including name, content, X/Y coordinates, width, and height.
The example program:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: tag recognition; UNIHIKER K10: screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_TAG_RECOGNITION); // Set to tag recognition mode
}
void loop() {
// Fix 1: Get latest data FIRST before checking availability (avoid using old data)
huskylens.getResult(ALGORITHM_TAG_RECOGNITION);
if (huskylens.available(ALGORITHM_TAG_RECOGNITION)) {
// Check if ID0 tag exists and display its info
if (huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 0) != NULL) {
// Fix 2: Simplify index (1-1→0 for 1st tag, 2-1→1 for 2nd tag; more intuitive)
k10.canvas->canvasText(String("ID0 tag count: ") + String(huskylens.getCachedResultNumByID(ALGORITHM_TAG_RECOGNITION, 0)), 1, 0x0000FF);
k10.canvas->canvasText("1st ID0 tag", 3, 0x0000FF);
k10.canvas->canvasText(String("Content: ") + String(RET_ITEM_STR(huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 0), Result, content)), 4, 0x0000FF);
k10.canvas->canvasText("2nd ID0 tag", 6, 0x0000FF);
k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 1), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 1), Result, yCenter)), 7, 0x0000FF);
}
}
k10.canvas->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data reading
}
Running Result:As shown in the figure, there are two unregistered tag codes (ID 0) in the image. The first ID0 tag is on the left, and its content is 9. The second ID0 tag is on the left, and its coordinates are (247, 163).
Was this article helpful?
