Object Recognition Example
HuskyLens 2 object recognition lets UNIHIKER K10 display total detected targets, center object name, first object ID, and, for a chosen ID, its count and coordinates in the image; it’s like giving your microcontroller a live, labeled map of everything the camera sees.
1. Object Recognition Code Example
Before You Start:
1.1 Output Related Data for Object Recognition
HUSKYLENS 2 can identify objects within its view (must be one of the 80 fixed identifiable categories, see Object Recognition Function Introduction for details: Object Recognition.
It can obtain object-related data, including: total number of identifiable objects in the view, ID number of the object closest to the center of the HUSKYLENS 2 camera view, and the first detected object.
The example program is as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2;
// Main program starts
void setup() {
k10.begin();
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir);
k10.creatCanvas();
huskylens.switchAlgorithm(ALGORITHM_OBJECT_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION);
if (huskylens.available(ALGORITHM_OBJECT_RECOGNITION)) {
k10.canvas->canvasText(String("Total objects: ") + String(huskylens.getCachedResultNum(ALGORITHM_OBJECT_RECOGNITION)), 1, 0x0000FF);
k10.canvas->canvasText(String("Center object: "), 3, 0x0000FF);
k10.canvas->canvasText((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_OBJECT_RECOGNITION), Result, name)), 5, 0x0000FF);
k10.canvas->canvasText(String("First object ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_OBJECT_RECOGNITION, 1 - 1), Result, ID)), 7, 0x0000FF);
}
k10.canvas->updateCanvas();
delay(50);
}
After the program is successfully uploaded, HuskyLens 2 enters Object Recognition mode. Align HuskyLens 2's camera with the objects in the view and perform individual learning.
Once learning is complete, align the camera with the target object, and the output results will be displayed on the UNIHIKER K10 screen.
Running Result: The total number of recognized objects can be output, with corresponding object IDs and names displayed as required. If a target object has not been learned, its ID will be 0.
1.2 Acquiring Relevant Data of Specified Objects
HuskyLens 2 can obtain relevant data of the specified object in the image after recognition. For example, it can determine if a specified object is present in the image, get the name of the specified object, and count how many specified objects of the same type are present in the image. When multiple objects of the same type appear in the image, you can specify to retrieve parameters of one of them, including name, X/Y coordinates, width, and height.
Example program is provided as follows:
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2;
// Main program starts
void setup() {
k10.begin();
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir);
k10.creatCanvas();
huskylens.switchAlgorithm(ALGORITHM_OBJECT_RECOGNITION);
}
void loop() {
huskylens.getResult(ALGORITHM_OBJECT_RECOGNITION);
if (huskylens.available(ALGORITHM_OBJECT_RECOGNITION)) {
if (huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 2) != NULL) {
k10.canvas->canvasText(String("ID2 count: ") + String(huskylens.getCachedResultNumByID(ALGORITHM_OBJECT_RECOGNITION, 2)), 1, 0x0000FF);
k10.canvas->canvasText(String("ID2 name: ") + String(RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_OBJECT_RECOGNITION, 2), Result, name)), 3, 0x0000FF);
k10.canvas->canvasText("First ID2 object", 5, 0x0000FF);
k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 2, 1-1), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_OBJECT_RECOGNITION, 2, 1-1), Result, yCenter)), 6, 0x0000FF);
}
}
k10.canvas->updateCanvas();
delay(50);
}
Running Result:As shown in the figure, you can obtain the total number of objects in the image, the quantity of the object with ID2 in the image, its name, and the coordinates of the first object with ID2 detected.
Was this article helpful?
