@@ -45,7 +45,7 @@ def build_model(model_name: str) -> Any:
45
45
VGG-Face, Facenet, OpenFace, DeepFace, DeepID for face recognition
46
46
Age, Gender, Emotion, Race for facial attributes
47
47
Returns:
48
- built model with corresponding class
48
+ built_model
49
49
"""
50
50
return modeling .build_model (model_name = model_name )
51
51
@@ -62,57 +62,37 @@ def verify(
62
62
) -> Dict [str , Any ]:
63
63
"""
64
64
Verify if an image pair represents the same person or different persons.
65
-
66
- The verification function converts facial images to vectors and calculates the similarity
67
- between those vectors. Vectors of images of the same person should exhibit higher similarity
68
- (or lower distance) than vectors of images of different persons.
69
-
70
65
Args:
71
66
img1_path (str or np.ndarray): Path to the first image. Accepts exact image path
72
67
as a string, numpy array (BGR), or base64 encoded images.
73
-
74
68
img2_path (str or np.ndarray): Path to the second image. Accepts exact image path
75
69
as a string, numpy array (BGR), or base64 encoded images.
76
-
77
70
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
78
71
OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face).
79
-
80
72
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
81
73
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv)
82
-
83
74
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
84
75
'euclidean', 'euclidean_l2' (default is cosine).
85
-
86
76
enforce_detection (boolean): If no face is detected in an image, raise an exception.
87
77
Set to False to avoid the exception for low-resolution images (default is True).
88
-
89
78
align (bool): Flag to enable face alignment (default is True).
90
-
91
79
normalization (string): Normalize the input image before feeding it to the model.
92
80
Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace (default is base)
93
-
94
81
Returns:
95
- result (dict): A dictionary containing verification results.
96
-
82
+ result (dict): A dictionary containing verification results with following keys.
97
83
- 'verified' (bool): Indicates whether the images represent the same person (True)
98
84
or different persons (False).
99
-
100
85
- 'distance' (float): The distance measure between the face vectors.
101
86
A lower distance indicates higher similarity.
102
-
103
87
- 'max_threshold_to_verify' (float): The maximum threshold used for verification.
104
88
If the distance is below this threshold, the images are considered a match.
105
-
106
89
- 'model' (str): The chosen face recognition model.
107
-
108
90
- 'similarity_metric' (str): The chosen similarity metric for measuring distances.
109
-
110
91
- 'facial_areas' (dict): Rectangular regions of interest for faces in both images.
111
92
- 'img1': {'x': int, 'y': int, 'w': int, 'h': int}
112
93
Region of interest for the first image.
113
94
- 'img2': {'x': int, 'y': int, 'w': int, 'h': int}
114
95
Region of interest for the second image.
115
-
116
96
- 'time' (float): Time taken for the verification process in seconds.
117
97
"""
118
98
@@ -138,77 +118,59 @@ def analyze(
138
118
) -> List [Dict [str , Any ]]:
139
119
"""
140
120
Analyze facial attributes such as age, gender, emotion, and race in the provided image.
141
-
142
121
Args:
143
122
img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
144
123
or a base64 encoded image. If the source image contains multiple faces, the result will
145
124
include information for each detected face.
146
-
147
125
actions (tuple): Attributes to analyze. The default is ('age', 'gender', 'emotion', 'race').
148
126
You can exclude some of these attributes from the analysis if needed.
149
-
150
127
enforce_detection (boolean): If no face is detected in an image, raise an exception.
151
128
Set to False to avoid the exception for low-resolution images (default is True).
152
-
153
129
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
154
130
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
155
-
156
131
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
157
132
'euclidean', 'euclidean_l2' (default is cosine).
158
-
159
133
align (boolean): Perform alignment based on the eye positions (default is True).
160
-
161
134
silent (boolean): Suppress or allow some log messages for a quieter analysis process
162
135
(default is False).
163
-
164
136
Returns:
165
137
results (List[Dict[str, Any]]): A list of dictionaries, where each dictionary represents
166
- the analysis results for a detected face.
167
-
168
- Each dictionary in the list contains the following keys:
169
-
170
- - 'region' (dict): Represents the rectangular region of the detected face in the image.
171
- - 'x': x-coordinate of the top-left corner of the face.
172
- - 'y': y-coordinate of the top-left corner of the face.
173
- - 'w': Width of the detected face region.
174
- - 'h': Height of the detected face region.
175
-
176
- - 'age' (float): Estimated age of the detected face.
177
-
178
- - 'face_confidence' (float): Confidence score for the detected face.
179
- Indicates the reliability of the face detection.
180
-
181
- - 'dominant_gender' (str): The dominant gender in the detected face.
182
- Either "Man" or "Woman."
183
-
184
- - 'gender' (dict): Confidence scores for each gender category.
185
- - 'Man': Confidence score for the male gender.
186
- - 'Woman': Confidence score for the female gender.
187
-
188
- - 'dominant_emotion' (str): The dominant emotion in the detected face.
189
- Possible values include "sad," "angry," "surprise," "fear," "happy,"
190
- "disgust," and "neutral."
191
-
192
- - 'emotion' (dict): Confidence scores for each emotion category.
193
- - 'sad': Confidence score for sadness.
194
- - 'angry': Confidence score for anger.
195
- - 'surprise': Confidence score for surprise.
196
- - 'fear': Confidence score for fear.
197
- - 'happy': Confidence score for happiness.
198
- - 'disgust': Confidence score for disgust.
199
- - 'neutral': Confidence score for neutrality.
200
-
201
- - 'dominant_race' (str): The dominant race in the detected face.
202
- Possible values include "indian," "asian," "latino hispanic,"
203
- "black," "middle eastern," and "white."
204
-
205
- - 'race' (dict): Confidence scores for each race category.
206
- - 'indian': Confidence score for Indian ethnicity.
207
- - 'asian': Confidence score for Asian ethnicity.
208
- - 'latino hispanic': Confidence score for Latino/Hispanic ethnicity.
209
- - 'black': Confidence score for Black ethnicity.
210
- - 'middle eastern': Confidence score for Middle Eastern ethnicity.
211
- - 'white': Confidence score for White ethnicity.
138
+ the analysis results for a detected face. Each dictionary in the list contains the
139
+ following keys:
140
+ - 'region' (dict): Represents the rectangular region of the detected face in the image.
141
+ - 'x': x-coordinate of the top-left corner of the face.
142
+ - 'y': y-coordinate of the top-left corner of the face.
143
+ - 'w': Width of the detected face region.
144
+ - 'h': Height of the detected face region.
145
+ - 'age' (float): Estimated age of the detected face.
146
+ - 'face_confidence' (float): Confidence score for the detected face.
147
+ Indicates the reliability of the face detection.
148
+ - 'dominant_gender' (str): The dominant gender in the detected face.
149
+ Either "Man" or "Woman."
150
+ - 'gender' (dict): Confidence scores for each gender category.
151
+ - 'Man': Confidence score for the male gender.
152
+ - 'Woman': Confidence score for the female gender.
153
+ - 'dominant_emotion' (str): The dominant emotion in the detected face.
154
+ Possible values include "sad," "angry," "surprise," "fear," "happy,"
155
+ "disgust," and "neutral."
156
+ - 'emotion' (dict): Confidence scores for each emotion category.
157
+ - 'sad': Confidence score for sadness.
158
+ - 'angry': Confidence score for anger.
159
+ - 'surprise': Confidence score for surprise.
160
+ - 'fear': Confidence score for fear.
161
+ - 'happy': Confidence score for happiness.
162
+ - 'disgust': Confidence score for disgust.
163
+ - 'neutral': Confidence score for neutrality.
164
+ - 'dominant_race' (str): The dominant race in the detected face.
165
+ Possible values include "indian," "asian," "latino hispanic,"
166
+ "black," "middle eastern," and "white."
167
+ - 'race' (dict): Confidence scores for each race category.
168
+ - 'indian': Confidence score for Indian ethnicity.
169
+ - 'asian': Confidence score for Asian ethnicity.
170
+ - 'latino hispanic': Confidence score for Latino/Hispanic ethnicity.
171
+ - 'black': Confidence score for Black ethnicity.
172
+ - 'middle eastern': Confidence score for Middle Eastern ethnicity.
173
+ - 'white': Confidence score for White ethnicity.
212
174
"""
213
175
return demography .analyze (
214
176
img_path = img_path ,
@@ -233,46 +195,36 @@ def find(
233
195
) -> List [pd .DataFrame ]:
234
196
"""
235
197
Identify individuals in a database
236
-
237
198
Args:
238
199
img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
239
200
or a base64 encoded image. If the source image contains multiple faces, the result will
240
201
include information for each detected face.
241
-
242
202
db_path (string): Path to the folder containing image files. All detected faces
243
203
in the database will be considered in the decision-making process.
244
-
245
204
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
246
- OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace
247
-
205
+ OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face).
248
206
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
249
- 'euclidean', 'euclidean_l2'.
250
-
207
+ 'euclidean', 'euclidean_l2' (default is cosine).
251
208
enforce_detection (boolean): If no face is detected in an image, raise an exception.
252
- Default is True. Set to False to avoid the exception for low-resolution images.
253
-
209
+ Set to False to avoid the exception for low-resolution images (default is True).
254
210
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
255
- 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8'.
256
-
257
- align (boolean): Perform alignment based on the eye positions.
258
-
211
+ 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
212
+ align (boolean): Perform alignment based on the eye positions (default is True).
259
213
normalization (string): Normalize the input image before feeding it to the model.
260
- Default is base. Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace
261
-
262
- silent (boolean): Suppress or allow some log messages for a quieter analysis process.
263
-
214
+ Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace (default is base).
215
+ silent (boolean): Suppress or allow some log messages for a quieter analysis process
216
+ (default is False).
264
217
Returns:
265
218
results (List[pd.DataFrame]): A list of pandas dataframes. Each dataframe corresponds
266
219
to the identity information for an individual detected in the source image.
267
220
The DataFrame columns include:
268
-
269
- - 'identity': Identity label of the detected individual.
270
- - 'target_x', 'target_y', 'target_w', 'target_h': Bounding box coordinates of the
271
- target face in the database.
272
- - 'source_x', 'source_y', 'source_w', 'source_h': Bounding box coordinates of the
273
- detected face in the source image.
274
- - '{model_name}_{distance_metric}': Similarity score between the faces based on the
275
- specified model and distance metric
221
+ - 'identity': Identity label of the detected individual.
222
+ - 'target_x', 'target_y', 'target_w', 'target_h': Bounding box coordinates of the
223
+ target face in the database.
224
+ - 'source_x', 'source_y', 'source_w', 'source_h': Bounding box coordinates of the
225
+ detected face in the source image.
226
+ - '{model_name}_{distance_metric}': Similarity score between the faces based on the
227
+ specified model and distance metric
276
228
"""
277
229
return recognition .find (
278
230
img_path = img_path ,
@@ -302,25 +254,20 @@ def represent(
302
254
img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
303
255
or a base64 encoded image. If the source image contains multiple faces, the result will
304
256
include information for each detected face.
305
-
306
257
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
307
- OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace
308
-
258
+ OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face.).
309
259
enforce_detection (boolean): If no face is detected in an image, raise an exception.
310
- Default is True. Set to False to avoid the exception for low-resolution images.
311
-
260
+ Default is True. Set to False to avoid the exception for low-resolution images
261
+ (default is True).
312
262
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
313
- 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8'.
314
-
315
- align (boolean): Perform alignment based on the eye positions.
316
-
263
+ 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
264
+ align (boolean): Perform alignment based on the eye positions (default is True).
317
265
normalization (string): Normalize the input image before feeding it to the model.
318
266
Default is base. Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace
319
-
267
+ (default is base).
320
268
Returns:
321
269
results (List[Dict[str, Any]]): A list of dictionaries, each containing the
322
270
following fields:
323
-
324
271
- embedding (np.array): Multidimensional vector representing facial features.
325
272
The number of dimensions varies based on the reference model
326
273
(e.g., FaceNet returns 128 dimensions, VGG-Face returns 4096 dimensions).
@@ -359,13 +306,13 @@ def stream(
359
306
in the database will be considered in the decision-making process.
360
307
361
308
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
362
- OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace
309
+ OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face).
363
310
364
311
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
365
- 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8'.
312
+ 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv) .
366
313
367
314
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
368
- 'euclidean', 'euclidean_l2'.
315
+ 'euclidean', 'euclidean_l2' (default is cosine) .
369
316
370
317
enable_face_analysis (bool): Flag to enable face analysis (default is True).
371
318
@@ -408,22 +355,15 @@ def extract_faces(
408
355
Args:
409
356
img_path (str or np.ndarray): Path to the first image. Accepts exact image path
410
357
as a string, numpy array (BGR), or base64 encoded images.
411
-
412
358
target_size (tuple): final shape of facial image. black pixels will be
413
- added to resize the image.
414
-
359
+ added to resize the image (default is (224, 224)).
415
360
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
416
361
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv)
417
-
418
362
enforce_detection (boolean): If no face is detected in an image, raise an exception.
419
- Default is True. Set to False to avoid the exception for low-resolution images.
420
-
363
+ Set to False to avoid the exception for low-resolution images (default is True).
421
364
align (bool): Flag to enable face alignment (default is True).
422
-
423
365
grayscale (boolean): Flag to convert the image to grayscale before
424
366
processing (default is False).
425
-
426
-
427
367
Returns:
428
368
results (List[Dict[str, Any]]): A list of dictionaries, where each dictionary contains:
429
369
- "face" (np.ndarray): The detected face as a NumPy array.
0 commit comments