mirror of
https://github.com/serengil/deepface.git
synced 2025-06-05 19:15:23 +00:00
Added max_faces
This commit is contained in:
parent
0022131bf6
commit
6bdf83b86f
@ -1,5 +1,5 @@
|
|||||||
# built-in dependencies
|
# built-in dependencies
|
||||||
from typing import Any, Dict, List, Tuple, Union
|
from typing import Any, Dict, List, Tuple, Union, Optional
|
||||||
|
|
||||||
# 3rd part dependencies
|
# 3rd part dependencies
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -10,7 +10,9 @@ from PIL import Image
|
|||||||
from deepface.modules import modeling
|
from deepface.modules import modeling
|
||||||
from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion
|
from deepface.models.Detector import Detector, DetectedFace, FacialAreaRegion
|
||||||
from deepface.commons import image_utils
|
from deepface.commons import image_utils
|
||||||
|
|
||||||
from deepface.commons.logger import Logger
|
from deepface.commons.logger import Logger
|
||||||
|
import time
|
||||||
|
|
||||||
logger = Logger()
|
logger = Logger()
|
||||||
|
|
||||||
@ -27,6 +29,7 @@ def extract_faces(
|
|||||||
color_face: str = "rgb",
|
color_face: str = "rgb",
|
||||||
normalize_face: bool = True,
|
normalize_face: bool = True,
|
||||||
anti_spoofing: bool = False,
|
anti_spoofing: bool = False,
|
||||||
|
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
|
||||||
@ -97,6 +100,7 @@ def extract_faces(
|
|||||||
img=img,
|
img=img,
|
||||||
align=align,
|
align=align,
|
||||||
expand_percentage=expand_percentage,
|
expand_percentage=expand_percentage,
|
||||||
|
max_faces=max_faces,
|
||||||
)
|
)
|
||||||
|
|
||||||
# in case of no face found
|
# in case of no face found
|
||||||
@ -176,7 +180,7 @@ def extract_faces(
|
|||||||
|
|
||||||
|
|
||||||
def detect_faces(
|
def detect_faces(
|
||||||
detector_backend: str, img: np.ndarray, align: bool = True, expand_percentage: int = 0
|
detector_backend: str, img: np.ndarray, align: bool = True, expand_percentage: int = 0, max_faces: Optional[int] = None
|
||||||
) -> List[DetectedFace]:
|
) -> List[DetectedFace]:
|
||||||
"""
|
"""
|
||||||
Detect face(s) from a given image
|
Detect face(s) from a given image
|
||||||
@ -202,7 +206,7 @@ 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
|
height, width, _ = img.shape
|
||||||
|
|
||||||
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
|
||||||
)
|
)
|
||||||
@ -233,6 +237,17 @@ def detect_faces(
|
|||||||
# find facial areas of given image
|
# find facial areas of given image
|
||||||
facial_areas = face_detector.detect_faces(img)
|
facial_areas = face_detector.detect_faces(img)
|
||||||
|
|
||||||
|
if max_faces is not None and max_faces < len(facial_areas):
|
||||||
|
# sort as largest facial areas come first
|
||||||
|
facial_areas = sorted(
|
||||||
|
facial_areas,
|
||||||
|
key=lambda facial_area: facial_area.w * facial_area.h,
|
||||||
|
reverse=True,
|
||||||
|
)
|
||||||
|
# discard rest of the items
|
||||||
|
facial_areas = facial_areas[0:max_faces]
|
||||||
|
|
||||||
|
start_time = time.time()
|
||||||
results = []
|
results = []
|
||||||
for facial_area in facial_areas:
|
for facial_area in facial_areas:
|
||||||
x = facial_area.x
|
x = facial_area.x
|
||||||
@ -285,6 +300,7 @@ def detect_faces(
|
|||||||
confidence=confidence,
|
confidence=confidence,
|
||||||
)
|
)
|
||||||
results.append(result)
|
results.append(result)
|
||||||
|
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
@ -81,6 +81,7 @@ def represent(
|
|||||||
align=align,
|
align=align,
|
||||||
expand_percentage=expand_percentage,
|
expand_percentage=expand_percentage,
|
||||||
anti_spoofing=anti_spoofing,
|
anti_spoofing=anti_spoofing,
|
||||||
|
max_faces=max_faces,
|
||||||
)
|
)
|
||||||
else: # skip
|
else: # skip
|
||||||
# Try load. If load error, will raise exception internal
|
# Try load. If load error, will raise exception internal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user