Object Tracking Example

Object tracking on HUSKYLENS 2 lets Arduino read an object’s ID, name, center coordinates, width and height via UART. It’s like giving your robot a label and position for each item it sees, so you can align the camera, box-select a target, then continuously print tracking data from the cached center result in the loop.

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".

Interface Diagram

Was this article helpful?

TOP