[UNIHIKER K10][Arduino IDE][C++] Object Tracking
HUSKYLENS 2 tracks a boxed object and streams ID, name, x y center, width and height to the UNIHIKER K10 screen, like a live HUD overlay for object motion.
1. Object Tracking Code Example
Before You Start:
1.1 Track Object Output Related Data
When HUSKYLENS 2 detects a trackable target object, related tracking data can be obtained. The available data includes: object ID, name, xy coordinates, width, and height.
Example program is as follows.
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects: Huskylens (tracking) & UNIHIKER K10 (screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction (adjust as needed)
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10 hardware
Wire.begin(); // Initialize I2C for Huskylens communication
// Wait for Huskylens to connect (retry every 100ms until success)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Initialize screen with set direction
k10.creatCanvas(); // Create canvas for screen display
huskylens.switchAlgorithm(ALGORITHM_OBJECT_TRACKING); // Set Huskylens to tracking mode
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_TRACKING); // Fetch latest tracking data
// Check if valid tracking data is available
if (huskylens.available(ALGORITHM_OBJECT_TRACKING)) {
// Get center object's data (simplify repeated calls)
auto centerObj = huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING);
// Display object info (blue color: 0x0000FF)
k10.canvas->canvasText(String("Obj ID: ") + String(RET_ITEM_NUM(centerObj, Result, ID)), 1, 0x0000FF);
k10.canvas->canvasText(String("Obj Name: ") + String(RET_ITEM_STR(centerObj, Result, name)), 3, 0x0000FF);
k10.canvas->canvasText(String("(x,y): ") + String(RET_ITEM_NUM(centerObj, Result, xCenter)) + String(","), 5, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_NUM(centerObj, Result, yCenter)), 6, 0x0000FF); // Split coords to avoid line overflow
k10.canvas->canvasText(String("(w,h): ") + String(RET_ITEM_NUM(centerObj, Result, width)) + String(","), 7, 0x0000FF);
k10.canvas->canvasText(String(RET_ITEM_NUM(centerObj, Result, height)), 8, 0x0000FF); // Split size to avoid line overflow
}
k10.canvas->updateCanvas(); // Refresh screen to show latest info
delay(50); // Short delay to stabilize data reading
}
After uploading the program and waiting for completion, align the HUSKYLENS 2 camera with the target object (you must first box the target object, refer to the Object Tracking usage instructions: Object Tracking. Once boxed, aim at the target object to be tracked and observe the output.
Running Result:Outputs include the tracked object ID, name, xy coordinates, width, and height. The object name defaults to "Object". You can change the name in Settings.
Was this article helpful?
