-
Notifications
You must be signed in to change notification settings - Fork 5
/
detect_align.py
273 lines (240 loc) · 10.8 KB
/
detect_align.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 13 17:50:05 2020
@author: lps
Reference: https://zhuanlan.zhihu.com/p/55479744
"""
import math
import cv2
from PIL import Image, ImageDraw
from matplotlib.pyplot import imshow
import face_recognition
from collections import defaultdict
import numpy as np
import matplotlib.pyplot as plt
from skimage import transform as trans
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[:, :, [2,1,0]])
draw = ImageDraw.Draw(origin_img)
for facial_feature in landmarks.keys():
draw.point(landmarks[facial_feature])
imshow(origin_img)
def preprocess(img, bbox=None, landmark=None, **kwargs):
if isinstance(img, str):
img = read_image(img, **kwargs)
M = None
image_size = []
str_image_size = kwargs.get('image_size', '')
if len(str_image_size)>0:
image_size = [int(x) for x in str_image_size.split(',')]
if len(image_size)==1:
image_size = [image_size[0], image_size[0]]
assert len(image_size)==2
# assert image_size[0]==256
# assert image_size[0]==256 or image_size[1]==96
if landmark is not None:
assert len(image_size)==2
""" image size=128 """
# src = np.array([
# [30.2946, 51.6963],
# [65.5318, 51.5014],
# [48.0252, 71.7366],
# [33.5493, 92.3655],
# [62.7299, 92.2041] ], dtype=np.float32 )
""" image size=256 """
src = np.array([
[68.57+20, 115.6963+10],
[149.78+20, 115.5014+10],
[109.714+20, 162.736-5],
[76.6834+20, 186.3655-8],
[143.3826+20, 186.2041-8] ], dtype=np.float32 )
if image_size[1]==112:
src[:,0] += 8.0
dst = landmark.astype(np.float32)
tform = trans.SimilarityTransform()
tform.estimate(dst, src)
M = tform.params[0:2,:]
#M = cv2.estimateRigidTransform( dst.reshape(1,5,2), src.reshape(1,5,2), False)
if M is None:
if bbox is None: #use center crop
det = np.zeros(4, dtype=np.int32)
det[0] = int(img.shape[1]*0.0625)
det[1] = int(img.shape[0]*0.0625)
det[2] = img.shape[1] - det[0]
det[3] = img.shape[0] - det[1]
else:
det = bbox
margin = kwargs.get('margin', 44)
bb = np.zeros(4, dtype=np.int32)
bb[0] = np.maximum(det[0]-margin/2, 0)
bb[1] = np.maximum(det[1]-margin/2, 0)
bb[2] = np.minimum(det[2]+margin/2, img.shape[1])
bb[3] = np.minimum(det[3]+margin/2, img.shape[0])
ret = img[bb[1]:bb[3],bb[0]:bb[2],:]
if len(image_size)>0:
ret = cv2.resize(ret, (image_size[1], image_size[0]))
return ret
else: #do align using landmark
assert len(image_size)==2
warped = cv2.warpAffine(img[:, :, [2,1,0]],M,(image_size[1],image_size[0]), borderValue = 0.0)
return warped
def process_data(image):
"""
image: random shape array
return: warped 256*256 array
"""
ldm = face_recognition.face_landmarks(image, model='large')[0]
ldm = np.concatenate(((np.array(ldm['left_eye'][0])+np.array(ldm['left_eye'][3]))/2,
(np.array(ldm['right_eye'][0])+np.array(ldm['right_eye'][3]))/2,
ldm['nose_bridge'][-1],
ldm['top_lip'][0],
ldm['bottom_lip'][0]
)).reshape(-1,2)
warped = preprocess(image, bbox=None, landmark=ldm,image_size='256')
return warped[:, :, [2,1,0]]
# TEST CODE
if __name__=='__main__':
# load image
img_name = '/media/a/HDD/lyfeng/Face_Proj/vgg_face_dataset/images/Abbie_Cornish/00000066.jpg'
image_array = cv2.imread(img_name)
face_landmarks_list = face_recognition.face_landmarks(image_array, model='large')
ldm = face_landmarks_list[0]
ldm = np.concatenate(((np.array(ldm['left_eye'][0])+np.array(ldm['left_eye'][3]))/2,
(np.array(ldm['right_eye'][0])+np.array(ldm['right_eye'][3]))/2,
ldm['nose_bridge'][-1],
ldm['top_lip'][0],
ldm['bottom_lip'][0]
)).reshape(-1,2)
warped = preprocess(image_array, bbox=None, landmark=ldm,image_size='256')
# plt.imsave('256.jpg',warped)
plt.imshow(warped)