Face Emotion Recognition Example

Face emotion recognition on HUSKYLENS 2 lets Arduino users detect seven pre-trained emotions and display IDs, counts and positions on a UNIHIKER K10 screen. It’s like a smart mirror that labels how many faces look happy, sad or surprised in real time, helping beginners quickly build interactive, emotion-aware projects without training datasets.

1. Face Emotion Recognition Code Example

Before You Start:

1.1 Recognize Emotions and Output Relevant Data

Under the Face Emotion Recognition function, HUSKYLENS 2 can recognize 7 specific emotions: Anger (ID 1), Disgust (ID 2), Fear (ID 3), Happiness (ID 4), Neutral (ID 5), Sad (ID 6), and Surprise (ID 7). These emotions are pre-trained into HUSKYLENS 2 at the factory and do not require manual learning by users. For detailed function usage instructions of emotion recognition, please refer to Face Emotion Recognition.

The following example program can count the number of all recognized emotions in the current HUSKYLENS 2 camera feed and output the ID of a specified emotion.

The example Code:

#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: emotion recognition; 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_EMOTION_RECOGNITION);  // Set to emotion recognition mode
}

void loop() {
  huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);  // Get latest emotion recognition data

  if (huskylens.available(ALGORITHM_EMOTION_RECOGNITION)) {
    // Simplify index: 1-1 → 0 (more intuitive, same meaning)
    k10.canvas->canvasText(String("Center emotion ID: ") + String(RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_EMOTION_RECOGNITION), Result, ID)), 1, 0x0000FF);
    k10.canvas->canvasText(String("Total emotions: ") + String(huskylens.getCachedResultNum(ALGORITHM_EMOTION_RECOGNITION)), 3, 0x0000FF);
    k10.canvas->canvasText(String("First emotion ID: ") + String(RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_EMOTION_RECOGNITION, 0), Result, ID)), 5, 0x0000FF);
  }

  k10.canvas->updateCanvas();  // Refresh screen
  delay(50);                   // 50ms delay for stable data reading
}

Upload the program, then wait for the program upload to complete.

When any of the above seven expressions appear in the camera view, HUSKYLENS 2's screen will frame the expression and display the expression ID, name, and confidence level. Meanwhile, the UNIHIKER K10 screen will show the result data output by the program.

Running result:Outputs the specified expression ID and the total number of expressions in the picture.

Interface Diagram

1.2 Retrieve Relevant Data of Specified Expressions

When multiple expressions with the same ID appear in the frame, you can use the following sample program to count relevant data of that specific ID expression.

#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects (Huskylens: emotion recognition; 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_EMOTION_RECOGNITION);  // Set to emotion recognition mode
}

void loop() {
  huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);  // Get latest emotion recognition data

  // Optimize: Remove redundant "available()" check (keep only one layer)
  if (huskylens.available(ALGORITHM_EMOTION_RECOGNITION)) {
    // Check if ID4 emotion exists and display its info
    if (huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, 4) != NULL) {
      // Simplify index: 1-1 → 0 (more intuitive, same meaning)
      k10.canvas->canvasText(String("ID4 emotion count: ") , 1, 0x0000FF);
      k10.canvas->canvasText(String(huskylens.getCachedResultNumByID(ALGORITHM_EMOTION_RECOGNITION, 4)), 2, 0x0000FF);
      k10.canvas->canvasText(String("ID4 emotion name: "), 3, 0x0000FF);
      k10.canvas->canvasText(String(RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, 4), Result, name)), 4, 0x0000FF);
      k10.canvas->canvasText("First ID4 emotion", 5, 0x0000FF);
      k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_EMOTION_RECOGNITION, 4, 0), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_EMOTION_RECOGNITION, 4, 0), Result, yCenter)), 6, 0x0000FF);
    }
    k10.canvas->updateCanvas();  // Refresh screen when data is available
  }

  delay(50);  // Optimize: Remove redundant delay (keep only one 50ms delay)
}

Running Result:As follows, when multiple specified ID expressions appear in the image, you can obtain data such as the total count of expressions with that ID in the image, their names, and the coordinates of a specific expression under that ID.

Interface Diagram

Was this article helpful?

TOP