Skip to content

Commit

Permalink
retinaface's nose and mouth field are returned in extract faces
Browse files Browse the repository at this point in the history
  • Loading branch information
serengil committed Oct 5, 2024
1 parent 6d1d6d3 commit 6b115eb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
7 changes: 6 additions & 1 deletion deepface/models/Detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ class FacialAreaRegion:
confidence (float, optional): Confidence score associated with the face detection.
Default is None.
"""

x: int
y: int
w: int
h: int
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
Expand All @@ -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
9 changes: 9 additions & 0 deletions deepface/models/face_detection/RetinaFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ def detect_faces(self, img: np.ndarray) -> List[FacialAreaRegion]:
# 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"]

Expand All @@ -57,6 +63,9 @@ def detect_faces(self, img: np.ndarray) -> List[FacialAreaRegion]:
left_eye=left_eye,
right_eye=right_eye,
confidence=confidence,
nose=nose,
mouth_left=mouth_left,
mouth_right=mouth_right,
)

resp.append(facial_area)
Expand Down
46 changes: 37 additions & 9 deletions deepface/modules/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
)
Expand Down

0 comments on commit 6b115eb

Please sign in to comment.