mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 03:55:21 +00:00
batched detection
This commit is contained in:
parent
72e82f0605
commit
f4d18a70c0
@ -19,7 +19,7 @@ logger = Logger()
|
|||||||
|
|
||||||
|
|
||||||
def extract_faces(
|
def extract_faces(
|
||||||
img_path: Union[str, np.ndarray, IO[bytes]],
|
img_path: Union[List[Union[str, np.ndarray, IO[bytes]]], str, np.ndarray, IO[bytes]],
|
||||||
detector_backend: str = "opencv",
|
detector_backend: str = "opencv",
|
||||||
enforce_detection: bool = True,
|
enforce_detection: bool = True,
|
||||||
align: bool = True,
|
align: bool = True,
|
||||||
@ -31,10 +31,10 @@ def extract_faces(
|
|||||||
max_faces: Optional[int] = None,
|
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 or list of images
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
img_path (str or np.ndarray or IO[bytes]): Path to the first image. Accepts exact image path
|
img_paths (List[str or np.ndarray or IO[bytes]] or str or np.ndarray or IO[bytes]): Path(s) to the image(s). Accepts exact image path
|
||||||
as a string, numpy array (BGR), a file object that supports at least `.read` and is
|
as a string, numpy array (BGR), a file object that supports at least `.read` and is
|
||||||
opened in binary mode, or base64 encoded images.
|
opened in binary mode, or base64 encoded images.
|
||||||
|
|
||||||
@ -80,135 +80,140 @@ def extract_faces(
|
|||||||
just available in the result only if anti_spoofing is set to True in input arguments.
|
just available in the result only if anti_spoofing is set to True in input arguments.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
resp_objs = []
|
if not isinstance(img_path, list):
|
||||||
|
img_path = [img_path]
|
||||||
|
|
||||||
# img might be path, base64 or numpy array. Convert it to numpy whatever it is.
|
all_images = []
|
||||||
img, img_name = image_utils.load_image(img_path)
|
img_names = []
|
||||||
|
|
||||||
if img is None:
|
for single_img_path in img_path:
|
||||||
raise ValueError(f"Exception while loading {img_name}")
|
# img might be path, base64 or numpy array. Convert it to numpy whatever it is.
|
||||||
|
img, img_name = image_utils.load_image(single_img_path)
|
||||||
|
|
||||||
height, width, _ = img.shape
|
if img is None:
|
||||||
|
raise ValueError(f"Exception while loading {img_name}")
|
||||||
|
|
||||||
base_region = FacialAreaRegion(x=0, y=0, w=width, h=height, confidence=0)
|
all_images.append(img)
|
||||||
|
img_names.append(img_name)
|
||||||
|
|
||||||
if detector_backend == "skip":
|
# Run detect_faces for all images at once
|
||||||
face_objs = [DetectedFace(img=img, facial_area=base_region, confidence=0)]
|
all_face_objs = detect_faces(
|
||||||
else:
|
detector_backend=detector_backend,
|
||||||
face_objs = detect_faces(
|
img=all_images,
|
||||||
detector_backend=detector_backend,
|
align=align,
|
||||||
img=img,
|
expand_percentage=expand_percentage,
|
||||||
align=align,
|
max_faces=max_faces,
|
||||||
expand_percentage=expand_percentage,
|
)
|
||||||
max_faces=max_faces,
|
|
||||||
)
|
|
||||||
|
|
||||||
# in case of no face found
|
if len(all_images) == 1:
|
||||||
if len(face_objs) == 0 and enforce_detection is True:
|
all_face_objs = [all_face_objs]
|
||||||
if img_name is not None:
|
|
||||||
raise ValueError(
|
|
||||||
f"Face could not be detected in {img_name}."
|
|
||||||
"Please confirm that the picture is a face photo "
|
|
||||||
"or consider to set enforce_detection param to False."
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(
|
|
||||||
"Face could not be detected. Please confirm that the picture is a face photo "
|
|
||||||
"or consider to set enforce_detection param to False."
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(face_objs) == 0 and enforce_detection is False:
|
all_resp_objs = []
|
||||||
face_objs = [DetectedFace(img=img, facial_area=base_region, confidence=0)]
|
|
||||||
|
|
||||||
for face_obj in face_objs:
|
for img, img_name, face_objs in zip(all_images, img_names, all_face_objs):
|
||||||
current_img = face_obj.img
|
height, width, _ = img.shape
|
||||||
current_region = face_obj.facial_area
|
|
||||||
|
|
||||||
if current_img.shape[0] == 0 or current_img.shape[1] == 0:
|
if len(face_objs) == 0 and enforce_detection is True:
|
||||||
continue
|
if img_name is not None:
|
||||||
|
raise ValueError(
|
||||||
|
f"Face could not be detected in {img_name}."
|
||||||
|
"Please confirm that the picture is a face photo "
|
||||||
|
"or consider to set enforce_detection param to False."
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ValueError(
|
||||||
|
"Face could not be detected. Please confirm that the picture is a face photo "
|
||||||
|
"or consider to set enforce_detection param to False."
|
||||||
|
)
|
||||||
|
|
||||||
if grayscale is True:
|
if len(face_objs) == 0 and enforce_detection is False:
|
||||||
logger.warn("Parameter grayscale is deprecated. Use color_face instead.")
|
base_region = FacialAreaRegion(x=0, y=0, w=width, h=height, confidence=0)
|
||||||
current_img = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY)
|
face_objs = [DetectedFace(img=img, facial_area=base_region, confidence=0)]
|
||||||
else:
|
|
||||||
if color_face == "rgb":
|
for face_obj in face_objs:
|
||||||
current_img = current_img[:, :, ::-1]
|
current_img = face_obj.img
|
||||||
elif color_face == "bgr":
|
current_region = face_obj.facial_area
|
||||||
pass # image is in BGR
|
|
||||||
elif color_face == "gray":
|
if current_img.shape[0] == 0 or current_img.shape[1] == 0:
|
||||||
|
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 = cv2.cvtColor(current_img, cv2.COLOR_BGR2GRAY)
|
||||||
else:
|
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:
|
if normalize_face:
|
||||||
current_img = current_img / 255 # normalize input in [0, 1]
|
current_img = current_img / 255 # normalize input in [0, 1]
|
||||||
|
|
||||||
# cast to int for flask, and do final checks for borders
|
# cast to int for flask, and do final checks for borders
|
||||||
x = max(0, int(current_region.x))
|
x = max(0, int(current_region.x))
|
||||||
y = max(0, int(current_region.y))
|
y = max(0, int(current_region.y))
|
||||||
w = min(width - x - 1, int(current_region.w))
|
w = min(width - x - 1, int(current_region.w))
|
||||||
h = min(height - y - 1, int(current_region.h))
|
h = min(height - y - 1, int(current_region.h))
|
||||||
|
|
||||||
facial_area = {
|
facial_area = {
|
||||||
"x": x,
|
"x": x,
|
||||||
"y": y,
|
"y": y,
|
||||||
"w": w,
|
"w": w,
|
||||||
"h": h,
|
"h": h,
|
||||||
"left_eye": current_region.left_eye,
|
"left_eye": current_region.left_eye,
|
||||||
"right_eye": current_region.right_eye,
|
"right_eye": current_region.right_eye,
|
||||||
}
|
}
|
||||||
|
|
||||||
# optional nose, mouth_left and mouth_right fields are coming just for retinaface
|
# optional nose, mouth_left and mouth_right fields are coming just for retinaface
|
||||||
if current_region.nose is not None:
|
if current_region.nose is not None:
|
||||||
facial_area["nose"] = current_region.nose
|
facial_area["nose"] = current_region.nose
|
||||||
if current_region.mouth_left is not None:
|
if current_region.mouth_left is not None:
|
||||||
facial_area["mouth_left"] = current_region.mouth_left
|
facial_area["mouth_left"] = current_region.mouth_left
|
||||||
if current_region.mouth_right is not None:
|
if current_region.mouth_right is not None:
|
||||||
facial_area["mouth_right"] = current_region.mouth_right
|
facial_area["mouth_right"] = current_region.mouth_right
|
||||||
|
|
||||||
resp_obj = {
|
resp_obj = {
|
||||||
"face": current_img,
|
"face": current_img,
|
||||||
"facial_area": facial_area,
|
"facial_area": facial_area,
|
||||||
"confidence": round(float(current_region.confidence or 0), 2),
|
"confidence": round(float(current_region.confidence or 0), 2),
|
||||||
}
|
}
|
||||||
|
|
||||||
if anti_spoofing is True:
|
if anti_spoofing is True:
|
||||||
antispoof_model = modeling.build_model(task="spoofing", model_name="Fasnet")
|
antispoof_model = modeling.build_model(task="spoofing", model_name="Fasnet")
|
||||||
is_real, antispoof_score = antispoof_model.analyze(img=img, facial_area=(x, y, w, h))
|
is_real, antispoof_score = antispoof_model.analyze(img=img, facial_area=(x, y, w, h))
|
||||||
resp_obj["is_real"] = is_real
|
resp_obj["is_real"] = is_real
|
||||||
resp_obj["antispoof_score"] = antispoof_score
|
resp_obj["antispoof_score"] = antispoof_score
|
||||||
|
|
||||||
resp_objs.append(resp_obj)
|
all_resp_objs.append(resp_obj)
|
||||||
|
|
||||||
if len(resp_objs) == 0 and enforce_detection == True:
|
return all_resp_objs
|
||||||
raise ValueError(
|
|
||||||
f"Exception while extracting faces from {img_name}."
|
|
||||||
"Consider to set enforce_detection arg to False."
|
|
||||||
)
|
|
||||||
|
|
||||||
return resp_objs
|
|
||||||
|
|
||||||
|
|
||||||
def detect_faces(
|
def detect_faces(
|
||||||
detector_backend: str,
|
detector_backend: str,
|
||||||
img: np.ndarray,
|
img: Union[np.ndarray, List[np.ndarray]],
|
||||||
align: bool = True,
|
align: bool = True,
|
||||||
expand_percentage: int = 0,
|
expand_percentage: int = 0,
|
||||||
max_faces: Optional[int] = None,
|
max_faces: Optional[int] = None,
|
||||||
) -> List[DetectedFace]:
|
) -> Union[List[List[DetectedFace]], List[DetectedFace]]:
|
||||||
"""
|
"""
|
||||||
Detect face(s) from a given image
|
Detect face(s) from a given image or list of images
|
||||||
Args:
|
Args:
|
||||||
detector_backend (str): detector name
|
detector_backend (str): detector name
|
||||||
|
|
||||||
img (np.ndarray): pre-loaded image
|
img (np.ndarray or List[np.ndarray]): pre-loaded image or list of images
|
||||||
|
|
||||||
align (bool): enable or disable alignment after detection
|
align (bool): enable or disable alignment after detection
|
||||||
|
|
||||||
expand_percentage (int): expand detected facial area with a percentage (default is 0).
|
expand_percentage (int): expand detected facial area with a percentage (default is 0).
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
results (List[DetectedFace]): A list of DetectedFace objects
|
results (Union[List[List[DetectedFace]], List[DetectedFace]]):
|
||||||
|
A list of lists of DetectedFace objects or a list of DetectedFace objects
|
||||||
where each object contains:
|
where each object contains:
|
||||||
|
|
||||||
- img (np.ndarray): The detected face as a NumPy array.
|
- img (np.ndarray): The detected face as a NumPy array.
|
||||||
@ -219,53 +224,65 @@ 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
|
if not isinstance(img, list):
|
||||||
|
img = [img]
|
||||||
|
|
||||||
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
|
||||||
)
|
)
|
||||||
|
all_detected_faces = []
|
||||||
|
|
||||||
# validate expand percentage score
|
for single_img in img:
|
||||||
if expand_percentage < 0:
|
height, width, _ = single_img.shape
|
||||||
logger.warn(
|
|
||||||
f"Expand percentage cannot be negative but you set it to {expand_percentage}."
|
|
||||||
"Overwritten it to 0."
|
|
||||||
)
|
|
||||||
expand_percentage = 0
|
|
||||||
|
|
||||||
# If faces are close to the upper boundary, alignment move them outside
|
# validate expand percentage score
|
||||||
# Add a black border around an image to avoid this.
|
if expand_percentage < 0:
|
||||||
height_border = int(0.5 * height)
|
logger.warn(
|
||||||
width_border = int(0.5 * width)
|
f"Expand percentage cannot be negative but you set it to {expand_percentage}."
|
||||||
if align is True:
|
"Overwritten it to 0."
|
||||||
img = cv2.copyMakeBorder(
|
)
|
||||||
img,
|
expand_percentage = 0
|
||||||
height_border,
|
|
||||||
height_border,
|
|
||||||
width_border,
|
|
||||||
width_border,
|
|
||||||
cv2.BORDER_CONSTANT,
|
|
||||||
value=[0, 0, 0], # Color of the border (black)
|
|
||||||
)
|
|
||||||
|
|
||||||
# find facial areas of given image
|
# If faces are close to the upper boundary, alignment move them outside
|
||||||
facial_areas = face_detector.detect_faces(img)
|
# Add a black border around an image to avoid this.
|
||||||
|
height_border = int(0.5 * height)
|
||||||
|
width_border = int(0.5 * width)
|
||||||
|
if align is True:
|
||||||
|
single_img = cv2.copyMakeBorder(
|
||||||
|
single_img,
|
||||||
|
height_border,
|
||||||
|
height_border,
|
||||||
|
width_border,
|
||||||
|
width_border,
|
||||||
|
cv2.BORDER_CONSTANT,
|
||||||
|
value=[0, 0, 0], # Color of the border (black)
|
||||||
|
)
|
||||||
|
|
||||||
if max_faces is not None and max_faces < len(facial_areas):
|
# find facial areas of given image
|
||||||
facial_areas = nlargest(
|
facial_areas = face_detector.detect_faces(single_img)
|
||||||
max_faces, facial_areas, key=lambda facial_area: facial_area.w * facial_area.h
|
|
||||||
)
|
|
||||||
|
|
||||||
return [
|
if max_faces is not None and max_faces < len(facial_areas):
|
||||||
extract_face(
|
facial_areas = nlargest(
|
||||||
facial_area=facial_area,
|
max_faces, facial_areas, key=lambda facial_area: facial_area.w * facial_area.h
|
||||||
img=img,
|
)
|
||||||
align=align,
|
|
||||||
expand_percentage=expand_percentage,
|
detected_faces = [
|
||||||
width_border=width_border,
|
extract_face(
|
||||||
height_border=height_border,
|
facial_area=facial_area,
|
||||||
)
|
img=single_img,
|
||||||
for facial_area in facial_areas
|
align=align,
|
||||||
]
|
expand_percentage=expand_percentage,
|
||||||
|
width_border=width_border,
|
||||||
|
height_border=height_border,
|
||||||
|
)
|
||||||
|
for facial_area in facial_areas
|
||||||
|
]
|
||||||
|
|
||||||
|
all_detected_faces.append(detected_faces)
|
||||||
|
|
||||||
|
if len(all_detected_faces) == 1:
|
||||||
|
return all_detected_faces[0]
|
||||||
|
return all_detected_faces
|
||||||
|
|
||||||
|
|
||||||
def extract_face(
|
def extract_face(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user