Skip to main content

Face Detection and Recognition

DNN-based face detection and recognition using the FaceDetectorYN and FaceRecognizerSF classes.

FaceDetectorYN

DNN-based face detector class. Model download link: Face Detection YuNet

Constructor

Use the static create() method to create an instance.

create (from file)

String
Path to the requested model file
String
Path to the config file for compatibility (not requested for ONNX models)
Size
Size of the input image
float
default:"0.9"
Threshold to filter out bounding boxes of score less than the given value
float
default:"0.3"
Threshold to suppress bounding boxes that have IoU greater than the given value
int
default:"5000"
Number of bounding boxes to preserve from top rank based on score before NMS
int
default:"0"
ID of the backend (DNN backend)
int
default:"0"
ID of the target device
Returns: Pointer to FaceDetectorYN instance

create (from buffer)

String
Name of origin framework
std::vector<uchar>
Buffer with content of binary file with model weights
std::vector<uchar>
Buffer with content of text file containing network configuration

Methods

setInputSize

Sets the size for the network input.
Size
Size of the input image. This overwrites the input size used when creating the model.
Call this method when the size of the input image does not match the input size when creating the model.

getInputSize

Gets the current input size.
Returns: Current input size

setScoreThreshold

Sets the score threshold to filter out bounding boxes.
float
Threshold for filtering out bounding boxes

getScoreThreshold

Gets the current score threshold.
Returns: Current score threshold

setNMSThreshold

Sets the Non-maximum-suppression threshold.
float
Threshold for NMS operation to suppress bounding boxes that have IoU greater than the given value

getNMSThreshold

Gets the current NMS threshold.
Returns: Current NMS threshold

setTopK

Sets the number of bounding boxes preserved before NMS.
int
Number of bounding boxes to preserve from top rank based on score

getTopK

Gets the current top K value.
Returns: Current top K value

detect

Detects faces in the input image.
InputArray
Input image to detect faces in
OutputArray
Detection results stored in a 2D cv::Mat of shape [num_faces, 15]:
  • 0-1: x, y of bbox top left corner
  • 2-3: width, height of bbox
  • 4-5: x, y of right eye
  • 6-7: x, y of left eye
  • 8-9: x, y of nose tip
  • 10-11: x, y of right corner of mouth
  • 12-13: x, y of left corner of mouth
  • 14: face score
Returns: Number of faces detected

Example Usage


FaceRecognizerSF

DNN-based face recognizer class. Model download link: Face Recognition SFace

Constructor

Use the static create() method to create an instance.

create (from file)

String
Path to the ONNX model used for face recognition
String
Path to the config file for compatibility (not requested for ONNX models)
int
default:"0"
ID of the backend
int
default:"0"
ID of the target device
Returns: Pointer to FaceRecognizerSF instance

create (from buffer)

String
Name of the framework (ONNX, etc.)
std::vector<uchar>
Buffer containing the binary model weights
std::vector<uchar>
Buffer containing the network configuration

Enums

DisType

Distance types for calculating distance between face features.

Methods

alignCrop

Aligns detected face with the source input image and crops it.
InputArray
Input image
InputArray
Detected face result from the input image (from FaceDetectorYN)
OutputArray
Output aligned and cropped face image

feature

Extracts face feature from aligned image.
InputArray
Input aligned face image
OutputArray
Output face feature vector

match

Calculates the distance between two face features.
InputArray
First input feature vector
InputArray
Second input feature vector of the same size and type as face_feature1
int
default:"FR_COSINE"
Distance calculation method: FR_COSINE (cosine distance) or FR_NORM_L2 (L2 norm distance)
Returns: Distance between the two face features. Lower values indicate more similar faces.

Example Usage

Complete Workflow Example

See Also