diff --git a/deepface/models/Detector.py b/deepface/models/Detector.py index be1130f..48f235c 100644 --- a/deepface/models/Detector.py +++ b/deepface/models/Detector.py @@ -45,6 +45,7 @@ class FacialAreaRegion: confidence (float, optional): Confidence score associated with the face detection. Default is None. """ + x: int y: int w: int @@ -52,6 +53,9 @@ class FacialAreaRegion: left_eye: Optional[Tuple[int, int]] = None right_eye: Optional[Tuple[int, int]] = None confidence: Optional[float] = None + nose: Optional[Tuple[int, int]] = None + mouth_right: Optional[Tuple[int, int]] = None + mouth_left: Optional[Tuple[int, int]] = None @dataclass @@ -63,7 +67,8 @@ class DetectedFace: img (np.ndarray): detected face image as numpy array facial_area (FacialAreaRegion): detected face's metadata (e.g. bounding box) confidence (float): confidence score for face detection - """ + """ + img: np.ndarray facial_area: FacialAreaRegion confidence: float diff --git a/deepface/models/face_detection/RetinaFace.py b/deepface/models/face_detection/RetinaFace.py index a3b1468..d3b81e7 100644 --- a/deepface/models/face_detection/RetinaFace.py +++ b/deepface/models/face_detection/RetinaFace.py @@ -42,10 +42,16 @@ class RetinaFaceClient(Detector): # retinaface sets left and right eyes with respect to the person left_eye = identity["landmarks"]["left_eye"] right_eye = identity["landmarks"]["right_eye"] + nose = identity["landmarks"]["nose"] + mouth_right = identity["landmarks"]["mouth_right"] + mouth_left = identity["landmarks"]["mouth_left"] # eyes are list of float, need to cast them tuple of int left_eye = tuple(int(i) for i in left_eye) right_eye = tuple(int(i) for i in right_eye) + nose = tuple(int(i) for i in nose) + mouth_right = tuple(int(i) for i in mouth_right) + mouth_left = tuple(int(i) for i in mouth_left) confidence = identity["score"] @@ -57,6 +63,9 @@ class RetinaFaceClient(Detector): left_eye=left_eye, right_eye=right_eye, confidence=confidence, + nose=nose, + mouth_left=mouth_left, + mouth_right=mouth_right, ) resp.append(facial_area) diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index 46165a9..6ab3a87 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -148,16 +148,26 @@ def extract_faces( w = min(width - x - 1, int(current_region.w)) h = min(height - y - 1, int(current_region.h)) + facial_area = { + "x": x, + "y": y, + "w": w, + "h": h, + "left_eye": current_region.left_eye, + "right_eye": current_region.right_eye, + } + + # optional nose, mouth_left and mouth_right fields are coming just for retinaface + if current_region.nose: + facial_area["nose"] = current_region.nose + if current_region.mouth_left: + facial_area["mouth_left"] = current_region.mouth_left + if current_region.mouth_right: + facial_area["mouth_right"] = current_region.mouth_right + resp_obj = { "face": current_img, - "facial_area": { - "x": x, - "y": y, - "w": w, - "h": h, - "left_eye": current_region.left_eye, - "right_eye": current_region.right_eye, - }, + "facial_area": facial_area, "confidence": round(float(current_region.confidence or 0), 2), } @@ -272,6 +282,9 @@ def expand_and_align_face( left_eye = facial_area.left_eye right_eye = facial_area.right_eye confidence = facial_area.confidence + nose = facial_area.nose + mouth_left = facial_area.mouth_left + mouth_right = facial_area.mouth_right if expand_percentage > 0: # Expand the facial region height and width by the provided percentage @@ -305,11 +318,26 @@ def expand_and_align_face( left_eye = (left_eye[0] - width_border, left_eye[1] - height_border) if right_eye is not None: right_eye = (right_eye[0] - width_border, right_eye[1] - height_border) + if nose is not None: + nose = (nose[0] - width_border, nose[1] - height_border) + if mouth_left is not None: + mouth_left = (mouth_left[0] - width_border, mouth_left[1] - height_border) + if mouth_right is not None: + mouth_right = (mouth_right[0] - width_border, mouth_right[1] - height_border) return DetectedFace( img=detected_face, facial_area=FacialAreaRegion( - x=x, y=y, h=h, w=w, confidence=confidence, left_eye=left_eye, right_eye=right_eye + x=x, + y=y, + h=h, + w=w, + confidence=confidence, + left_eye=left_eye, + right_eye=right_eye, + nose=nose, + mouth_left=mouth_left, + mouth_right=mouth_right, ), confidence=confidence, )