Sped up face alignment by using cv2 for rotation rather than PIL

This commit is contained in:
Josh 2024-08-23 15:46:54 +01:00
parent d478f41c47
commit 70bce5ae63

View File

@ -12,7 +12,6 @@ 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()
@ -247,9 +246,14 @@ def detect_faces(
# discard rest of the items # discard rest of the items
facial_areas = facial_areas[0:max_faces] 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:
results.append(expand_and_align_face(facial_area=facial_area, img=img, align=align, expand_percentage=expand_percentage, width_border=width_border, height_border=height_border))
return results
def expand_and_align_face(facial_area: FacialAreaRegion, img: np.ndarray, align: bool, expand_percentage: int, width_border: int, height_border: int) -> DetectedFace:
x = facial_area.x x = facial_area.x
y = facial_area.y y = facial_area.y
w = facial_area.w w = facial_area.w
@ -271,7 +275,6 @@ def detect_faces(
# extract detected face unaligned # extract detected face unaligned
detected_face = img[int(y) : int(y + h), int(x) : int(x + w)] detected_face = img[int(y) : int(y + h), int(x) : int(x + w)]
# align original image, then find projection of detected face area after alignment # align original image, then find projection of detected face area after alignment
if align is True: # and left_eye is not None and right_eye is not None: if align is True: # and left_eye is not None and right_eye is not None:
aligned_img, angle = align_img_wrt_eyes(img=img, left_eye=left_eye, right_eye=right_eye) aligned_img, angle = align_img_wrt_eyes(img=img, left_eye=left_eye, right_eye=right_eye)
@ -292,17 +295,13 @@ def detect_faces(
if right_eye is not None: if right_eye is not None:
right_eye = (right_eye[0] - width_border, right_eye[1] - height_border) right_eye = (right_eye[0] - width_border, right_eye[1] - height_border)
result = DetectedFace( return DetectedFace(
img=detected_face, img=detected_face,
facial_area=FacialAreaRegion( facial_area=FacialAreaRegion(
x=x, y=y, h=h, w=w, confidence=confidence, left_eye=left_eye, right_eye=right_eye x=x, y=y, h=h, w=w, confidence=confidence, left_eye=left_eye, right_eye=right_eye
), ),
confidence=confidence, confidence=confidence,
) )
results.append(result)
return results
def align_img_wrt_eyes( def align_img_wrt_eyes(
img: np.ndarray, img: np.ndarray,
@ -327,7 +326,12 @@ def align_img_wrt_eyes(
return img, 0 return img, 0
angle = float(np.degrees(np.arctan2(left_eye[1] - right_eye[1], left_eye[0] - right_eye[0]))) angle = float(np.degrees(np.arctan2(left_eye[1] - right_eye[1], left_eye[0] - right_eye[0])))
img = np.array(Image.fromarray(img).rotate(angle, resample=Image.BICUBIC))
(h, w) = img.shape[:2]
center = (w // 2, h // 2)
M = cv2.getRotationMatrix2D(center, -angle, 1.0)
img = cv2.warpAffine(img, M, (w, h), flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_CONSTANT, borderValue=(0,0,0))
return img, angle return img, angle