Tag Recognition Example
HuskyLens 2 tag recognition reads AprilTags, counts them, and streams ID, content and position over serial. It’s like a smart camera that labels every marker in view. Example Arduino code shows how to get the closest tag, the first tag, or all tags with a given ID, so makers can track tag layouts in real time.
1. Tag Recognition Code Example
Before You Start:
1.1 Output Related Data of Recognized Tags
Under the Tag Recognition function, HUSKYLENS 2 can recognize AprilTag labels present in the image. By programming, you can count the total number of tags, obtain the relevant data of the tag closest to the cross cursor in the image, and print it via the serial port. Readable tag data includes: tag ID, tag content, tag width, tag height, as well as the X and Y coordinates of the tag center. When multiple tags are present in the image, you can retrieve data of specified tags.
For detailed operation on how to use the Tag Recognition function, please refer to: Tag Recognition Function.
The Example Program:
#include "DFRobot_HuskylensV2.h"
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
huskylens.switchAlgorithm(ALGORITHM_TAG_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_TAG_RECOGNITION);
if ((huskylens.available(ALGORITHM_TAG_RECOGNITION))) {
Serial.println((String("Total number of tags: ") + String((huskylens.getCachedResultNum(ALGORITHM_TAG_RECOGNITION)))));
Serial.println((String("ID of the tag near the center: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, ID)))));
Serial.println((String("Center coordinates of the tag near the center: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION), Result, yCenter)))))));
Serial.println((String("ID of the first detected tag: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_TAG_RECOGNITION, 1-1), Result, ID)))));
}
delay(500);
}
When a tag is recognized, the HUSKYLENS 2 screen will frame the tag and display the parsed tag content in the top-left corner of the box. Open the Serial Monitor to observe the output data.
Running Result: As follows, unlearned tags default to ID 0. The tag content output via the serial port remains consistent with what is displayed on the HUSKYLENS 2 screen.
The following, for the learned tags, the tag ID and content from their serial port output must match the display on the HUSKYLENS 2 screen.
1.2 Output Data for a Specified Tag ID
When multiple tags with the same ID appear in the image (learned tag IDs are assigned in the order of learning, and unlearned ones have a unified ID of 0), use the following example program to count the number of tags with the same ID and obtain the relevant data for a specified ID tag.
Example Program:
#include "DFRobot_HuskylensV2.h"
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
huskylens.switchAlgorithm(ALGORITHM_TAG_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_TAG_RECOGNITION);
if ((huskylens.available(ALGORITHM_TAG_RECOGNITION))) {
if (((huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1) != NULL))) {
Serial.println((String("Total number of ID1 tags: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_TAG_RECOGNITION, 1)))));
Serial.println((String("Content of ID1 tag: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1), Result, content)))));
Serial.println((String("Center coordinates of the first ID1 tag: ") + String((String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_TAG_RECOGNITION, 1), Result, yCenter)))))))));
}
}
delay(500);
}
When multiple instances of the same tag are detected in the image, HUSKYLENS 2 can identify all tags with ID 1 (provided a tag has been learned and assigned ID 1). The serial port can monitor and count the total number of ID 1 tags in the current image, and simultaneously output data from the first detected ID 1 tag.
Was this article helpful?
