From bb8096ad97e69d6b057f952fec9c31e9c6a40166 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 10:19:26 -0300 Subject: [PATCH 1/6] Add flag to normalize output face image --- deepface/DeepFace.py | 5 +++++ deepface/modules/detection.py | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 327f9a88..b4608fb9 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -483,6 +483,7 @@ def extract_faces( align: bool = True, expand_percentage: int = 0, grayscale: bool = False, + normalize_face: bool = True, anti_spoofing: bool = False, ) -> List[Dict[str, Any]]: """ @@ -506,6 +507,9 @@ def extract_faces( grayscale (boolean): Flag to convert the output face image to grayscale (default is False). + normalize_face (boolean): Flag to enable normalization (divide by 255) of the output + face image output face image normalization (default is True). + anti_spoofing (boolean): Flag to enable anti spoofing (default is False). Returns: @@ -535,6 +539,7 @@ def extract_faces( align=align, expand_percentage=expand_percentage, grayscale=grayscale, + normalize_face=normalize_face, anti_spoofing=anti_spoofing, ) diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index 76daa084..d1879447 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -25,6 +25,7 @@ def extract_faces( align: bool = True, expand_percentage: int = 0, grayscale: bool = False, + normalize_face: bool = True, anti_spoofing: bool = False, ) -> List[Dict[str, Any]]: """ @@ -43,11 +44,14 @@ def extract_faces( align (bool): Flag to enable face alignment (default is True). - expand_percentage (int): expand detected facial area with a percentage + expand_percentage (int): expand detected facial area with a percentage. grayscale (boolean): Flag to convert the output face image to grayscale (default is False). + normalize_face (boolean): Flag to enable normalization (divide by 255) of the output + face image output face image normalization (default is True). + anti_spoofing (boolean): Flag to enable anti spoofing (default is False). Returns: @@ -117,7 +121,8 @@ def extract_faces( if grayscale is True: current_img = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY) - current_img = current_img / 255 # normalize input in [0, 1] + if normalize_face: + current_img = current_img / 255 # normalize input in [0, 1] x = int(current_region.x) y = int(current_region.y) From 878564ee1c0e8d2682b2743a1bf812cc5de4feb7 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 10:21:42 -0300 Subject: [PATCH 2/6] Add color_face option for extract_faces --- deepface/DeepFace.py | 11 ++++++----- deepface/modules/demography.py | 3 ++- deepface/modules/detection.py | 18 +++++++++++++----- deepface/modules/recognition.py | 6 ++++-- deepface/modules/representation.py | 3 ++- deepface/modules/verification.py | 3 ++- 6 files changed, 29 insertions(+), 15 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index b4608fb9..2cebe4a4 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -482,7 +482,7 @@ def extract_faces( enforce_detection: bool = True, align: bool = True, expand_percentage: int = 0, - grayscale: bool = False, + color_face: str = 'rgb', normalize_face: bool = True, anti_spoofing: bool = False, ) -> List[Dict[str, Any]]: @@ -504,8 +504,8 @@ def extract_faces( expand_percentage (int): expand detected facial area with a percentage (default is 0). - grayscale (boolean): Flag to convert the output face image to grayscale - (default is False). + color_face (string): Color to return face image output. Options: 'rgb', 'bgr' or 'gray' + (default is 'rgb'). normalize_face (boolean): Flag to enable normalization (divide by 255) of the output face image output face image normalization (default is True). @@ -538,7 +538,7 @@ def extract_faces( enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, - grayscale=grayscale, + color_face=color_face, normalize_face=normalize_face, anti_spoofing=anti_spoofing, ) @@ -591,7 +591,8 @@ def detectFace( detector_backend=detector_backend, enforce_detection=enforce_detection, align=align, - grayscale=False, + color_face='rgb', + normalize_face=True, ) extracted_face = None if len(face_objs) > 0: diff --git a/deepface/modules/demography.py b/deepface/modules/demography.py index 3cc3ebc6..e6d49897 100644 --- a/deepface/modules/demography.py +++ b/deepface/modules/demography.py @@ -123,7 +123,8 @@ def analyze( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - grayscale=False, + color_face='rgb', + normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index d1879447..35963643 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -24,7 +24,7 @@ def extract_faces( enforce_detection: bool = True, align: bool = True, expand_percentage: int = 0, - grayscale: bool = False, + color_face: str = 'rgb', normalize_face: bool = True, anti_spoofing: bool = False, ) -> List[Dict[str, Any]]: @@ -46,8 +46,8 @@ def extract_faces( expand_percentage (int): expand detected facial area with a percentage. - grayscale (boolean): Flag to convert the output face image to grayscale - (default is False). + color_face (string): Color to return face image output. Options: 'rgb', 'bgr' or 'gray' + (default is 'rgb'). normalize_face (boolean): Flag to enable normalization (divide by 255) of the output face image output face image normalization (default is True). @@ -118,8 +118,16 @@ def extract_faces( if current_img.shape[0] == 0 or current_img.shape[1] == 0: continue - if grayscale is True: + if color_face == 'rgb': + current_img = current_img[:, :, ::-1] + elif color_face == 'bgr': + pass # image is in BGR + elif color_face == 'gray': current_img = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY) + else: + raise ValueError( + f"The color_face can be rgb, bgr or gray, but it is {color_face}." + ) if normalize_face: current_img = current_img / 255 # normalize input in [0, 1] @@ -130,7 +138,7 @@ def extract_faces( h = int(current_region.h) resp_obj = { - "face": current_img if grayscale else current_img[:, :, ::-1], + "face": current_img, "facial_area": { "x": x, "y": y, diff --git a/deepface/modules/recognition.py b/deepface/modules/recognition.py index 4eda51f9..608202b9 100644 --- a/deepface/modules/recognition.py +++ b/deepface/modules/recognition.py @@ -240,7 +240,8 @@ def find( source_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - grayscale=False, + color_face='rgb', + normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, @@ -364,7 +365,8 @@ def __find_bulk_embeddings( img_objs = detection.extract_faces( img_path=employee, detector_backend=detector_backend, - grayscale=False, + color_face='rgb', + normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/representation.py b/deepface/modules/representation.py index b2282881..48cc0e65 100644 --- a/deepface/modules/representation.py +++ b/deepface/modules/representation.py @@ -71,7 +71,8 @@ def represent( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - grayscale=False, + color_face='rgb', + normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/verification.py b/deepface/modules/verification.py index 8b03ed43..f6538021 100644 --- a/deepface/modules/verification.py +++ b/deepface/modules/verification.py @@ -239,7 +239,8 @@ def __extract_faces_and_embeddings( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - grayscale=False, + color_face='rgb', + normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, From 9300ed0dfd922c582646983326d0566499e12d62 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 10:45:05 -0300 Subject: [PATCH 3/6] Add grayscale parameter again --- deepface/DeepFace.py | 5 +++++ deepface/modules/detection.py | 24 ++++++++++++++++-------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 2cebe4a4..cd0d4f7f 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -482,6 +482,7 @@ def extract_faces( enforce_detection: bool = True, align: bool = True, expand_percentage: int = 0, + grayscale: bool = False, color_face: str = 'rgb', normalize_face: bool = True, anti_spoofing: bool = False, @@ -504,6 +505,9 @@ def extract_faces( expand_percentage (int): expand detected facial area with a percentage (default is 0). + grayscale (boolean): (Deprecated) Flag to convert the output face image to grayscale + (default is False). + color_face (string): Color to return face image output. Options: 'rgb', 'bgr' or 'gray' (default is 'rgb'). @@ -538,6 +542,7 @@ def extract_faces( enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, + grayscale=grayscale, color_face=color_face, normalize_face=normalize_face, anti_spoofing=anti_spoofing, diff --git a/deepface/modules/detection.py b/deepface/modules/detection.py index 35963643..0c32aa44 100644 --- a/deepface/modules/detection.py +++ b/deepface/modules/detection.py @@ -24,6 +24,7 @@ def extract_faces( enforce_detection: bool = True, align: bool = True, expand_percentage: int = 0, + grayscale: bool = False, color_face: str = 'rgb', normalize_face: bool = True, anti_spoofing: bool = False, @@ -46,6 +47,9 @@ def extract_faces( expand_percentage (int): expand detected facial area with a percentage. + grayscale (boolean): (Deprecated) Flag to convert the output face image to grayscale + (default is False). + color_face (string): Color to return face image output. Options: 'rgb', 'bgr' or 'gray' (default is 'rgb'). @@ -118,16 +122,20 @@ def extract_faces( if current_img.shape[0] == 0 or current_img.shape[1] == 0: continue - if color_face == 'rgb': - current_img = current_img[:, :, ::-1] - elif color_face == 'bgr': - pass # image is in BGR - elif color_face == 'gray': + if grayscale is True: + logger.warn("Parameter grayscale is deprecated. Use color_face instead.") current_img = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY) else: - raise ValueError( - f"The color_face can be rgb, bgr or gray, but it is {color_face}." - ) + if color_face == 'rgb': + current_img = current_img[:, :, ::-1] + elif color_face == 'bgr': + pass # image is in BGR + elif color_face == 'gray': + current_img = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY) + else: + raise ValueError( + f"The color_face can be rgb, bgr or gray, but it is {color_face}." + ) if normalize_face: current_img = current_img / 255 # normalize input in [0, 1] From 546675ca4065e13dfcaa39f13bdbceba8518d26a Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 11:14:15 -0300 Subject: [PATCH 4/6] Remove call for default values --- deepface/modules/demography.py | 2 -- deepface/modules/recognition.py | 4 ---- deepface/modules/representation.py | 2 -- deepface/modules/verification.py | 2 -- 4 files changed, 10 deletions(-) diff --git a/deepface/modules/demography.py b/deepface/modules/demography.py index e6d49897..cf101c6e 100644 --- a/deepface/modules/demography.py +++ b/deepface/modules/demography.py @@ -123,8 +123,6 @@ def analyze( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - color_face='rgb', - normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/recognition.py b/deepface/modules/recognition.py index 608202b9..72d2fd3b 100644 --- a/deepface/modules/recognition.py +++ b/deepface/modules/recognition.py @@ -240,8 +240,6 @@ def find( source_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - color_face='rgb', - normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, @@ -365,8 +363,6 @@ def __find_bulk_embeddings( img_objs = detection.extract_faces( img_path=employee, detector_backend=detector_backend, - color_face='rgb', - normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/representation.py b/deepface/modules/representation.py index 48cc0e65..bbccc276 100644 --- a/deepface/modules/representation.py +++ b/deepface/modules/representation.py @@ -71,8 +71,6 @@ def represent( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - color_face='rgb', - normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/verification.py b/deepface/modules/verification.py index f6538021..c6b5ee69 100644 --- a/deepface/modules/verification.py +++ b/deepface/modules/verification.py @@ -239,8 +239,6 @@ def __extract_faces_and_embeddings( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, - color_face='rgb', - normalize_face=True, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, From d385c63766868fdc69d91ac4a03c5c8579717859 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 11:17:14 -0300 Subject: [PATCH 5/6] Remove default param --- deepface/DeepFace.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index cd0d4f7f..81a418d7 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -596,8 +596,6 @@ def detectFace( detector_backend=detector_backend, enforce_detection=enforce_detection, align=align, - color_face='rgb', - normalize_face=True, ) extracted_face = None if len(face_objs) > 0: From 0e423a64d13229cf34cd8eab4917c0735d54f91f Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 11:20:32 -0300 Subject: [PATCH 6/6] Restore grayscale=False --- deepface/DeepFace.py | 1 + deepface/modules/demography.py | 1 + deepface/modules/recognition.py | 2 ++ deepface/modules/representation.py | 1 + deepface/modules/verification.py | 1 + 5 files changed, 6 insertions(+) diff --git a/deepface/DeepFace.py b/deepface/DeepFace.py index 81a418d7..a81f8f1b 100644 --- a/deepface/DeepFace.py +++ b/deepface/DeepFace.py @@ -594,6 +594,7 @@ def detectFace( face_objs = extract_faces( img_path=img_path, detector_backend=detector_backend, + grayscale=False, enforce_detection=enforce_detection, align=align, ) diff --git a/deepface/modules/demography.py b/deepface/modules/demography.py index cf101c6e..16345714 100644 --- a/deepface/modules/demography.py +++ b/deepface/modules/demography.py @@ -124,6 +124,7 @@ def analyze( img_path=img_path, detector_backend=detector_backend, enforce_detection=enforce_detection, + grayscale=False, align=align, expand_percentage=expand_percentage, anti_spoofing=anti_spoofing, diff --git a/deepface/modules/recognition.py b/deepface/modules/recognition.py index 72d2fd3b..4eda51f9 100644 --- a/deepface/modules/recognition.py +++ b/deepface/modules/recognition.py @@ -240,6 +240,7 @@ def find( source_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, + grayscale=False, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, @@ -363,6 +364,7 @@ def __find_bulk_embeddings( img_objs = detection.extract_faces( img_path=employee, detector_backend=detector_backend, + grayscale=False, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/representation.py b/deepface/modules/representation.py index bbccc276..b2282881 100644 --- a/deepface/modules/representation.py +++ b/deepface/modules/representation.py @@ -71,6 +71,7 @@ def represent( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, + grayscale=False, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage, diff --git a/deepface/modules/verification.py b/deepface/modules/verification.py index c6b5ee69..8b03ed43 100644 --- a/deepface/modules/verification.py +++ b/deepface/modules/verification.py @@ -239,6 +239,7 @@ def __extract_faces_and_embeddings( img_objs = detection.extract_faces( img_path=img_path, detector_backend=detector_backend, + grayscale=False, enforce_detection=enforce_detection, align=align, expand_percentage=expand_percentage,