-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacify.py
138 lines (120 loc) · 4.09 KB
/
facify.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
import sys, math, Image
import os
import os.path
import numpy as np
import cv,cv2
import dlib
import argparse
parser = argparse.ArgumentParser(description='Runs the GAN.')
parser.add_argument('--directory', type=str)
parser.add_argument('--output', type=str)
args = parser.parse_args()
DIR = args.directory
#DIR = "../data/celeb/"
#DIR = "/home/martyn/Downloads/imdb/imdb/81/"
def Distance(p1,p2):
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
return math.sqrt(dx*dx+dy*dy)
def ScaleRotateTranslate(image, angle, center = None, new_center = None, scale = None, resample=Image.BICUBIC):
if (scale is None) and (center is None):
return image.rotate(angle=angle, resample=resample)
nx,ny = x,y = center
sx=sy=1.0
if new_center:
(nx,ny) = new_center
if scale:
(sx,sy) = (scale, scale)
cosine = math.cos(angle)
sine = math.sin(angle)
a = cosine/sx
b = sine/sx
c = x-nx*a-ny*b
d = -sine/sy
e = cosine/sy
f = y-nx*d-ny*e
return image.transform(image.size, Image.AFFINE, (a,b,c,d,e,f), resample=resample)
def CropFace(image, eye_left=(0,0), eye_right=(0,0), offset_pct=(0.2,0.2), dest_sz = (70,70)):
# calculate offsets in original image
offset_h = math.floor(float(offset_pct[0])*dest_sz[0])
offset_v = math.floor(float(offset_pct[1])*dest_sz[1])
# get the direction
eye_direction = (eye_right[0] - eye_left[0], eye_right[1] - eye_left[1])
# calc rotation angle in radians
dist = Distance(eye_left, eye_right)
# calculate the reference eye-width
reference = dest_sz[0] - 2.0*offset_h
# scale factor
scale = float(dist)/float(reference)
rotation = -math.atan2(float(eye_direction[1]),float(eye_direction[0]))
# rotate original around the left eye
image = ScaleRotateTranslate(image, center=(np.array(eye_left)+np.array(eye_right))/2, angle=rotation)
# crop the rotated image
crop_xy = (eye_left[0] - scale*offset_h, eye_left[1] - scale*offset_v)
crop_size = (dest_sz[0]*scale, dest_sz[1]*scale)
if(crop_xy[0] < 0 or crop_xy[1] < 0 or int(crop_xy[0]+crop_size[0]) > image.size[0] or int(crop_xy[1]+crop_size[1]) > image.size[1]):
print("CROP OVER", crop_xy, crop_size, image.size)
return None
image = image.crop((int(crop_xy[0]), int(crop_xy[1]), int(crop_xy[0]+crop_size[0]), int(crop_xy[1]+crop_size[1])))
# resize it
image = image.resize(dest_sz, Image.ANTIALIAS)
return image
def get_eyes(image):
predictor = dlib.shape_predictor("./shape_predictor_68_face_landmarks.dat")
detector = dlib.get_frontal_face_detector()
img = np.array(image)
try:
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
except:
return None
dets = detector(gray, 1)
faces = []
for k,d in enumerate(dets):
shape = predictor(gray, d)
s1 = shape.part(40)
s2 = shape.part(47)
l = np.array((int(s1.x),s1.y))
r = np.array((int(s2.x),s2.y))
threshold = 60
if(abs((r-l)[0])<threshold):
continue
print("appending face")
faces.append([l,r])
print('--')
return faces
good=0
bad=0
for file in os.listdir(DIR):
name = file.split(".")
if(len(name) > 1):
name = name[-2]
else:
name = name[-1]
destination = args.output+"/"+name + ".jpg"
if os.path.isfile(destination.split(".")[-2]+".1"+destination.split(".")[-1]):
print("Skip", destination)
continue
input_file = DIR+file
try:
image = Image.open(input_file)
except:
print("Skipping ", input_file)
continue
eyes = get_eyes(image)
if eyes:
e_count=0
for e in eyes:
e_count+=1
offset = (0.4,0.51)
newdestination = destination.split(".")[-2] + "."+str(e_count)+"." + destination.split(".")[-1]
print("Cropping to "+newdestination)
res = CropFace(image, eye_left=e[0], eye_right=e[1], offset_pct=offset, dest_sz=(256,256))
if(res == None):
bad+=1
else:
res.save(newdestination)
good+=1
else:
bad+=1
print("Skipping", name)
print("Good/bad ratio", float(good)/(bad+good))