Face Recognition Example

Face recognition on HUSKYLENS 2 is a smart camera mode that outputs face ID, name and bounding box data over I2C or UART in real time. It’s like a digital ruler that measures faces and tracks their position while filtering and locking onto a selected face ID for stable, ID-specific tracking in interactive or robotics projects.

1. Face Recognition Code Example

Before You Start:

1.1 Face Recognition Output Data

Under the face recognition function, when a face appears on the HUSKYLENS 2 screen, it can be detected and framed. We can obtain data about the face closest to the crosshair in the HUSKYLENS 2 image and print it via the serial port. The readable face data includes: face ID, face name, width, height, and the X and Y coordinates of the face center.

The I2C mode example program is as follows.

#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_FACE_RECOGNITION);
}
void loop() {
  huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
  if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
    Serial.println((String("Face ID") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, ID)))));
    Serial.println((String("Face name") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, name)))));
    Serial.println((String("Face center coordinates") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, yCenter)))))))));
    Serial.println((String("Face width") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, width)))));
    Serial.println((String("Face height") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, height)))));
    delay(500);
  }
}


The I2C mode example program is as follows.

The following sample code will only provide code for I2C mode. If you need to use UART mode, simply modify the code by following the initialization method in setup().

#include "SoftwareSerial.h"
#include "DFRobot_HuskylensV2.h"

HuskylensV2 huskylens;
SoftwareSerial Serial1(2, 3);




void setup() {
	delay(1000);
	Serial1.begin(9600);
    while (!huskylens.begin(Serial1)) {
        delay(100);
    }
	huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION);
}
void loop() {
	huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
	if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
		Serial.println((String("人脸的ID") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, ID)))));
		Serial.println((String("人脸名称") + String((RET_ITEM_STR(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, name)))));
		Serial.println((String("人脸中心点坐标") + String((String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, xCenter))) + String((String(",") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, yCenter)))))))));
		Serial.println((String("人脸宽") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, width)))));
		Serial.println((String("人脸高") + String((RET_ITEM_NUM(huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION), Result, height)))));
		delay(500);
	}
}

Running Result: After the program is successfully uploaded, Point the camera at a learned face. For a previously learned face, the output ID will match the ID displayed on the HUSKYLENS 2 screen.

Interface Diagram

Please refer to the detailed operation guide for learning how to perform face recognition:Face Recognition Function.

###1.2 Counting Faces and Outputting Specified Data

When multiple faces appear in the frame, use the example program below to count the number of faces and retrieve data related to a specific face ID.

Example Program Below:

#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_FACE_RECOGNITION);
}
void loop() {
	huskylens.getResult(ALGORITHM_FACE_RECOGNITION);
	if ((huskylens.available(ALGORITHM_FACE_RECOGNITION))) {
		Serial.println((String("Number of detected faces: ") + String((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))));
	}
	if (((huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1) != NULL))) {
		Serial.println((String("Total number of ID1 faces: ") + String((huskylens.getCachedResultNumByID(ALGORITHM_FACE_RECOGNITION, 1)))));
		Serial.println((String("Name of ID1 face: ") + String((RET_ITEM_STR(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, name)))));
		Serial.println((String("Center coordinates of ID1 face: ") + String((String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, xCenter))) + String((String(", ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, yCenter)))))))));
		Serial.println((String("Width of ID1 face: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, width)))));
		Serial.println((String("Height of ID1 face: ") + String((RET_ITEM_NUM(huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1), Result, height)))));
	}
	delay(500);
}

Running Result: As shown in the figure, running the program, the serial port prints the number of faces in the current image. When a face with ID 1 appears in the image, it prints the relevant data of Face ID 1.

Interface Diagram

Was this article helpful?

TOP