2
2
import os
3
3
import warnings
4
4
import logging
5
- from typing import Any , Dict , List , Tuple , Union
5
+ from typing import Any , Dict , List , Tuple , Union , Optional
6
6
7
7
# 3rd party dependencies
8
8
import numpy as np
@@ -65,34 +65,49 @@ def verify(
65
65
Args:
66
66
img1_path (str or np.ndarray): Path to the first image. Accepts exact image path
67
67
as a string, numpy array (BGR), or base64 encoded images.
68
+
68
69
img2_path (str or np.ndarray): Path to the second image. Accepts exact image path
69
70
as a string, numpy array (BGR), or base64 encoded images.
71
+
70
72
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
71
73
OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face).
74
+
72
75
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
73
- 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv)
76
+ 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
77
+
74
78
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
75
79
'euclidean', 'euclidean_l2' (default is cosine).
80
+
76
81
enforce_detection (boolean): If no face is detected in an image, raise an exception.
77
82
Set to False to avoid the exception for low-resolution images (default is True).
83
+
78
84
align (bool): Flag to enable face alignment (default is True).
85
+
79
86
normalization (string): Normalize the input image before feeding it to the model.
80
87
Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace (default is base)
88
+
81
89
Returns:
82
90
result (dict): A dictionary containing verification results with following keys.
91
+
83
92
- 'verified' (bool): Indicates whether the images represent the same person (True)
84
93
or different persons (False).
94
+
85
95
- 'distance' (float): The distance measure between the face vectors.
86
96
A lower distance indicates higher similarity.
97
+
87
98
- 'max_threshold_to_verify' (float): The maximum threshold used for verification.
88
99
If the distance is below this threshold, the images are considered a match.
100
+
89
101
- 'model' (str): The chosen face recognition model.
102
+
90
103
- 'similarity_metric' (str): The chosen similarity metric for measuring distances.
104
+
91
105
- 'facial_areas' (dict): Rectangular regions of interest for faces in both images.
92
106
- 'img1': {'x': int, 'y': int, 'w': int, 'h': int}
93
107
Region of interest for the first image.
94
108
- 'img2': {'x': int, 'y': int, 'w': int, 'h': int}
95
109
Region of interest for the second image.
110
+
96
111
- 'time' (float): Time taken for the verification process in seconds.
97
112
"""
98
113
@@ -122,37 +137,51 @@ def analyze(
122
137
img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
123
138
or a base64 encoded image. If the source image contains multiple faces, the result will
124
139
include information for each detected face.
140
+
125
141
actions (tuple): Attributes to analyze. The default is ('age', 'gender', 'emotion', 'race').
126
142
You can exclude some of these attributes from the analysis if needed.
143
+
127
144
enforce_detection (boolean): If no face is detected in an image, raise an exception.
128
145
Set to False to avoid the exception for low-resolution images (default is True).
146
+
129
147
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
130
148
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
149
+
131
150
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
132
151
'euclidean', 'euclidean_l2' (default is cosine).
152
+
133
153
align (boolean): Perform alignment based on the eye positions (default is True).
154
+
134
155
silent (boolean): Suppress or allow some log messages for a quieter analysis process
135
156
(default is False).
157
+
136
158
Returns:
137
159
results (List[Dict[str, Any]]): A list of dictionaries, where each dictionary represents
138
160
the analysis results for a detected face. Each dictionary in the list contains the
139
161
following keys:
162
+
140
163
- 'region' (dict): Represents the rectangular region of the detected face in the image.
141
164
- 'x': x-coordinate of the top-left corner of the face.
142
165
- 'y': y-coordinate of the top-left corner of the face.
143
166
- 'w': Width of the detected face region.
144
167
- 'h': Height of the detected face region.
168
+
145
169
- 'age' (float): Estimated age of the detected face.
170
+
146
171
- 'face_confidence' (float): Confidence score for the detected face.
147
172
Indicates the reliability of the face detection.
173
+
148
174
- 'dominant_gender' (str): The dominant gender in the detected face.
149
- Either "Man" or "Woman."
175
+ Either "Man" or "Woman".
176
+
150
177
- 'gender' (dict): Confidence scores for each gender category.
151
178
- 'Man': Confidence score for the male gender.
152
179
- 'Woman': Confidence score for the female gender.
180
+
153
181
- 'dominant_emotion' (str): The dominant emotion in the detected face.
154
182
Possible values include "sad," "angry," "surprise," "fear," "happy,"
155
- "disgust," and "neutral."
183
+ "disgust," and "neutral"
184
+
156
185
- 'emotion' (dict): Confidence scores for each emotion category.
157
186
- 'sad': Confidence score for sadness.
158
187
- 'angry': Confidence score for anger.
@@ -161,9 +190,11 @@ def analyze(
161
190
- 'happy': Confidence score for happiness.
162
191
- 'disgust': Confidence score for disgust.
163
192
- 'neutral': Confidence score for neutrality.
193
+
164
194
- 'dominant_race' (str): The dominant race in the detected face.
165
195
Possible values include "indian," "asian," "latino hispanic,"
166
196
"black," "middle eastern," and "white."
197
+
167
198
- 'race' (dict): Confidence scores for each race category.
168
199
- 'indian': Confidence score for Indian ethnicity.
169
200
- 'asian': Confidence score for Asian ethnicity.
@@ -190,6 +221,7 @@ def find(
190
221
enforce_detection : bool = True ,
191
222
detector_backend : str = "opencv" ,
192
223
align : bool = True ,
224
+ threshold : Optional [float ] = None ,
193
225
normalization : str = "base" ,
194
226
silent : bool = False ,
195
227
) -> List [pd .DataFrame ]:
@@ -199,31 +231,51 @@ def find(
199
231
img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
200
232
or a base64 encoded image. If the source image contains multiple faces, the result will
201
233
include information for each detected face.
234
+
202
235
db_path (string): Path to the folder containing image files. All detected faces
203
236
in the database will be considered in the decision-making process.
237
+
204
238
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
205
239
OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face).
240
+
206
241
distance_metric (string): Metric for measuring similarity. Options: 'cosine',
207
242
'euclidean', 'euclidean_l2' (default is cosine).
243
+
208
244
enforce_detection (boolean): If no face is detected in an image, raise an exception.
209
245
Set to False to avoid the exception for low-resolution images (default is True).
246
+
210
247
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
211
248
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
249
+
212
250
align (boolean): Perform alignment based on the eye positions (default is True).
251
+
252
+ threshold (float): Specify a threshold to determine whether a pair represents the same
253
+ person or different individuals. This threshold is used for comparing distances.
254
+ If left unset, default pre-tuned threshold values will be applied based on the specified
255
+ model name and distance metric (default is None).
256
+
213
257
normalization (string): Normalize the input image before feeding it to the model.
214
258
Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace (default is base).
259
+
215
260
silent (boolean): Suppress or allow some log messages for a quieter analysis process
216
261
(default is False).
262
+
217
263
Returns:
218
264
results (List[pd.DataFrame]): A list of pandas dataframes. Each dataframe corresponds
219
265
to the identity information for an individual detected in the source image.
220
266
The DataFrame columns include:
267
+
221
268
- 'identity': Identity label of the detected individual.
269
+
222
270
- 'target_x', 'target_y', 'target_w', 'target_h': Bounding box coordinates of the
223
271
target face in the database.
272
+
224
273
- 'source_x', 'source_y', 'source_w', 'source_h': Bounding box coordinates of the
225
274
detected face in the source image.
226
- - '{model_name}_{distance_metric}': Similarity score between the faces based on the
275
+
276
+ - 'threshold': threshold to determine a pair whether same person or different persons
277
+
278
+ - 'distance': Similarity score between the faces based on the
227
279
specified model and distance metric
228
280
"""
229
281
return recognition .find (
@@ -234,6 +286,7 @@ def find(
234
286
enforce_detection = enforce_detection ,
235
287
detector_backend = detector_backend ,
236
288
align = align ,
289
+ threshold = threshold ,
237
290
normalization = normalization ,
238
291
silent = silent ,
239
292
)
@@ -254,27 +307,36 @@ def represent(
254
307
img_path (str or np.ndarray): The exact path to the image, a numpy array in BGR format,
255
308
or a base64 encoded image. If the source image contains multiple faces, the result will
256
309
include information for each detected face.
310
+
257
311
model_name (str): Model for face recognition. Options: VGG-Face, Facenet, Facenet512,
258
312
OpenFace, DeepFace, DeepID, Dlib, ArcFace and SFace (default is VGG-Face.).
313
+
259
314
enforce_detection (boolean): If no face is detected in an image, raise an exception.
260
315
Default is True. Set to False to avoid the exception for low-resolution images
261
316
(default is True).
317
+
262
318
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
263
319
'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
320
+
264
321
align (boolean): Perform alignment based on the eye positions (default is True).
322
+
265
323
normalization (string): Normalize the input image before feeding it to the model.
266
324
Default is base. Options: base, raw, Facenet, Facenet2018, VGGFace, VGGFace2, ArcFace
267
325
(default is base).
326
+
268
327
Returns:
269
328
results (List[Dict[str, Any]]): A list of dictionaries, each containing the
270
329
following fields:
330
+
271
331
- embedding (np.array): Multidimensional vector representing facial features.
272
332
The number of dimensions varies based on the reference model
273
333
(e.g., FaceNet returns 128 dimensions, VGG-Face returns 4096 dimensions).
334
+
274
335
- facial_area (dict): Detected facial area by face detection in dictionary format.
275
336
Contains 'x' and 'y' as the left-corner point, and 'w' and 'h'
276
337
as the width and height. If `detector_backend` is set to 'skip', it represents
277
338
the full image area and is nonsensical.
339
+
278
340
- face_confidence (float): Confidence score of face detection. If `detector_backend` is set
279
341
to 'skip', the confidence will be 0 and is nonsensical.
280
342
"""
@@ -355,19 +417,28 @@ def extract_faces(
355
417
Args:
356
418
img_path (str or np.ndarray): Path to the first image. Accepts exact image path
357
419
as a string, numpy array (BGR), or base64 encoded images.
420
+
358
421
target_size (tuple): final shape of facial image. black pixels will be
359
422
added to resize the image (default is (224, 224)).
423
+
360
424
detector_backend (string): face detector backend. Options: 'opencv', 'retinaface',
361
- 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv)
425
+ 'mtcnn', 'ssd', 'dlib', 'mediapipe', 'yolov8' (default is opencv).
426
+
362
427
enforce_detection (boolean): If no face is detected in an image, raise an exception.
363
428
Set to False to avoid the exception for low-resolution images (default is True).
429
+
364
430
align (bool): Flag to enable face alignment (default is True).
431
+
365
432
grayscale (boolean): Flag to convert the image to grayscale before
366
433
processing (default is False).
434
+
367
435
Returns:
368
436
results (List[Dict[str, Any]]): A list of dictionaries, where each dictionary contains:
437
+
369
438
- "face" (np.ndarray): The detected face as a NumPy array.
439
+
370
440
- "facial_area" (List[float]): The detected face's regions represented as a list of floats.
441
+
371
442
- "confidence" (float): The confidence score associated with the detected face.
372
443
"""
373
444
0 commit comments