-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_utils.py
61 lines (42 loc) · 1.69 KB
/
my_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import numpy as np
import math
from PIL import Image
from deepface.commons import distance
# this function aligns given face in img based on left and right eye coordinates
def alignment_procedure(img, left_eye, right_eye, bbox):
# Crop Face
x, y, w, h = bbox
img_roi = img[y:y+h, x:x+w]
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))
# -----------------------
# 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))
# Image ROI
img_roi = Image.fromarray(img_roi)
img_roi = np.array(img_roi.rotate(direction * angle))
# -----------------------
return img_roi # return img anyway