mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 12:05:22 +00:00
pseudo batched retinaface
This commit is contained in:
parent
619930cd1e
commit
b544a2d866
@ -1,5 +1,5 @@
|
|||||||
# built-in dependencies
|
# built-in dependencies
|
||||||
from typing import List
|
from typing import List, Union
|
||||||
|
|
||||||
# 3rd party dependencies
|
# 3rd party dependencies
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@ -13,23 +13,28 @@ class RetinaFaceClient(Detector):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.model = rf.build_model()
|
self.model = rf.build_model()
|
||||||
|
|
||||||
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 retinaface
|
Detect and align faces with retinaface in an image or a list of images
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
img (np.ndarray): pre-loaded image as numpy array
|
img (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[FacialAreaRegion], List[List[FacialAreaRegion]]]): A list or a list of lists of FacialAreaRegion objects
|
||||||
"""
|
"""
|
||||||
resp = []
|
if isinstance(img, np.ndarray):
|
||||||
|
imgs = [img]
|
||||||
|
else:
|
||||||
|
imgs = img
|
||||||
|
|
||||||
|
batch_results = []
|
||||||
|
|
||||||
|
for img in imgs:
|
||||||
|
resp = []
|
||||||
obj = rf.detect_faces(img, model=self.model, threshold=0.9)
|
obj = rf.detect_faces(img, model=self.model, threshold=0.9)
|
||||||
|
|
||||||
if not isinstance(obj, dict):
|
if isinstance(obj, dict):
|
||||||
return resp
|
|
||||||
|
|
||||||
for face_idx in obj.keys():
|
for face_idx in obj.keys():
|
||||||
identity = obj[face_idx]
|
identity = obj[face_idx]
|
||||||
detection = identity["facial_area"]
|
detection = identity["facial_area"]
|
||||||
@ -39,16 +44,12 @@ class RetinaFaceClient(Detector):
|
|||||||
x = detection[0]
|
x = detection[0]
|
||||||
w = detection[2] - x
|
w = detection[2] - x
|
||||||
|
|
||||||
# retinaface sets left and right eyes with respect to the person
|
left_eye = tuple(int(i) for i in identity["landmarks"]["left_eye"])
|
||||||
left_eye = identity["landmarks"]["left_eye"]
|
right_eye = tuple(int(i) for i in identity["landmarks"]["right_eye"])
|
||||||
right_eye = identity["landmarks"]["right_eye"]
|
|
||||||
nose = identity["landmarks"].get("nose")
|
nose = identity["landmarks"].get("nose")
|
||||||
mouth_right = identity["landmarks"].get("mouth_right")
|
mouth_right = identity["landmarks"].get("mouth_right")
|
||||||
mouth_left = identity["landmarks"].get("mouth_left")
|
mouth_left = identity["landmarks"].get("mouth_left")
|
||||||
|
|
||||||
# eyes are list of float, need to cast them tuple of int
|
|
||||||
left_eye = tuple(int(i) for i in left_eye)
|
|
||||||
right_eye = tuple(int(i) for i in right_eye)
|
|
||||||
if nose is not None:
|
if nose is not None:
|
||||||
nose = tuple(int(i) for i in nose)
|
nose = tuple(int(i) for i in nose)
|
||||||
if mouth_right is not None:
|
if mouth_right is not None:
|
||||||
@ -73,4 +74,6 @@ class RetinaFaceClient(Detector):
|
|||||||
|
|
||||||
resp.append(facial_area)
|
resp.append(facial_area)
|
||||||
|
|
||||||
return resp
|
batch_results.append(resp)
|
||||||
|
|
||||||
|
return batch_results if len(batch_results) > 1 else batch_results[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user