1// This file is part of OpenCV project.
2// It is subject to the license terms in the LICENSE file found in the top-level directory
3// of this distribution and at http://opencv.org/license.html.
4
5#ifndef OPENCV_OBJDETECT_FACE_HPP
6#define OPENCV_OBJDETECT_FACE_HPP
7
8#include <opencv2/core.hpp>
9
10namespace cv
11{
12
13//! @addtogroup objdetect_dnn_face
14//! @{
15
16/** @brief DNN-based face detector
17
18model download link: https://github.com/opencv/opencv_zoo/tree/master/models/face_detection_yunet
19 */
20class CV_EXPORTS_W FaceDetectorYN
21{
22public:
23 virtual ~FaceDetectorYN() {}
24
25 /** @brief Set the size for the network input, which overwrites the input size of creating model. Call this method when the size of input image does not match the input size when creating model
26 *
27 * @param input_size the size of the input image
28 */
29 CV_WRAP virtual void setInputSize(const Size& input_size) = 0;
30
31 CV_WRAP virtual Size getInputSize() = 0;
32
33 /** @brief Set the score threshold to filter out bounding boxes of score less than the given value
34 *
35 * @param score_threshold threshold for filtering out bounding boxes
36 */
37 CV_WRAP virtual void setScoreThreshold(float score_threshold) = 0;
38
39 CV_WRAP virtual float getScoreThreshold() = 0;
40
41 /** @brief Set the Non-maximum-suppression threshold to suppress bounding boxes that have IoU greater than the given value
42 *
43 * @param nms_threshold threshold for NMS operation
44 */
45 CV_WRAP virtual void setNMSThreshold(float nms_threshold) = 0;
46
47 CV_WRAP virtual float getNMSThreshold() = 0;
48
49 /** @brief Set the number of bounding boxes preserved before NMS
50 *
51 * @param top_k the number of bounding boxes to preserve from top rank based on score
52 */
53 CV_WRAP virtual void setTopK(int top_k) = 0;
54
55 CV_WRAP virtual int getTopK() = 0;
56
57 /** @brief Detects faces in the input image. Following is an example output.
58
59 * ![image](pics/lena-face-detection.jpg)
60
61 * @param image an image to detect
62 * @param faces detection results stored in a 2D cv::Mat of shape [num_faces, 15]
63 * - 0-1: x, y of bbox top left corner
64 * - 2-3: width, height of bbox
65 * - 4-5: x, y of right eye (blue point in the example image)
66 * - 6-7: x, y of left eye (red point in the example image)
67 * - 8-9: x, y of nose tip (green point in the example image)
68 * - 10-11: x, y of right corner of mouth (pink point in the example image)
69 * - 12-13: x, y of left corner of mouth (yellow point in the example image)
70 * - 14: face score
71 */
72 CV_WRAP virtual int detect(InputArray image, OutputArray faces) = 0;
73
74 /** @brief Creates an instance of face detector class with given parameters
75 *
76 * @param model the path to the requested model
77 * @param config the path to the config file for compability, which is not requested for ONNX models
78 * @param input_size the size of the input image
79 * @param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
80 * @param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
81 * @param top_k keep top K bboxes before NMS
82 * @param backend_id the id of backend
83 * @param target_id the id of target device
84 */
85 CV_WRAP static Ptr<FaceDetectorYN> create(CV_WRAP_FILE_PATH const String& model,
86 CV_WRAP_FILE_PATH const String& config,
87 const Size& input_size,
88 float score_threshold = 0.9f,
89 float nms_threshold = 0.3f,
90 int top_k = 5000,
91 int backend_id = 0,
92 int target_id = 0);
93
94 /** @overload
95 *
96 * @param framework Name of origin framework
97 * @param bufferModel A buffer with a content of binary file with weights
98 * @param bufferConfig A buffer with a content of text file contains network configuration
99 * @param input_size the size of the input image
100 * @param score_threshold the threshold to filter out bounding boxes of score smaller than the given value
101 * @param nms_threshold the threshold to suppress bounding boxes of IoU bigger than the given value
102 * @param top_k keep top K bboxes before NMS
103 * @param backend_id the id of backend
104 * @param target_id the id of target device
105 */
106 CV_WRAP static Ptr<FaceDetectorYN> create(const String& framework,
107 const std::vector<uchar>& bufferModel,
108 const std::vector<uchar>& bufferConfig,
109 const Size& input_size,
110 float score_threshold = 0.9f,
111 float nms_threshold = 0.3f,
112 int top_k = 5000,
113 int backend_id = 0,
114 int target_id = 0);
115
116};
117
118/** @brief DNN-based face recognizer
119
120model download link: https://github.com/opencv/opencv_zoo/tree/master/models/face_recognition_sface
121 */
122class CV_EXPORTS_W FaceRecognizerSF
123{
124public:
125 virtual ~FaceRecognizerSF() {}
126
127 /** @brief Definition of distance used for calculating the distance between two face features
128 */
129 enum DisType { FR_COSINE=0, FR_NORM_L2=1 };
130
131 /** @brief Aligns detected face with the source input image and crops it
132 * @param src_img input image
133 * @param face_box the detected face result from the input image
134 * @param aligned_img output aligned image
135 */
136 CV_WRAP virtual void alignCrop(InputArray src_img, InputArray face_box, OutputArray aligned_img) const = 0;
137
138 /** @brief Extracts face feature from aligned image
139 * @param aligned_img input aligned image
140 * @param face_feature output face feature
141 */
142 CV_WRAP virtual void feature(InputArray aligned_img, OutputArray face_feature) = 0;
143
144 /** @brief Calculates the distance between two face features
145 * @param face_feature1 the first input feature
146 * @param face_feature2 the second input feature of the same size and the same type as face_feature1
147 * @param dis_type defines how to calculate the distance between two face features with optional values "FR_COSINE" or "FR_NORM_L2"
148 */
149 CV_WRAP virtual double match(InputArray face_feature1, InputArray face_feature2, int dis_type = FaceRecognizerSF::FR_COSINE) const = 0;
150
151 /** @brief Creates an instance of this class with given parameters
152 * @param model the path of the onnx model used for face recognition
153 * @param config the path to the config file for compability, which is not requested for ONNX models
154 * @param backend_id the id of backend
155 * @param target_id the id of target device
156 */
157 CV_WRAP static Ptr<FaceRecognizerSF> create(CV_WRAP_FILE_PATH const String& model, CV_WRAP_FILE_PATH const String& config, int backend_id = 0, int target_id = 0);
158
159 /**
160 * @brief Creates an instance of this class from a buffer containing the model weights and configuration.
161 * @param framework Name of the framework (ONNX, etc.)
162 * @param bufferModel A buffer containing the binary model weights.
163 * @param bufferConfig A buffer containing the network configuration.
164 * @param backend_id The id of the backend.
165 * @param target_id The id of the target device.
166 *
167 * @return A pointer to the created instance of FaceRecognizerSF.
168 */
169 CV_WRAP static Ptr<FaceRecognizerSF> create(const String& framework,
170 const std::vector<uchar>& bufferModel,
171 const std::vector<uchar>& bufferConfig,
172 int backend_id = 0,
173 int target_id = 0);
174};
175
176//! @}
177} // namespace cv
178
179#endif
180

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of opencv/modules/objdetect/include/opencv2/objdetect/face.hpp