Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom output extract faces #1279

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions deepface/DeepFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,8 @@ def extract_faces(
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]]:
"""
Expand All @@ -503,9 +505,15 @@ 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
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').

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:
Expand Down Expand Up @@ -535,6 +543,8 @@ def extract_faces(
align=align,
expand_percentage=expand_percentage,
grayscale=grayscale,
color_face=color_face,
normalize_face=normalize_face,
anti_spoofing=anti_spoofing,
)

Expand Down Expand Up @@ -584,9 +594,9 @@ def detectFace(
face_objs = extract_faces(
img_path=img_path,
detector_backend=detector_backend,
grayscale=False,
enforce_detection=enforce_detection,
align=align,
grayscale=False,
serengil marked this conversation as resolved.
Show resolved Hide resolved
)
extracted_face = None
if len(face_objs) > 0:
Expand Down
2 changes: 1 addition & 1 deletion deepface/modules/demography.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def analyze(
img_objs = detection.extract_faces(
img_path=img_path,
detector_backend=detector_backend,
grayscale=False,
serengil marked this conversation as resolved.
Show resolved Hide resolved
enforce_detection=enforce_detection,
grayscale=False,
align=align,
expand_percentage=expand_percentage,
anti_spoofing=anti_spoofing,
Expand Down
31 changes: 26 additions & 5 deletions deepface/modules/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ def extract_faces(
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]]:
"""
Expand All @@ -43,11 +45,17 @@ 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
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').

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:
Expand Down Expand Up @@ -115,17 +123,30 @@ def extract_faces(
continue

if grayscale is True:
logger.warn("Parameter grayscale is deprecated. Use color_face instead.")
current_img = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY)

current_img = current_img / 255 # normalize input in [0, 1]
else:
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]

x = int(current_region.x)
y = int(current_region.y)
w = int(current_region.w)
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,
Expand Down