1
- from typing import Any , Union , List , Tuple
1
+ from typing import Any , Union , List
2
2
import cv2
3
3
import numpy as np
4
- from deepface .models .Detector import Detector
4
+ from deepface .models .Detector import Detector , DetectedFace , FacialAreaRegion
5
5
from deepface .modules import detection
6
6
7
7
# Link -> https://github.com/timesler/facenet-pytorch
@@ -12,33 +12,22 @@ class FastMtCnnClient(Detector):
12
12
def __init__ (self ):
13
13
self .model = self .build_model ()
14
14
15
- def detect_faces (
16
- self , img : np .ndarray , align : bool = True
17
- ) -> List [Tuple [np .ndarray , List [float ], float ]]:
15
+ def detect_faces (self , img : np .ndarray , align : bool = True ) -> List [DetectedFace ]:
18
16
"""
19
17
Detect and align face with mtcnn
20
18
Args:
21
19
img (np.ndarray): pre-loaded image
22
20
align (bool): default is true
23
21
Returns:
24
- results (List[Tuple[np.ndarray, List[float], float]]): A list of tuples
25
- where each tuple contains:
26
- - detected_face (np.ndarray): The detected face as a NumPy array.
27
- - face_region (List[float]): The image region represented as
28
- a list of floats e.g. [x, y, w, h]
29
- - confidence (float): The confidence score associated with the detected face.
30
-
31
- Example:
32
- results = [
33
- (array(..., dtype=uint8), [110, 60, 150, 380], 0.99),
34
- (array(..., dtype=uint8), [150, 50, 299, 375], 0.98),
35
- (array(..., dtype=uint8), [120, 55, 300, 371], 0.96),
36
- ]
22
+ results (List[DetectedFace]): A list of DetectedFace objects
23
+ where each object contains:
24
+ - img (np.ndarray): The detected face as a NumPy array.
25
+ - facial_area (FacialAreaRegion): The facial area region represented as x, y, w, h
26
+ - confidence (float): The confidence score associated with the detected face.
37
27
"""
38
28
resp = []
39
29
40
30
detected_face = None
41
- img_region = [0 , 0 , img .shape [1 ], img .shape [0 ]]
42
31
43
32
img_rgb = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB ) # mtcnn expects RGB but OpenCV read BGR
44
33
detections = self .model .detect (
@@ -49,7 +38,7 @@ def detect_faces(
49
38
for current_detection in zip (* detections ):
50
39
x , y , w , h = xyxy_to_xywh (current_detection [0 ])
51
40
detected_face = img [int (y ) : int (y + h ), int (x ) : int (x + w )]
52
- img_region = [ x , y , w , h ]
41
+ img_region = FacialAreaRegion ( x = x , y = y , w = w , h = h )
53
42
confidence = current_detection [1 ]
54
43
55
44
if align :
@@ -59,7 +48,11 @@ def detect_faces(
59
48
img = detected_face , left_eye = left_eye , right_eye = right_eye
60
49
)
61
50
62
- resp .append ((detected_face , img_region , confidence ))
51
+ detected_face_obj = DetectedFace (
52
+ img = detected_face , facial_area = img_region , confidence = confidence
53
+ )
54
+
55
+ resp .append (detected_face_obj )
63
56
64
57
return resp
65
58
0 commit comments