Skip to content

Commit

Permalink
Linting + optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
caption-pro-travis-ci committed Aug 26, 2024
1 parent cc8c3f0 commit f4d164e
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions deepface/modules/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from typing import Any, Dict, List, Tuple, Union, Optional

# 3rd part dependencies
from heapq import nlargest
import numpy as np
import cv2
from PIL import Image

# project dependencies
from deepface.modules import modeling
Expand Down Expand Up @@ -179,7 +179,9 @@ def extract_faces(


def detect_faces(
detector_backend: str, img: np.ndarray, align: bool = True, expand_percentage: int = 0, max_faces: Optional[int] = None
detector_backend: str, img: np.ndarray,
align: bool = True, expand_percentage: int = 0,
max_faces: Optional[int] = None
) -> List[DetectedFace]:
"""
Detect face(s) from a given image
Expand All @@ -205,7 +207,6 @@ def detect_faces(
- confidence (float): The confidence score associated with the detected face.
"""
height, width, _ = img.shape

face_detector: Detector = modeling.build_model(
task="face_detector", model_name=detector_backend
)
Expand Down Expand Up @@ -237,23 +238,28 @@ def detect_faces(
facial_areas = face_detector.detect_faces(img)

if max_faces is not None and max_faces < len(facial_areas):
# sort as largest facial areas come first
facial_areas = sorted(
facial_areas = nlargest(
max_faces,
facial_areas,
key=lambda facial_area: facial_area.w * facial_area.h,
reverse=True,
key=lambda facial_area: facial_area.w * facial_area.h
)
# discard rest of the items
facial_areas = facial_areas[0:max_faces]

results = []

for facial_area in facial_areas:
results.append(expand_and_align_face(facial_area=facial_area, img=img, align=align, expand_percentage=expand_percentage, width_border=width_border, height_border=height_border))

return results
return [
expand_and_align_face(
facial_area=facial_area,
img=img,
align=align,
expand_percentage=expand_percentage,
width_border=width_border,
height_border=height_border
)
for facial_area in facial_areas
]

def expand_and_align_face(facial_area: FacialAreaRegion, img: np.ndarray, align: bool, expand_percentage: int, width_border: int, height_border: int) -> DetectedFace:
def expand_and_align_face(
facial_area: FacialAreaRegion, img: np.ndarray,
align: bool, expand_percentage: int, width_border: int,
height_border: int) -> DetectedFace:
x = facial_area.x
y = facial_area.y
w = facial_area.w
Expand Down Expand Up @@ -294,7 +300,7 @@ def expand_and_align_face(facial_area: FacialAreaRegion, img: np.ndarray, align:
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)

return DetectedFace(
img=detected_face,
facial_area=FacialAreaRegion(
Expand Down Expand Up @@ -330,7 +336,11 @@ def align_img_wrt_eyes(
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, angle, 1.0)
img = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))
img = cv2.warpAffine(
img, M, (w, h),
flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT,
borderValue=(0,0,0)
)

return img, angle

Expand Down

0 comments on commit f4d164e

Please sign in to comment.