Face Emotion Recognition Example
Face emotion recognition on HUSKYLENS 2 counts seven preset moods, like a smart mirror reading faces in real time, then prints each label and total via Arduino serial output.
1. Face Emotion Recognition Code Example
Before You Start:
1.1 Recognize All Emotions in the Camera Frame
Under the Face Emotion Recognition function, HUSKYLENS 2 can recognize 7 specific emotions: Angry (ID 1), Disgust (ID 2), Fear (ID 3), Happy (ID 4), Neutral (ID 5), Sad (ID 6), and Surprised (ID 7). These emotions are pre-trained on HUSKYLENS 2 at the factory and do not require manual learning by users. For detailed functional instructions on face emotion recognition, please refer to Emotion Recognition Function.
The following example code can count the number of each emotion recognized in the current HUSKYLENS 2 camera frame and output the relevant data for each emotion in sequence.
Example code as follows:
#include "DFRobot_HuskylensV2.h"
// Dynamic variables
volatile float mind_n_EmotionTotal, mind_n_IndexVariable;
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
mind_n_EmotionTotal = 0;
mind_n_IndexVariable = 0;
}
void loop() {
huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);
if ((huskylens.available(ALGORITHM_EMOTION_RECOGNITION))) {
mind_n_EmotionTotal = (huskylens.getCachedResultNum(ALGORITHM_EMOTION_RECOGNITION));
Serial.println((String("Total number of emotions in the frame: ") + String(mind_n_EmotionTotal)));
while (!(mind_n_IndexVariable >= mind_n_EmotionTotal)) {
mind_n_IndexVariable += 1;
Serial.println((String((String("The ") + String((String(mind_n_IndexVariable) + String("th emotion is: "))))) + String((RET_ITEM_STR(huskylens.getCachedResultByIndex(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable - 1), Result, name)))));
}
mind_n_IndexVariable = 0;
}
delay(500);
}
When any of the above seven expressions appear in the camera view, the HUSKYLENS 2 screen will frame the expression and display its name and confidence score.
Running Result: Observe the serial port output for the total number of recognized expressions and their corresponding names.
1.2 Counting the Number of Each Emotion Type
Given that the face emotion recognition function outputs IDs in the range 1-7 (corresponding to 7 emotions), we can use the following example program to count the number of each emotion type recognized in the current HUSKYLENS 2 camera view.
#include "DFRobot_HuskylensV2.h"
// Dynamic variables
volatile float mind_n_IndexVariable;
// Create object
HuskylensV2 huskylens;
// Main program starts
void setup() {
Serial.begin(9600);
Wire.begin();
while (!huskylens.begin(Wire)) {
delay(100);
}
huskylens.switchAlgorithm(ALGORITHM_EMOTION_RECOGNITION);
mind_n_IndexVariable = 1;
}
void loop() {
huskylens.getResult(ALGORITHM_EMOTION_RECOGNITION);
if ((huskylens.available(ALGORITHM_EMOTION_RECOGNITION))) {
if ((mind_n_IndexVariable <= 7)) {
if (((huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable) != NULL))) {
Serial.println((String((String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable), Result, name))) + String(" total count: "))) + String((huskylens.getCachedResultNumByID(ALGORITHM_EMOTION_RECOGNITION, mind_n_IndexVariable)))));
}
mind_n_IndexVariable += 1;
}
else {
mind_n_IndexVariable = 1;
}
}
delay(500);
}
Running Result: Run the program, and when multiple faces with different expressions appear in the image, observe the UART output to see the name and corresponding count of each emotion category currently recognized.
Was this article helpful?
