-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfacemash.py
114 lines (87 loc) · 2.84 KB
/
facemash.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
import cv2
import dlib
import sys, os, time, random
import numpy as np
detector = dlib.get_frontal_face_detector()
PREDICTOR_PATH = "shape_predictor_68_face_landmarks.dat"
predictor = dlib.shape_predictor(PREDICTOR_PATH)
def get_img(path):
print "[+] Opened image from:", path
return cv2.imread(path)
def get_rects(img):
rects = detector(img)
print "[+] Number of faces found:", len(rects)
return rects
def get_landmarks(img, rect):
return np.matrix([[p.x, p.y] for p in predictor(img, rect).parts()])
# https://matthewearl.github.io/2015/07/28/switching-eds-with-python/
def transformation_from_points(points1, points2):
points1 = points1.astype(np.float64)
points2 = points2.astype(np.float64)
c1 = np.mean(points1, axis=0)
c2 = np.mean(points2, axis=0)
points1 -= c1
points2 -= c2
s1 = np.std(points1)
s2 = np.std(points2)
points1 /= s1
points2 /= s2
U, S, Vt = np.linalg.svd(points1.T * points2)
R = (U * Vt).T
return np.vstack([np.hstack(((s2 / s1) * R,
c2.T - (s2 / s1) * R * c1.T)),
np.matrix([0., 0., 1.])])
def warp_im(im, M, dshape):
output_im = np.ones(dshape, dtype=im.dtype)*255
cv2.warpAffine(im,
M[:2],
(dshape[1], dshape[0]),
dst=output_im,
borderMode=cv2.BORDER_TRANSPARENT,
flags=cv2.WARP_INVERSE_MAP)
return output_im
folder = sys.argv[1]
count = 0
for img_file in os.listdir(folder):
if img_file.startswith(".DS"):
continue
path = folder + "/" + img_file
print count, ":", path
if count == 0:
# get our reference image
ref_img = get_img(path)
rects = get_rects(ref_img)
if len(rects) > 0:
ref_rect = rects[0]
else:
continue
ref_landmarks = get_landmarks(ref_img, ref_rect)
average = ref_img.copy()
cv2.imshow('average', average)
# cv2.waitKey(0)
else:
# do the thing
img = get_img(path)
rects = get_rects(img)
if len(rects) > 0:
rect = rects[0]
else:
continue
landmarks = get_landmarks(img, rect)
transformation_matrix = transformation_from_points(ref_landmarks, landmarks)
warped_img = warp_im(img, transformation_matrix, ref_img.shape)
average = average/255.0
warped_img = warped_img/255.0
average = ((average * count) + warped_img) / (count+1)
average = average*255.0
if random.random()>0.5:
average = np.ceil(average)
else:
average = np.floor(average)
average = average.astype('uint8')
cv2.imshow('average', average)
cv2.waitKey(1)
# if count > 1:
# break
count += 1
cv2.waitKey(0)