mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
Merge pull request #1319 from Circuit8/represent-optimizations
Represent optimizations
This commit is contained in:
commit
ed1b117016
1
.gitignore
vendored
1
.gitignore
vendored
@ -17,3 +17,4 @@ benchmarks/results
|
|||||||
benchmarks/outputs
|
benchmarks/outputs
|
||||||
benchmarks/dataset
|
benchmarks/dataset
|
||||||
benchmarks/lfwe
|
benchmarks/lfwe
|
||||||
|
venv
|
@ -1,15 +1,16 @@
|
|||||||
# built-in dependencies
|
# built-in dependencies
|
||||||
from typing import Any, Dict, List, Tuple, Union
|
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
|
||||||
from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion
|
from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion
|
||||||
from deepface.commons import image_utils
|
from deepface.commons import image_utils
|
||||||
|
|
||||||
from deepface.commons.logger import Logger
|
from deepface.commons.logger import Logger
|
||||||
|
|
||||||
logger = Logger()
|
logger = Logger()
|
||||||
@ -27,6 +28,7 @@ def extract_faces(
|
|||||||
color_face: str = "rgb",
|
color_face: str = "rgb",
|
||||||
normalize_face: bool = True,
|
normalize_face: bool = True,
|
||||||
anti_spoofing: bool = False,
|
anti_spoofing: bool = False,
|
||||||
|
max_faces: Optional[int] = None,
|
||||||
) -> List[Dict[str, Any]]:
|
) -> List[Dict[str, Any]]:
|
||||||
"""
|
"""
|
||||||
Extract faces from a given image
|
Extract faces from a given image
|
||||||
@ -97,6 +99,7 @@ def extract_faces(
|
|||||||
img=img,
|
img=img,
|
||||||
align=align,
|
align=align,
|
||||||
expand_percentage=expand_percentage,
|
expand_percentage=expand_percentage,
|
||||||
|
max_faces=max_faces,
|
||||||
)
|
)
|
||||||
|
|
||||||
# in case of no face found
|
# in case of no face found
|
||||||
@ -176,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
|
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
|
||||||
@ -202,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
|
||||||
)
|
)
|
||||||
@ -233,8 +237,29 @@ def detect_faces(
|
|||||||
# find facial areas of given image
|
# find facial areas of given image
|
||||||
facial_areas = face_detector.detect_faces(img)
|
facial_areas = face_detector.detect_faces(img)
|
||||||
|
|
||||||
results = []
|
if max_faces is not None and max_faces < len(facial_areas):
|
||||||
for facial_area in facial_areas:
|
facial_areas = nlargest(
|
||||||
|
max_faces,
|
||||||
|
facial_areas,
|
||||||
|
key=lambda facial_area: facial_area.w * facial_area.h
|
||||||
|
)
|
||||||
|
|
||||||
|
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:
|
||||||
x = facial_area.x
|
x = facial_area.x
|
||||||
y = facial_area.y
|
y = facial_area.y
|
||||||
w = facial_area.w
|
w = facial_area.w
|
||||||
@ -256,7 +281,6 @@ def detect_faces(
|
|||||||
|
|
||||||
# extract detected face unaligned
|
# extract detected face unaligned
|
||||||
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)]
|
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)]
|
||||||
|
|
||||||
# align original image, then find projection of detected face area after alignment
|
# align original image, then find projection of detected face area after alignment
|
||||||
if align is True: # and left_eye is not None and right_eye is not None:
|
if align is True: # and left_eye is not None and right_eye is not None:
|
||||||
aligned_img, angle = align_img_wrt_eyes(img=img, left_eye=left_eye, right_eye=right_eye)
|
aligned_img, angle = align_img_wrt_eyes(img=img, left_eye=left_eye, right_eye=right_eye)
|
||||||
@ -277,16 +301,13 @@ def detect_faces(
|
|||||||
if right_eye is not None:
|
if right_eye is not None:
|
||||||
right_eye = (right_eye[0] - width_border, right_eye[1] - height_border)
|
right_eye = (right_eye[0] - width_border, right_eye[1] - height_border)
|
||||||
|
|
||||||
result = DetectedFace(
|
return DetectedFace(
|
||||||
img=detected_face,
|
img=detected_face,
|
||||||
facial_area=FacialAreaRegion(
|
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
|
||||||
),
|
),
|
||||||
confidence=confidence,
|
confidence=confidence,
|
||||||
)
|
)
|
||||||
results.append(result)
|
|
||||||
return results
|
|
||||||
|
|
||||||
|
|
||||||
def align_img_wrt_eyes(
|
def align_img_wrt_eyes(
|
||||||
img: np.ndarray,
|
img: np.ndarray,
|
||||||
@ -311,7 +332,16 @@ def align_img_wrt_eyes(
|
|||||||
return img, 0
|
return img, 0
|
||||||
|
|
||||||
angle = float(np.degrees(np.arctan2(left_eye[1] - right_eye[1], left_eye[0] - right_eye[0])))
|
angle = float(np.degrees(np.arctan2(left_eye[1] - right_eye[1], left_eye[0] - right_eye[0])))
|
||||||
img = np.array(Image.fromarray(img).rotate(angle, resample=Image.BICUBIC))
|
|
||||||
|
(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)
|
||||||
|
)
|
||||||
|
|
||||||
return img, angle
|
return img, angle
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,6 +81,7 @@ def represent(
|
|||||||
align=align,
|
align=align,
|
||||||
expand_percentage=expand_percentage,
|
expand_percentage=expand_percentage,
|
||||||
anti_spoofing=anti_spoofing,
|
anti_spoofing=anti_spoofing,
|
||||||
|
max_faces=max_faces,
|
||||||
)
|
)
|
||||||
else: # skip
|
else: # skip
|
||||||
# Try load. If load error, will raise exception internal
|
# Try load. If load error, will raise exception internal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user