From 878564ee1c0e8d2682b2743a1bf812cc5de4feb7 Mon Sep 17 00:00:00 2001 From: Programador Artificial Date: Mon, 15 Jul 2024 10:21:42 -0300 Subject: [PATCH] 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 b4608fb..2cebe4a 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 3cc3ebc..e6d4989 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 d187944..3596364 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 4eda51f..608202b 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 b228288..48cc0e6 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 8b03ed4..f653802 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,