Skip to content

Commit

Permalink
Merge pull request #863 from bhky/master
Browse files Browse the repository at this point in the history
Simplify face alignment procedure
  • Loading branch information
serengil authored Dec 21, 2023
2 parents 0c0dfb8 + dc75d76 commit bd6393d
Showing 1 changed file with 14 additions and 44 deletions.
58 changes: 14 additions & 44 deletions deepface/detectors/FaceDetector.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import math
from PIL import Image
import numpy as np
from deepface.commons import distance
from deepface.detectors import (
OpenCvWrapper,
SsdWrapper,
Expand Down Expand Up @@ -85,47 +83,19 @@ def detect_faces(face_detector, detector_backend, img, align=True):
raise ValueError("invalid detector_backend passed - " + detector_backend)


def alignment_procedure(img, left_eye, right_eye):
# this function aligns given face in img based on left and right eye coordinates

left_eye_x, left_eye_y = left_eye
right_eye_x, right_eye_y = right_eye

# -----------------------
# find rotation direction

if left_eye_y > right_eye_y:
point_3rd = (right_eye_x, left_eye_y)
direction = -1 # rotate same direction to clock
else:
point_3rd = (left_eye_x, right_eye_y)
direction = 1 # rotate inverse direction of clock

# -----------------------
# find length of triangle edges

a = distance.findEuclideanDistance(np.array(left_eye), np.array(point_3rd))
b = distance.findEuclideanDistance(np.array(right_eye), np.array(point_3rd))
c = distance.findEuclideanDistance(np.array(right_eye), np.array(left_eye))
def get_alignment_angle_arctan2(left_eye, right_eye):
"""
The left_eye is the eye to the left of the viewer,
i.e., right eye of the person in the image.
The top-left point of the frame is (0, 0).
"""
return float(np.degrees(
np.arctan2(right_eye[1] - left_eye[1], right_eye[0] - left_eye[0])
))

# -----------------------

# apply cosine rule

if b != 0 and c != 0: # this multiplication causes division by zero in cos_a calculation
cos_a = (b * b + c * c - a * a) / (2 * b * c)
angle = np.arccos(cos_a) # angle in radian
angle = (angle * 180) / math.pi # radian to degree

# -----------------------
# rotate base image

if direction == -1:
angle = 90 - angle

img = Image.fromarray(img)
img = np.array(img.rotate(direction * angle))

# -----------------------

return img # return img anyway
def alignment_procedure(img, left_eye, right_eye):
angle = get_alignment_angle_arctan2(left_eye, right_eye)
img = Image.fromarray(img)
img = np.array(img.rotate(angle))
return img

0 comments on commit bd6393d

Please sign in to comment.