Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify face alignment procedure #863

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -82,47 +80,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
Loading