[Arduino][Arduino IDE][C++] Self‑Learning Classifier

Last revision 2026/08/07

HUSKYLENS 2 self-learning classifier for Arduino lets the camera learn custom objects and later recognize them, like labeling photos automatically. Using the example code, it switches to the self-learning algorithm, reads recognition results, and prints each object’s ID and name to the Serial Monitor, defaulting to “Object” if no name is set.

1. Self-Learning Classifier Code Example

Before You Start:

1.1 Output ID and Name of Learned Objects

During the Self-Learning Classifier function, HUSKYLENS 2 can learn and recognize any custom object. For detailed usage instructions on the custom classification function, please refer to the Self-Learning Classifier Function.

During the Self-Learning Classifier function, once an object has been learned, HUSKYLENS 2 will recognize it when encountered again. Use the following example program to obtain the corresponding ID and name of the learned object.

#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_SELF_LEARNING_CLASSIFICATION);
}
void loop() {
  huskylens.getResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION);
  if ((huskylens.available(ALGORITHM_SELF_LEARNING_CLASSIFICATION))) {
    Serial.println((String("ID of the recognized object: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION), Result, ID)))));
    Serial.println((String("Name of the recognized object: ") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_SELF_LEARNING_CLASSIFICATION), Result, name)))));
  }
}

Running Result: As follows, when a previously learned object appears in the image, it will be framed and its ID and confidence level will be displayed simultaneously. Open the Serial Monitor to observe the corresponding ID and name in the output. If the name of the object is not set, the output name defaults to: Object.

Interface Diagram

Was this article helpful?

TOP