From 1d358aa15a3ce6d3fd3be5cd1121cfb66b7cfadc Mon Sep 17 00:00:00 2001 From: galthran-wq Date: Tue, 18 Feb 2025 09:44:59 +0000 Subject: [PATCH] pseudo-batching dlib --- deepface/models/face_detection/Dlib.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/deepface/models/face_detection/Dlib.py b/deepface/models/face_detection/Dlib.py index 26bce84..2ff9c86 100644 --- a/deepface/models/face_detection/Dlib.py +++ b/deepface/models/face_detection/Dlib.py @@ -1,5 +1,5 @@ # built-in dependencies -from typing import List +from typing import List, Union # 3rd party dependencies import numpy as np @@ -47,10 +47,27 @@ class DlibClient(Detector): detector["sp"] = sp return detector - def detect_faces(self, img: np.ndarray) -> List[FacialAreaRegion]: + def detect_faces(self, img: Union[np.ndarray, List[np.ndarray]]) -> Union[List[FacialAreaRegion], List[List[FacialAreaRegion]]]: """ Detect and align face with dlib + Args: + img (Union[np.ndarray, List[np.ndarray]]): pre-loaded image as numpy array or a list of those + + Returns: + results (Union[List[FacialAreaRegion], List[List[FacialAreaRegion]]]): A list or a list of lists of FacialAreaRegion objects + """ + if isinstance(img, np.ndarray): + return self._detect_faces_in_single_image(img) + elif isinstance(img, list): + return [self._detect_faces_in_single_image(single_img) for single_img in img] + else: + raise ValueError("Input must be a numpy array or a list of numpy arrays.") + + def _detect_faces_in_single_image(self, img: np.ndarray) -> List[FacialAreaRegion]: + """ + Helper function to detect faces in a single image. + Args: img (np.ndarray): pre-loaded image as numpy array