Face Recognition Example

HuskyLens 2 face recognition lets UNIHIKER K10 count faces, track center and first IDs, and read detailed keypoints and per‑ID stats; it’s like giving your microcontroller a smart camera that reports who is where in real time.

1. Face Recognition Code Example

Before You Start:

1.1 Output Relevant Data of Face Recognition

For Face Recognition, when a face appears on the HUSKYLENS 2 screen, it will be detected and framed, allowing you to obtain the total number of detected faces and relevant data of specified faces.

The I2C example program below:

#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_FACE_RECOGNITION);
}
void loop() {
  huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
  if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
    if (((huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1) != NULL))) {
      k10.canvas->canvasText((String("Total faces: ") + String((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))), 1, 0xFF0000);
      k10.canvas->canvasText((String("Center face ID: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, ID)))), 3, 0xFF0000);
      k10.canvas->canvasText((String("First face ID: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1), Result, ID)))), 5, 0xFF0000);
      k10.canvas->updateCanvas();
    }
  }
}

The UART example program below:

#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();
  delay(1000);
	Serial1.begin(9600, SERIAL_8N1, P0, P1);
    while (!huskylens.begin(Serial1)) {
        delay(100);
  k10.initScreen(screen_dir);
  k10.creatCanvas();
  huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION);
}
void loop() {
  huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
  if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
    if (((huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1) != NULL))) {
      k10.canvas->canvasText((String("Total faces: ") + String((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))), 1, 0xFF0000);
      k10.canvas->canvasText((String("Center face ID: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, ID)))), 3, 0xFF0000);
      k10.canvas->canvasText((String("First face ID: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1), Result, ID)))), 5, 0xFF0000);
      k10.canvas->updateCanvas();
    }
  }
}

Point the HuskyLens 2 camera at each face in the scene to learn them individually. For detailed operation on how to learn faces, please refer to:Face Recognition.

After learning is complete, point the camera at the learned face, and you will see the output result on the UNIHIKER K10 screen.

Running Result:As follows, you can retrieve the total number of faces detected in the scene, whether they have been learned or not; you can specify to retrieve the ID of the face closest to the center of the screen, and the ID of the first detected face (unlearned faces have ID 0).

Interface Diagram

1.2 Acquire Face Key-Point Data of a Specified Face

This feature enables capturing key-point and positional data of a specified face, including: Face ID, Face Name, Face width, Face height, Face center (X, Y) coordinates, Left eye (X, Y) coordinates, Right eye (X, Y) coordinates, Left mouth corner (X, Y) coordinates, Right mouth corner (X, Y) coordinates, and Nose (X, Y) coordinates.

For example, the sample program below can capture key-point data of a face positioned near the center of the camera view. This function also works for unlearned faces.

#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_FACE_RECOGNITION);
}
void loop() {
  huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
  if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
    k10.canvas->canvasText((String("Center face ID: ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, ID)))), 1, 0xFF0000);
    k10.canvas->canvasText((String("Left eye: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, leye_x))) + String((String(",") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, leye_y)))))))), 3, 0xFF0000);
    k10.canvas->canvasText((String("Right eye: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, reye_x))) + String((String(",") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, reye_y)))))))), 5, 0xFF0000);
    k10.canvas->canvasText((String("Left mouth: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, lmouth_x))) + String((String(",") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, lmouth_y)))))))), 7, 0xFF0000);
    k10.canvas->canvasText((String("Right mouth: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, rmouth_x))) + String((String(",") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, rmouth_y)))))))), 9, 0xFF0000);
    k10.canvas->canvasText((String("Nose: ") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, nose_x))) + String((String(",") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), FaceResult, nose_y)))))))), 11, 0xFF0000);
    k10.canvas->updateCanvas();
  }
}

Running Result:As shown in the following figure, running the program, the UNIHIKER K10 screen displays the face ID and the coordinate data of the facial features. Since this face has not been trained, the face ID is 0.

Interface Diagram

1.3 Acquire Relevant Data of a Specified Face

In addition to the data mentioned above, more facial information can be obtained, such as the total count of a specified face in the scene, the name of that face, and the relevant data of the first detected instance of that face. (Unlearned faces can also retrieve the above data)

For an example using a learned face, the sample program is as follows:

/*!
 * MindPlus
 * esp32s3bit
 *
 */
#include "unihiker_k10.h"
#include "DFRobot_HuskylensV2.h"
// Create objects
HuskylensV2  huskylens;
UNIHIKER_K10 k10;
uint8_t      screen_dir = 2;


// Main program initialization
void setup() {
  k10.begin();
  Wire.begin();
  // Wait for Huskylens connection (retry every 100ms)
  while (!huskylens.begin(Wire)) {
    delay(100);
  }
  k10.initScreen(screen_dir);
  k10.creatCanvas();
  huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION);
}

void loop() {
  huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
  if (huskylens.available(ALGORITHM_FACE_RECOGNITION)) {
    // Check if face with ID 1 exists
    if (huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1) != NULL) {
      k10.canvas->canvasText(String("ID1 face count: ") + String(huskylens.getCachedResultNumByID(ALGORITHM_FACE_RECOGNITION, 1)), 1, 0xFF0000);
      k10.canvas->canvasText(String("ID1 face name: ") + String(RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, name)), 3, 0xFF0000);
      k10.canvas->canvasText("First ID1 face", 5, 0xFF0000);
      // Simplify coordinate string concatenation and replace 1-1 with 0 (same meaning)
      k10.canvas->canvasText(String("Coords: ") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_FACE_RECOGNITION, 1, 0), Result, xCenter)) + String(",") + String(RET_ITEM_NUM(huskylens.getCachedIndexResultByID(ALGORITHM_FACE_RECOGNITION, 1, 0), Result, yCenter)), 6, 0xFF0000);
      k10.canvas->updateCanvas();
    }
  }
}

Running Result:As shown in the figure, after running the program, the UNIHIKER K10 screen displays the total count of ID1 faces in the scene, the name of that face, and the (XY) center coordinates of the first detected ID1 face.

Interface Diagram

Was this article helpful?

TOP