[Arduino][Arduino IDE][C++] Object Recognition
Last revision 2026/08/07
This tutorial covers programming the object recognition function with an Arduino main controller and HUSKYLENS 2.HUSKYLENS 2 object recognition with Arduino reads total objects, IDs near center, and first match via UART, then filters by specific ID to get count, name, and bounding box coordinates.
1. Object Recognition Code Example
Before You Start:
1.1 Output Related Data for Object Recognition
When in Object Recognition mode, HUSKYLENS 2 can recognize objects within the field of view (80 fixed object categories, see Object Recognition Function and output relevant data via UART. The readable data includes: total number of recognizable objects in the frame, the object ID closest to the center of the HUSKYLENS 2 camera view, and the first detected object.
Example Program 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_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION);
if ((huskylens.available(ALGORITHM_OBJECT_RECOGNITION))) {
Serial.println((String("Total number of detected objects: ") + String((huskylens.getCachedResultNum(ALGORITHM_OBJECT_RECOGNITION)))));
Serial.println((String("Name of the object near the center") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_RECOGNITION), Result, name)))));
Serial.println((String("Name of the first detected object") + String((RET_ITEM_STR(huskylens.getCachedResultByIndex(ALGORITHM_OBJECT_RECOGNITION, 1 - 1), Result, name)))));
}
delay(500);
}
Running Result: The system will output the total number of recognized objects and corresponding object ID numbers as required.
1.2 Get Relevant Data of the Specified Object in the Image
After HUSKYLENS 2 recognizes an object, it can obtain relevant data of the specified object in the image. For example, determine if a learned object is present in the image, get the name of the specified object, obtain the count of the same type of objects in the image. When multiple objects of the same type appear in the image, you can specify to obtain parameters of one of them, including name, X/Y coordinates, width, and height.
Example program as follows:
#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_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION);
Serial.println((String("Total number of detected objects: ") + String((huskylens.getCachedResultNum(ALGORITHM_OBJECT_RECOGNITION)))));
if (((huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 1) != NULL))) {
Serial.println((String("Number of ID1 objects: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_OBJECT_RECOGNITION, 1)))));
Serial.println((String("Name of ID1 object: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 1), Result, name)))));
Serial.println((String("Center coordinates of the first detected object with ID 1: ") + String((String((String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 1, 1-1), Result, xCenter))) + String(", "))) + String((RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 1, 1-1), Result, yCenter)))))));
}
delay(500);
}
Running Result: As shown in the figure, you can obtain the total number of objects in the image, the count of ID1 objects, their names, and the coordinates of the first detected ID1 object.
Was this article helpful?
