[Arduino][Arduino IDE][C++] Object Tracking
This tutorial covers programming the object tracking function with Arduino and HUSKYLENS 2.HUSKYLENS 2 tracks a box-selected object via Arduino, streaming ID, name and center XY over UART, like a smart camera ruler.
1. Object Tracking Code Example
Before You Start:
1.1 Output Related Data When Tracking Objects
When HUSKYLENS 2 detects a trackable target object, it obtains related tracking data and outputs it via UART. The data that can be read includes: object ID, name, xy coordinates, width, and height.
The example program is shown below:
#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_OBJECT_TRACKING);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_TRACKING);
if ((huskylens.available(ALGORITHM_OBJECT_TRACKING))) {
Serial.println((String("ID of the tracked object: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, ID)))));
Serial.println((String("Name of the tracked object: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, name)))));
Serial.println((String("Center coordinates of the tracked object: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_TRACKING), Result, yCenter)))))));
}
delay(500);
}
Align the HUSKYLENS 2 camera with the object to be tracked in the frame (the target must be box-selected first, see Object Tracking Function for details). After box-selecting the target, open the Serial Monitor to observe the output results.
Running Result: The object ID, name, xy coordinates, width, and height of the tracked object will be output. The object name defaults to "Object".
Was this article helpful?
