forked from ForrestPi/FaceProjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaceAligner.py
176 lines (163 loc) · 7.79 KB
/
FaceAligner.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding:utf-8 _*-
#https://zhuanlan.zhihu.com/p/55479744
"""
@author: yiling
@date: 2020/1/2
@file: preprocessing.py
@description: align and crop face, transfer landmarks accordingly
"""
import math
import cv2
from PIL import Image, ImageDraw
from matplotlib import pyplot as plt
from matplotlib.pyplot import imshow
#import face_recognition
from mtcnn.detector import detect_faces
from collections import defaultdict
import numpy as np
def detect_landmark(image_array, model_type="large"):
""" return landmarks of a given image array
:param image_array: numpy array of a single image
:param model_type: 'large' returns 68 landmarks; 'small' return 5 landmarks
:return: dict of landmarks for facial parts as keys and tuple of coordinates as values
"""
face_landmarks_list = face_recognition.face_landmarks(image_array, model=model_type)
face_landmarks_list = face_landmarks_list[0]
return face_landmarks_list
def align_face(image_array, landmarks):
""" align faces according to eyes position
:param image_array: numpy array of a single image
:param landmarks: dict of landmarks for facial parts as keys and tuple of coordinates as values
:return:
rotated_img: numpy array of aligned image
eye_center: tuple of coordinates for eye center
angle: degrees of rotation
"""
# get list landmarks of left and right eye
left_eye = landmarks['left_eye']
right_eye = landmarks['right_eye']
# calculate the mean point of landmarks of left and right eye
left_eye_center = np.mean(left_eye, axis=0).astype("int")
right_eye_center = np.mean(right_eye, axis=0).astype("int")
# compute the angle between the eye centroids
dy = right_eye_center[1] - left_eye_center[1]
dx = right_eye_center[0] - left_eye_center[0]
# compute angle between the line of 2 centeroids and the horizontal line
angle = math.atan2(dy, dx) * 180. / math.pi
# calculate the center of 2 eyes
eye_center = ((left_eye_center[0] + right_eye_center[0]) // 2,
(left_eye_center[1] + right_eye_center[1]) // 2)
# at the eye_center, rotate the image by the angle
rotate_matrix = cv2.getRotationMatrix2D(eye_center, angle, scale=1)
rotated_img = cv2.warpAffine(image_array, rotate_matrix, (image_array.shape[1], image_array.shape[0]))
return rotated_img, eye_center, angle
def rotate(origin, point, angle, row):
""" rotate coordinates in image coordinate system
:param origin: tuple of coordinates,the rotation center
:param point: tuple of coordinates, points to rotate
:param angle: degrees of rotation
:param row: row size of the image
:return: rotated coordinates of point
"""
x1, y1 = point
x2, y2 = origin
y1 = row - y1
y2 = row - y2
angle = math.radians(angle)
x = x2 + math.cos(angle) * (x1 - x2) - math.sin(angle) * (y1 - y2)
y = y2 + math.sin(angle) * (x1 - x2) + math.cos(angle) * (y1 - y2)
y = row - y
return int(x), int(y)
def rotate_landmarks(landmarks, eye_center, angle, row):
""" rotate landmarks to fit the aligned face
:param landmarks: dict of landmarks for facial parts as keys and tuple of coordinates as values
:param eye_center: tuple of coordinates for eye center
:param angle: degrees of rotation
:param row: row size of the image
:return: rotated_landmarks with the same structure with landmarks, but different values
"""
rotated_landmarks = defaultdict(list)
for facial_feature in landmarks.keys():
for landmark in landmarks[facial_feature]:
rotated_landmark = rotate(origin=eye_center, point=landmark, angle=angle, row=row)
rotated_landmarks[facial_feature].append(rotated_landmark)
return rotated_landmarks
def corp_face(image_array, size, landmarks):
""" crop face according to eye,mouth and chin position
:param image_array: numpy array of a single image
:param size: single int value, size for w and h after crop
:param landmarks: dict of landmarks for facial parts as keys and tuple of coordinates as values
:return:
cropped_img: numpy array of cropped image
left, top: left and top coordinates of cropping
"""
x_min = np.min(landmarks['chin'], axis=0)[0]
x_max = np.max(landmarks['chin'], axis=0)[0]
x_center = (x_max - x_min) / 2 + x_min
left, right = (x_center - size / 2, x_center + size / 2)
eye_landmark = landmarks['left_eye'] + landmarks['right_eye']
eye_center = np.mean(eye_landmark, axis=0).astype("int")
lip_landmark = landmarks['top_lip'] + landmarks['bottom+lip']
lip_center = np.mean(lip_landmark, axis=0).astype("int")
mid_part = lip_center[1] - eye_center[1]
top, bottom = eye_center[1] - (size - mid_part) / 2, lip_center[1] + (size - mid_part) / 2
pil_img = Image.fromarray(image_array)
left, top, right, bottom = [int(i) for i in [left, top, right, bottom]]
cropped_img = pil_img.crop((left, top, right, bottom))
cropped_img = np.array(cropped_img)
return cropped_img, left, top
def transfer_landmark(landmarks, left, top):
"""transfer landmarks to fit the cropped face
:param landmarks: dict of landmarks for facial parts as keys and tuple of coordinates as values
:param left: left coordinates of cropping
:param top: top coordinates of cropping
:return: transferred_landmarks with the same structure with landmarks, but different values
"""
transferred_landmarks = defaultdict(list)
for facial_feature in landmarks.keys():
for landmark in landmarks[facial_feature]:
transferred_landmark = (landmark[0] - left, landmark[1] - top)
transferred_landmarks[facial_feature].append(transferred_landmark)
return transferred_landmarks
def face_preprocess(image, landmark_model_type='large', crop_size=140):
""" for a given image, do face alignment and crop face
:param image: numpy array of a single image
:param landmark_model_type: 'large' returns 68 landmarks; 'small' return 5 landmarks
:param crop_size: ingle int value, size for w and h after crop
:return:
cropped_face: image array with face aligned and cropped
transferred_landmarks: landmarks that fit cropped_face
"""
# detect landmarks
face_landmarks_dict = detect_landmark(image_array=image, model_type=landmark_model_type)
# rotate image array to align face
aligned_face, eye_center, angle = align_face(image_array=image, landmarks=face_landmarks_dict)
# rotate landmarks coordinates to fit the aligned face
rotated_landmarks = rotate_landmarks(landmarks=face_landmarks_dict,
eye_center=eye_center, angle=angle, row=image.shape[0])
# crop face according to landmarks
cropped_face, left, top = corp_face(image_array=aligned_face, size=crop_size, landmarks=rotated_landmarks)
# transfer landmarks to fit the cropped face
transferred_landmarks = transfer_landmark(landmarks=rotated_landmarks, left=left, top=top)
return cropped_face, transferred_landmarks
def visualize_landmark(image_array, landmarks):
""" plot landmarks on image
:param image_array: numpy array of a single image
:param landmarks: dict of landmarks for facial parts as keys and tuple of coordinates as values
:return: plots of images with landmarks on
"""
origin_img = Image.fromarray(image_array)
draw = ImageDraw.Draw(origin_img)
for facial_feature in landmarks.keys():
draw.point(landmarks[facial_feature])
imshow(origin_img)
if __name__=='__main__':
# load image
img_name = './lena.jpg'
image_array = cv2.imread(img_name)
# preprocess the face image
face,landmarks = face_preprocess(image = image_array,
landmark_model_type='large',
crop_size=140)
visualize_landmark(image_array=face,landmarks=landmarks)
plt.show()