-
Notifications
You must be signed in to change notification settings - Fork 3
/
PosReshape.py
138 lines (104 loc) · 3.39 KB
/
PosReshape.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 numpy as np
import dlib
import cv2
import os
def get_params(img, d, l_eyes, r_eyes, mouth, nose):
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
l_eyes[0] += shape.part(40).x
l_eyes[1] += shape.part(40).y
r_eyes[0] += shape.part(43).x
r_eyes[1] += shape.part(43).y
mouth[0] += shape.part(49).x + shape.part(55).x
mouth[1] += shape.part(49).y + shape.part(55).y
nose[0] += shape.part(28).x + shape.part(31).x
nose[1] += shape.part(28).y + shape.part(31).y
return l_eyes, r_eyes, mouth, nose
def correct_width(img, correction, start):
if(correction < 0):
add = np.zeros((img.shape[0], 1, 3))
print(img.shape)
print(add.shape)
if(start == 0):
img = np.hstack((add, img))
else:
img = np.hstack((img, add))
img = cv2.resize(img, (img.shape[0], img.shape[1] - 1))
else:
if(start == 0):
img = img[:, 0:img.shape[1] - 2]
else:
img = img[:, 1:img.shape[1] - 1]
img = cv2.resize(img, (img.shape[0], img.shape[1] + 1))
return img
def correctposx(img, correction):
if(correction < 0):
add = np.zeros((img.shape[0], 1, 3))
img = img[:, 0:img.shape[1] - 1]
img = np.hstack((add, img))
# shift left
else:
add = np.zeros((img.shape[0], 1, 3))
img = img[:, 1:img.shape[1]]
img = np.hstack((img, add))
return img
def correctposy(img, correction):
if(correction < 0):
add = np.zeros((1,img.shape[1], 3))
img = img[0:img.shape[0] - 1, :]
img = np.vstack((add, img))
# shift down
else:
add = np.zeros((1,img.shape[1], 3))
img = img[ 1:img.shape[0], :]
img = np.vstack((img, add))
return img
tada = 'Positional Pictures/'
tads = os.listdir(tada)
for tad in tads:
base = tad + '/'
print(base)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('dlibcascades/shape_predictor_68_face_landmarks.dat')
images = os.listdir(tada + base)
l_eyes = [0, 0]
r_eyes = [0, 0]
mouth = [0, 0]
nose = [0, 0]
for image in images:
img = cv2.imread(tada + base + image)
dets = detector(img, 1)
for k, d in enumerate(dets):
l_eyes, r_eyes, mouth, nose = get_params(img, d, l_eyes, r_eyes, mouth, nose)
print("Training via " + image)
# Average calculation
l_eyes = [int(x/len(images)) for x in l_eyes]
r_eyes = [int(x/len(images)) for x in r_eyes]
mouth = [int(0.5*x/len(images)) for x in mouth]
nose = [int(0.5*x/len(images)) for x in nose]
print(l_eyes, r_eyes, mouth, nose)
lr_mean = [(l_eyes[0] + r_eyes[0])/2.0 ,(l_eyes[1] + r_eyes[1])/2.0]
for image in images:
print("Working for " + image)
img = cv2.imread(tada + base + image)
dets = detector(img, 1)
rows,cols,ch = img.shape
for k, d in enumerate(dets):
# Get the landmarks/parts for the face in box d.
shape = predictor(img, d)
sl_eyes = [0, 0]
sr_eyes = [0, 0]
s_mouth = [0, 0]
s_nose = [0, 0]
sl_eyes, sr_eyes, s_mouth, s_nose = get_params(img, d, sl_eyes, sr_eyes, s_mouth, s_nose)
s_mouth = [int(0.5*x) for x in s_mouth]
pts1 = np.float32([sl_eyes, sr_eyes, s_mouth])
pts2 = np.float32([l_eyes, r_eyes, mouth])
# for i in range(68):
# # cv2.circle(img,(shape.part(i).x,shape.part(i).y),4,(0,0,255))
# cv2.circle(img,(s_mouth[0],s_mouth[1]),4,(0,0,255))
print(sl_eyes, sr_eyes, s_mouth)
print(l_eyes, r_eyes, mouth)
M = cv2.getAffineTransform(pts1,pts2)
dst = cv2.warpAffine(img,M,(cols,rows))
cv2.imwrite("Posi/" + base + image, dst)