mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
yolo detect batched
This commit is contained in:
parent
b2d6178bed
commit
1bd83356e7
@ -1,6 +1,6 @@
|
|||||||
# built-in dependencies
|
# built-in dependencies
|
||||||
import os
|
import os
|
||||||
from typing import List, Any
|
from typing import List, Any, Union, Tuple
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
# 3rd party dependencies
|
# 3rd party dependencies
|
||||||
@ -62,25 +62,33 @@ class YoloDetectorClient(Detector):
|
|||||||
# Return face_detector
|
# Return face_detector
|
||||||
return YOLO(weight_file)
|
return YOLO(weight_file)
|
||||||
|
|
||||||
def detect_faces(self, img: np.ndarray) -> List[FacialAreaRegion]:
|
def detect_faces(self, imgs: Union[np.ndarray, List[np.ndarray]]) -> Union[List[List[FacialAreaRegion]], List[FacialAreaRegion]]:
|
||||||
"""
|
"""
|
||||||
Detect and align face with yolo
|
Detect and align faces in an image or a list of images with yolo
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
img (np.ndarray): pre-loaded image as numpy array
|
imgs (Union[np.ndarray, List[np.ndarray]]): pre-loaded image as numpy array or a list of those
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
results (List[FacialAreaRegion]): A list of FacialAreaRegion objects
|
results (Union[List[List[FacialAreaRegion]], List[FacialAreaRegion]]):
|
||||||
|
A list of lists of FacialAreaRegion objects for each image or a list of FacialAreaRegion objects
|
||||||
"""
|
"""
|
||||||
resp = []
|
if not isinstance(imgs, list):
|
||||||
|
imgs = [imgs]
|
||||||
|
|
||||||
# Detect faces
|
all_results = []
|
||||||
results = self.model.predict(
|
|
||||||
img,
|
# Detect faces for all images
|
||||||
|
results_list = self.model.predict(
|
||||||
|
imgs,
|
||||||
verbose=False,
|
verbose=False,
|
||||||
show=False,
|
show=False,
|
||||||
conf=float(os.getenv("YOLO_MIN_DETECTION_CONFIDENCE", "0.25")),
|
conf=float(os.getenv("YOLO_MIN_DETECTION_CONFIDENCE", "0.25")),
|
||||||
)[0]
|
)
|
||||||
|
|
||||||
|
# Iterate over each image's results
|
||||||
|
for results in results_list:
|
||||||
|
resp = []
|
||||||
|
|
||||||
# For each face, extract the bounding box, the landmarks and confidence
|
# For each face, extract the bounding box, the landmarks and confidence
|
||||||
for result in results:
|
for result in results:
|
||||||
@ -104,8 +112,9 @@ class YoloDetectorClient(Detector):
|
|||||||
left_eye = result.keypoints.xy[0][1].tolist()
|
left_eye = result.keypoints.xy[0][1].tolist()
|
||||||
|
|
||||||
# eyes are list of float, need to cast them tuple of int
|
# eyes are list of float, need to cast them tuple of int
|
||||||
left_eye = tuple(int(i) for i in left_eye)
|
# Ensure eyes are tuples of exactly two integers or None
|
||||||
right_eye = tuple(int(i) for i in right_eye)
|
left_eye = tuple(map(int, left_eye[:2])) if left_eye and len(left_eye) == 2 else None
|
||||||
|
right_eye = tuple(map(int, right_eye[:2])) if right_eye and len(right_eye) == 2 else None
|
||||||
|
|
||||||
x, y, w, h = int(x - w / 2), int(y - h / 2), int(w), int(h)
|
x, y, w, h = int(x - w / 2), int(y - h / 2), int(w), int(h)
|
||||||
facial_area = FacialAreaRegion(
|
facial_area = FacialAreaRegion(
|
||||||
@ -119,7 +128,11 @@ class YoloDetectorClient(Detector):
|
|||||||
)
|
)
|
||||||
resp.append(facial_area)
|
resp.append(facial_area)
|
||||||
|
|
||||||
return resp
|
all_results.append(resp)
|
||||||
|
|
||||||
|
if len(all_results) == 1:
|
||||||
|
return all_results[0]
|
||||||
|
return all_results
|
||||||
|
|
||||||
|
|
||||||
class YoloDetectorClientV8n(YoloDetectorClient):
|
class YoloDetectorClientV8n(YoloDetectorClient):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user