-
Notifications
You must be signed in to change notification settings - Fork 3
/
average_face.py
executable file
·367 lines (273 loc) · 11.1 KB
/
average_face.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 21 10:57:56 2020
@author: hardi
"""
#!/usr/bin/env python
#coding=utf8
# Copyright (c) 2016 Satya Mallick <spmallick@learnopencv.com>
# All rights reserved. No warranty, explicit or implicit, provided.
# Modifications and small fixes by Steffen Kühne, 2018
import os
import math
import sys
import cv2
import numpy as np
import dlib
import glob
from skimage import io
#
#if len(sys.argv) != 3:
# print(
# "Missing argument. Please provide a predictor model and the path to your image folder.\n"
# "A predictor model can be downloaded from: "
# "http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2\n"
# "Usage example: python extract.py shape_predictor_68_face_landmarks.dat ./images\n"
# )
# exit()
def extract_points(predictor_path, faces_folder_path):
# predictor_path = sys.argv[1]
# faces_folder_path = sys.argv[2]
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
for f in glob.glob(os.path.join(faces_folder_path, "*.jpg")):
print("Processing file: {}".format(f))
img = io.imread(f)
# Find the bounding boxes of each face.
dets = detector(img, 1)
print("Number of faces detected: {}".format(len(dets)))
for k, d in enumerate(dets):
results = []
print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
k, d.left(),
d.top(),
d.right(),
d.bottom()
))
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
print("Part 0: {}, Part 1: {} ...".format(
shape.part(0),
shape.part(1)
))
# Save each landmark as xy coordinate
for n in range(0, 68):
results.append(str(shape.part(n).x) + " " + str(shape.part(n).y))
with open(f + ".txt", "w") as output:
output.write("\n".join(results))
def average_face(path):
# Default size of the output image
w = 224
h = 224
# # Overwrite default image size
# if len(sys.argv) == 3:
# if str.isdigit(sys.argv[2]):
# w = int(sys.argv[2])
# if str.isdigit(sys.argv[3]):
# h = int(sys.argv[3])
#
# path = sys.argv[1]
# Read points for all images
all_points = read_points(path)
# Read all images
images = read_images(path)
# Eye corners
eyecorner_dst = [
(np.int(0.3 * w), np.int(h / 3)),
(np.int(0.7 * w), np.int(h / 3))
]
images_norm = []
points_norm = []
# Add boundary points for delaunay triangulation
boundary_pts = np.array([
(0, 0), (w / 2, 0), (w - 1, 0), (w - 1, h / 2),
(w - 1, h - 1), (w / 2, h - 1), (0, h - 1), (0, h / 2)
])
# Initialize location of average points to 0s
points_avg = np.array(
[(0, 0)] * (len(all_points[0]) + len(boundary_pts)),
np.float32()
)
num_images = len(images)
# Warp images and trasnform landmarks to output coordinate system,
# and find average of transformed landmarks.
for i in range(0, num_images):
points1 = all_points[i]
# Corners of the eye in input image
eyecorner_src = [all_points[i][36], all_points[i][45]]
# Compute similarity transform
tform = similarity_transform(eyecorner_src, eyecorner_dst)
# Apply similarity transformation
img = cv2.warpAffine(images[i], tform, (w, h))
# Apply similarity transform on points
points2 = np.reshape(np.array(points1), (68, 1, 2))
points = cv2.transform(points2, tform)
points = np.float32(np.reshape(points, (68, 2)))
# Append boundary points. Will be used in Delaunay Triangulation
points = np.append(points, boundary_pts, axis=0)
# Calculate location of average landmark points.
points_avg = points_avg + points / num_images
points_norm.append(points)
images_norm.append(img)
# Delaunay triangulation
rect = (0, 0, w, h)
tri = calculate_triangles(rect, np.array(points_avg))
# Output image
output = np.zeros((h, w, 3), np.float32())
# Warp input images to average image landmarks
for i in range(0, len(images_norm)):
img = np.zeros((h, w, 3), np.float32())
# Transform triangles one by one
for j in range(0, len(tri)):
t_in = []
t_out = []
for k in range(0, 3):
p_in = points_norm[i][tri[j][k]]
p_in = constrain_point(p_in, w, h)
p_out = points_avg[tri[j][k]]
p_out = constrain_point(p_out, w, h)
t_in.append(p_in)
t_out.append(p_out)
warp_triangle(images_norm[i], img, t_in, t_out)
# Add image intensities for averaging
output = output + img
# Divide by num_images to get average
output = output / num_images
# Display result
# cv2.imshow('image', output)
# cv2.waitKey(0)
# Saving result
cv2.imwrite( "average_face.jpg", 255 * output)
# return (255 * output)
# Read points from text files in directory
def read_points(path):
# Create an array of array of points.
points_array = []
# List all files in the directory and read points from text files one by one
for file_path in sorted(os.listdir(path)):
print(file_path)
if file_path.endswith('.txt'):
# Create an array of points.
points = []
# Read points from file_path
with open(os.path.join(path, file_path)) as f:
for line in f:
x, y = line.split()
points.append((int(x), int(y)))
# Store array of points
points_array.append(points)
return points_array
# Read all jpg images in folder.
def read_images(path):
#Create array of array of images.
images_array = []
#List all files in the directory and read points from text files one by one
for file_path in sorted(os.listdir(path)):
if file_path.endswith('.jpg'):
# Read image found.
img = cv2.imread(os.path.join(path, file_path))
# Convert to float_ing point
img = np.float32(img) / 255.0
# Add to array of images
images_array.append(img)
return images_array
# Compute similarity transform given two sets of two points.
# OpenCV requires 3 pairs of corresponding points.
# We are faking the third one.
def similarity_transform(in_points, out_points):
s60 = math.sin(60 * math.pi / 180)
c60 = math.cos(60 * math.pi / 180)
in_pts = np.copy(in_points).tolist()
out_pts = np.copy(out_points).tolist()
xin = c60 * (in_pts[0][0] - in_pts[1][0]) - s60 * \
(in_pts[0][1] - in_pts[1][1]) + in_pts[1][0]
yin = s60 * (in_pts[0][0] - in_pts[1][0]) + c60 * \
(in_pts[0][1] - in_pts[1][1]) + in_pts[1][1]
in_pts.append([np.int(xin), np.int(yin)])
xout = c60 * (out_pts[0][0] - out_pts[1][0]) - s60 * \
(out_pts[0][1] - out_pts[1][1]) + out_pts[1][0]
yout = s60 * (out_pts[0][0] - out_pts[1][0]) + c60 * \
(out_pts[0][1] - out_pts[1][1]) + out_pts[1][1]
out_pts.append([np.int(xout), np.int(yout)])
tform = cv2.estimateAffinePartial2D(np.array([in_pts]), np.array([out_pts]));
return tform[0]
# Check if a point is inside a rectangle
def rect_contains(rect, point):
if point[0] < rect[0]:
return False
elif point[1] < rect[1]:
return False
elif point[0] > rect[2]:
return False
elif point[1] > rect[3]:
return False
return True
# Calculate Delanauy triangles
def calculate_triangles(rect, points):
# Create subdiv
subdiv = cv2.Subdiv2D(rect)
# Insert points into subdiv
for p in points:
subdiv.insert((p[0], p[1]))
# List of triangles. Each triangle is a list of 3 points ( 6 numbers )
triangle_list = subdiv.getTriangleList()
# Find the indices of triangles in the points array
delaunay_tri = []
for t in triangle_list:
pt = []
pt.append((t[0], t[1]))
pt.append((t[2], t[3]))
pt.append((t[4], t[5]))
pt1 = (t[0], t[1])
pt2 = (t[2], t[3])
pt3 = (t[4], t[5])
if rect_contains(rect, pt1) and rect_contains(rect, pt2) and rect_contains(rect, pt3):
ind = []
for j in range(0, 3):
for k in range(0, len(points)):
if abs(pt[j][0] - points[k][0]) < 1.0 and abs(pt[j][1] - points[k][1]) < 1.0:
ind.append(k)
if len(ind) == 3:
delaunay_tri.append((ind[0], ind[1], ind[2]))
return delaunay_tri
def constrain_point(p, w, h):
p = (min(max(p[0], 0), w - 1), min(max(p[1], 0), h - 1))
return p
# Apply affine transform calculated using src_tri and dst_tri to src and
# output an image of size.
def apply_affine_transform(src, src_tri, dst_tri, size):
# Given a pair of triangles, find the affine transform.
warp_mat = cv2.getAffineTransform(np.float32(src_tri), np.float32(dst_tri))
# Apply the Affine Transform just found to the src image
dst = cv2.warpAffine(src, warp_mat, (size[0], size[1]), None,
flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT_101)
return dst
# Warps and alpha blends triangular regions from img1 and img2 to img
def warp_triangle(img1, img2, t1, t2):
# Find bounding rectangle for each triangle
r1 = cv2.boundingRect(np.float32([t1]))
r2 = cv2.boundingRect(np.float32([t2]))
# Offset points by left top corner of the respective rectangles
t1_rect = []
t2_rect = []
t2_rect_int = []
for i in range(0, 3):
t1_rect.append(((t1[i][0] - r1[0]), (t1[i][1] - r1[1])))
t2_rect.append(((t2[i][0] - r2[0]), (t2[i][1] - r2[1])))
t2_rect_int.append(((t2[i][0] - r2[0]), (t2[i][1] - r2[1])))
# Get mask by filling triangle
mask = np.zeros((r2[3], r2[2], 3), dtype=np.float32)
cv2.fillConvexPoly(mask, np.int32(t2_rect_int), (1.0, 1.0, 1.0), 16, 0)
# Apply warpImage to small rectangular patches
img1_rect = img1[r1[1]:r1[1] + r1[3], r1[0]:r1[0] + r1[2]]
size = (r2[2], r2[3])
img2_rect = apply_affine_transform(img1_rect, t1_rect, t2_rect, size)
img2_rect = img2_rect * mask
# Copy triangular region of the rectangular patch to the output image
img2[r2[1]:r2[1] + r2[3], r2[0]:r2[0] + r2[2]] = img2[r2[1]:r2[1] + r2[3], r2[0]:r2[0] + r2[2]] * ((1.0, 1.0, 1.0) - mask)
img2[r2[1]:r2[1] + r2[3], r2[0]:r2[0] + r2[2]] = img2[r2[1]:r2[1] + r2[3], r2[0]:r2[0] + r2[2]] + img2_rect
#if __name__ == '__main__':
#predictor_path
#faces_folder_path =
#extract_points(predictor_path, faces_folder_path)
#average_face(path)