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)

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

create (from buffer)

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

Methods

setInputSize

Sets the size for the network input.
input_size
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.
score_threshold
float
Threshold for filtering out bounding boxes

getScoreThreshold

Gets the current score threshold.
Returns: Current score threshold

setNMSThreshold

Sets the Non-maximum-suppression threshold.
nms_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.
top_k
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.
image
InputArray
Input image to detect faces in
faces
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)

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

create (from buffer)

framework
String
Name of the framework (ONNX, etc.)
bufferModel
std::vector<uchar>
Buffer containing the binary model weights
bufferConfig
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.
src_img
InputArray
Input image
face_box
InputArray
Detected face result from the input image (from FaceDetectorYN)
aligned_img
OutputArray
Output aligned and cropped face image

feature

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

match

Calculates the distance between two face features.
face_feature1
InputArray
First input feature vector
face_feature2
InputArray
Second input feature vector of the same size and type as face_feature1
dis_type
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