Self-Learning Classifier Example
Self-learning classifier lets HUSKYLENS 2 remember and recognize objects automatically, like a camera that learns faces and then calls them by name. It’s like teaching flashcards to a child who then reads them back. This demo initializes UNIHIKER K10 and Huskylens via I2C, switches to self-learning classification, continuously fetches recognition results, and prints the detected object’s ID and name on the screen when the learned target reappears.
1. Self-Learning Classifier Code Examples
Before You Start:
1.1 Recognizing Learned Objects
Under the Self-Learning Classifier function, once an object has been learned, HUSKYLENS 2 can recognize it when it encounters the object again. This example program can be used to retrieve the ID and name of the learned object when recognized.
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: self-learning classification; UNIHIKER K10: screen)
HuskylensV2 huskylens;
UNIHIKER_K10 k10;
uint8_t screen_dir = 2; // Screen display direction
// Main program initialization
void setup() {
k10.begin(); // Initialize UNIHIKER K10
Wire.begin(); // Initialize I2C communication
// Wait for Huskylens connection (retry every 100ms)
while (!huskylens.begin(Wire)) {
delay(100);
}
k10.initScreen(screen_dir); // Init screen with set direction
k10.creatCanvas(); // Create drawing canvas
huskylens.switchAlgorithm(ALGORITHM_SELF_LEARNING_CLASSIFICATION); // Set to self-learning classification mode
}
void loop() {
huskylens.getResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION); // Get latest self-learning classification data
// Check if classification data is available
if (huskylens.available(ALGORITHM_SELF_LEARNING_CLASSIFICATION)) {
// Simplify by storing center object data
auto centerObj = huskylens.getCachedCenterResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION);
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->updateCanvas(); // Refresh screen
delay(50); // 50ms delay for stable data
}
Upload the program, then wait for the upload to complete.
After HUSKYLENS 2 finishes learning an object, aim its camera at the learned object and observe the output. For detailed operation on object learning, please refer to: Self-Learning Classifier.
Running result:As follows, when a learned object appears in the image, it will be framed and its name, ID, and confidence level will be displayed. If the object's name is not set, the default name will be: Object.
Was this article helpful?
