Tag Recognition Example
HuskyLens 2 tag recognition reads AprilTag IDs, content and positions. It’s like giving your robot a barcode scanner for visual tags. Code examples show how to switch the tag algorithm, count all tags, list learned IDs, get the center tag ID, and query specific tags by ID, content and XY center coordinates.
1.Tag recognition
Before You Start:
1.1 Tag Recognition - Output Relevant Data
HUSKYLENS 2 can recognize AprilTag tags appearing in the frame. You can get data about detected tags through programming. Readable tag data includes: data for a specific tag (such as ID, content, width, height, and X/Y coordinates of the center point) and the total number of detected tags.
Example Program:
from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *
u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_TAG_RECOGNITION)
line1=u_gui.draw_text(text="",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="",x=0,y=120,font_size=15, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_TAG_RECOGNITION)
if (huskylens.available(ALGORITHM_TAG_RECOGNITION)):
line1.config(text=(str("Tag count:") + str((huskylens.getCachedResultNum(ALGORITHM_TAG_RECOGNITION)))))
line2.config(text=(str("learned tag count:") + str((huskylens.getCachedResultMaxID(ALGORITHM_TAG_RECOGNITION)))))
line3.config(text=(str("Center count ID:") + str((huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_TAG_RECOGNITION) else -1))))
line4.config(text=(str("First tag ID:") + str((huskylens.getCachedResultByIndex(ALGORITHM_TAG_RECOGNITION, 1-1).ID if huskylens.getCachedResultByIndex(ALGORITHM_TAG_RECOGNITION, 1-1) else -1))))
Click 'Run' in Mind+ and wait for the program to upload.
Point the HuskyLens 2 camera at the tag in the frame to learn it. For detailed instructions on how to learn tags, refer to:Tag recognition function - Learn Tags.
Point the HUSKYLENS 2 camera at the tag and observe the results displayed on the M10 (User provided K10, but document context is M10) screen.
Output Result: As shown, it can output the number of detected tags (whether learned or not) and the specified tag ID. Unlearned tags have an ID of 0.
1.2 Get Data for a Specific Tag in the Frame
After HuskyLens 2 recognizes tags, it can acquire data for a specific tag in the frame. For example, it can determine if a tag with a specific ID is in the frame, get the count of tags with the same ID, and when multiple tags with the same ID appear, it can be set to acquire parameters for a specific one, including Name, Content, X/Y coordinates, Width, and Height.
Example Program:
from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *
u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_TAG_RECOGNITION)
line1=u_gui.draw_text(text="",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="",x=0,y=120,font_size=15, color="#0000FF")
line5=u_gui.draw_text(text="",x=0,y=160,font_size=15, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_TAG_RECOGNITION)
if (huskylens.available(ALGORITHM_TAG_RECOGNITION)):
line1.config(text=(str("Tag ID0 count:") + str((huskylens.getCachedResultNumByID(ALGORITHM_TAG_RECOGNITION, 0)))))
line2.config(text="First ID0")
line3.config(text=(str("Content:") + str((huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 1-1).content if huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 1-1) else -1))))
line4.config(text="Second ID0")
line5.config(text=(str("Coords:") + str((str((huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 2-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 2-1) else -1)) + str((str(",") + str((huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 2-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_TAG_RECOGNITION, 0, 2-1) else -1))))))))
Output Result: As shown, there are two unlearned tags (ID 0) in the frame. The first ID 0 tag is on the left, and its content is 10 The second ID 0 tag is on the right, and its coordinates are (318, 183).
Was this article helpful?
