mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
Linting + optimizations
This commit is contained in:
parent
cc8c3f0461
commit
f4d164e0aa
@ -2,9 +2,9 @@
|
|||||||
from typing import Any, Dict, List, Tuple, Union, Optional
|
from typing import Any, Dict, List, Tuple, Union, Optional
|
||||||
|
|
||||||
# 3rd part dependencies
|
# 3rd part dependencies
|
||||||
|
from heapq import nlargest
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
# project dependencies
|
# project dependencies
|
||||||
from deepface.modules import modeling
|
from deepface.modules import modeling
|
||||||
@ -179,7 +179,9 @@ def extract_faces(
|
|||||||
|
|
||||||
|
|
||||||
def detect_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]:
|
) -> List[DetectedFace]:
|
||||||
"""
|
"""
|
||||||
Detect face(s) from a given image
|
Detect face(s) from a given image
|
||||||
@ -205,7 +207,6 @@ def detect_faces(
|
|||||||
- confidence (float): The confidence score associated with the detected face.
|
- confidence (float): The confidence score associated with the detected face.
|
||||||
"""
|
"""
|
||||||
height, width, _ = img.shape
|
height, width, _ = img.shape
|
||||||
|
|
||||||
face_detector: Detector = modeling.build_model(
|
face_detector: Detector = modeling.build_model(
|
||||||
task="face_detector", model_name=detector_backend
|
task="face_detector", model_name=detector_backend
|
||||||
)
|
)
|
||||||
@ -237,23 +238,28 @@ def detect_faces(
|
|||||||
facial_areas = face_detector.detect_faces(img)
|
facial_areas = face_detector.detect_faces(img)
|
||||||
|
|
||||||
if max_faces is not None and max_faces < len(facial_areas):
|
if max_faces is not None and max_faces < len(facial_areas):
|
||||||
# sort as largest facial areas come first
|
facial_areas = nlargest(
|
||||||
facial_areas = sorted(
|
max_faces,
|
||||||
facial_areas,
|
facial_areas,
|
||||||
key=lambda facial_area: facial_area.w * facial_area.h,
|
key=lambda facial_area: facial_area.w * facial_area.h
|
||||||
reverse=True,
|
|
||||||
)
|
)
|
||||||
# discard rest of the items
|
|
||||||
facial_areas = facial_areas[0:max_faces]
|
|
||||||
|
|
||||||
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
|
||||||
|
]
|
||||||
|
|
||||||
for facial_area in facial_areas:
|
def expand_and_align_face(
|
||||||
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))
|
facial_area: FacialAreaRegion, img: np.ndarray,
|
||||||
|
align: bool, expand_percentage: int, width_border: int,
|
||||||
return results
|
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
|
x = facial_area.x
|
||||||
y = facial_area.y
|
y = facial_area.y
|
||||||
w = facial_area.w
|
w = facial_area.w
|
||||||
@ -330,7 +336,11 @@ def align_img_wrt_eyes(
|
|||||||
(h, w) = img.shape[:2]
|
(h, w) = img.shape[:2]
|
||||||
center = (w // 2, h // 2)
|
center = (w // 2, h // 2)
|
||||||
M = cv2.getRotationMatrix2D(center, angle, 1.0)
|
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
|
return img, angle
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user