From 6bdf83b86fb4d6a204ec293c42bf630d6518675d Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 22 Aug 2024 20:13:40 +0100 Subject: [PATCH 1/5] Added max_faces --- deepface/modules/detection.py | 22 +++++++++++++++++++--- deepface/modules/representation.py | 1 + 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index 6b5ab2e..8b4d056 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -1,5 +1,5 @@ # built-in dependencies -from typing import Any, Dict, List, Tuple, Union +from typing import Any, Dict, List, Tuple, Union, Optional # 3rd part dependencies import numpy as np @@ -10,7 +10,9 @@ from PIL import Image from deepface.modules import modeling from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion from deepface.commons import image_utils + from deepface.commons.logger import Logger +import time logger = Logger() @@ -27,6 +29,7 @@ def extract_faces( color_face: str = "rgb", normalize_face: bool = True, anti_spoofing: bool = False, + max_faces: Optional[int] = None, ) -> List[Dict[str, Any]]: """ Extract faces from a given image @@ -97,6 +100,7 @@ def extract_faces( img=img, align=align, expand_percentage=expand_percentage, + max_faces=max_faces, ) # in case of no face found @@ -176,7 +180,7 @@ def extract_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]: """ Detect face(s) from a given image @@ -202,7 +206,7 @@ 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 ) @@ -233,6 +237,17 @@ def detect_faces( # find facial areas of given image 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, + key=lambda facial_area: facial_area.w * facial_area.h, + reverse=True, + ) + # discard rest of the items + facial_areas = facial_areas[0:max_faces] + + start_time = time.time() results = [] for facial_area in facial_areas: x = facial_area.x @@ -285,6 +300,7 @@ def detect_faces( confidence=confidence, ) results.append(result) + return results diff --git a/deepface/modules/representation.py b/deepface/modules/representation.py index b187ce4..a147640 100644 --- a/deepface/modules/representation.py +++ b/deepface/modules/representation.py @@ -81,6 +81,7 @@ def represent( align=align, expand_percentage=expand_percentage, anti_spoofing=anti_spoofing, + max_faces=max_faces, ) else: # skip # Try load. If load error, will raise exception internal From d478f41c479355aacbd987e733e557f9f326064d Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 23 Aug 2024 14:03:06 +0100 Subject: [PATCH 2/5] gitignore venv --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b8359b9..5dedb26 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ tests/*.csv benchmarks/results benchmarks/outputs benchmarks/dataset -benchmarks/lfwe \ No newline at end of file +benchmarks/lfwe +venv \ No newline at end of file From 70bce5ae631cccbd638d0dba4ff4d56376277725 Mon Sep 17 00:00:00 2001 From: Josh Date: Fri, 23 Aug 2024 15:46:54 +0100 Subject: [PATCH 3/5] Sped up face alignment by using cv2 for rotation rather than PIL --- deepface/modules/detection.py | 110 ++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index 8b4d056..c78d5ea 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -12,7 +12,6 @@ from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion from deepface.commons import image_utils from deepface.commons.logger import Logger -import time logger = Logger() @@ -247,62 +246,62 @@ def detect_faces( # discard rest of the items facial_areas = facial_areas[0:max_faces] - start_time = time.time() results = [] + for facial_area in facial_areas: - x = facial_area.x - y = facial_area.y - w = facial_area.w - h = facial_area.h - left_eye = facial_area.left_eye - right_eye = facial_area.right_eye - confidence = facial_area.confidence - - if expand_percentage > 0: - # Expand the facial region height and width by the provided percentage - # ensuring that the expanded region stays within img.shape limits - expanded_w = w + int(w * expand_percentage / 100) - expanded_h = h + int(h * expand_percentage / 100) - - x = max(0, x - int((expanded_w - w) / 2)) - y = max(0, y - int((expanded_h - h) / 2)) - w = min(img.shape[1] - x, expanded_w) - h = min(img.shape[0] - y, expanded_h) - - # extract detected face unaligned - 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 - 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) - - rotated_x1, rotated_y1, rotated_x2, rotated_y2 = project_facial_area( - facial_area=(x, y, x + w, y + h), angle=angle, size=(img.shape[0], img.shape[1]) - ) - detected_face = aligned_img[ - int(rotated_y1) : int(rotated_y2), int(rotated_x1) : int(rotated_x2) - ] - - # restore x, y, le and re before border added - x = x - width_border - y = y - height_border - # w and h will not change - if left_eye is not None: - 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) - - result = 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 - ), - confidence=confidence, - ) - results.append(result) + 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 +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 + h = facial_area.h + left_eye = facial_area.left_eye + right_eye = facial_area.right_eye + confidence = facial_area.confidence + + if expand_percentage > 0: + # Expand the facial region height and width by the provided percentage + # ensuring that the expanded region stays within img.shape limits + expanded_w = w + int(w * expand_percentage / 100) + expanded_h = h + int(h * expand_percentage / 100) + + x = max(0, x - int((expanded_w - w) / 2)) + y = max(0, y - int((expanded_h - h) / 2)) + w = min(img.shape[1] - x, expanded_w) + h = min(img.shape[0] - y, expanded_h) + + # extract detected face unaligned + 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 + 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) + + rotated_x1, rotated_y1, rotated_x2, rotated_y2 = project_facial_area( + facial_area=(x, y, x + w, y + h), angle=angle, size=(img.shape[0], img.shape[1]) + ) + detected_face = aligned_img[ + int(rotated_y1) : int(rotated_y2), int(rotated_x1) : int(rotated_x2) + ] + + # restore x, y, le and re before border added + x = x - width_border + y = y - height_border + # w and h will not change + if left_eye is not None: + 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( + x=x, y=y, h=h, w=w, confidence=confidence, left_eye=left_eye, right_eye=right_eye + ), + confidence=confidence, + ) def align_img_wrt_eyes( img: np.ndarray, @@ -327,7 +326,12 @@ def align_img_wrt_eyes( return img, 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 From cc8c3f0461a253de512c2cdf39b35e16d382e9bd Mon Sep 17 00:00:00 2001 From: Josh Date: Sat, 24 Aug 2024 09:07:53 +0100 Subject: [PATCH 4/5] Fixed rotation angle --- deepface/modules/detection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index c78d5ea..d85e611 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -329,7 +329,7 @@ def align_img_wrt_eyes( (h, w) = img.shape[: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)) return img, angle From f4d164e0aa93aa40eaec4d080017d2ae6c6182b2 Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 26 Aug 2024 14:34:00 +0100 Subject: [PATCH 5/5] Linting + optimizations --- deepface/modules/detection.py | 46 +++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index d85e611..bdfca52 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -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 @@ -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 @@ -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 ) @@ -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 = [] + 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: - 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 - -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 @@ -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( @@ -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