-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdata_organizer.py
188 lines (167 loc) · 6.14 KB
/
data_organizer.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
import numpy as np
from PIL import Image
import os, re, itertools
import pca, normalize
(TRAIN, VERIFY) = (.8, .1)
root = 'J:\\Applications & Tools\\CAS-PEAL-R1\\FRONTAL\\'
cache = 'storage/cache/'
normal = 'NormalJPG\\'
aging = 'AgingJPG\\'
bg = 'BackgroundJPG\\'
glasses = 'AccessoryJPG\\'
eye_data = 'FaceFP_2.txt'
try:
if (not os.path.exists('storage/data.npz')):
raise EnvironmentError("No data array!")
elif (not os.path.exists('storage/mask.png')):
raise LookupError("No mask image!")
elif (not os.path.exists('storage/datapairs.npz')):
raise LookupError("No data pairs!")
elif (not os.path.isdir(cache)):
raise LookupError("No cached faces!")
elif (not os.path.exists('storage/basis.npz')):
raise LookupError("No pca basis!")
mask_img = Image.open('storage/mask.png')
mask_img = (np.asarray(mask_img, dtype='float64') - 127.5) / 127.5
pca_basis_files = np.load('storage/basis.npz')
pca_basis = {'evalues': pca_basis_files['evalues'],
'efaces': pca_basis_files['efaces'],
'mu': pca_basis_files['mu']}
pca_basis_files.close()
except LookupError as e:
print(e)
print("Mask image, data pairs, cached faces and/or PCA basis not found. "
"Generate these before preparing any image pairs.")
except EnvironmentError as e:
print(e)
print("Data array not found. Generate it before generating the "
"other prerequisites")
def fixline(add, line):
line = line.strip()
(filename, coords_string) = line.split(' ')
face = int(re.search('([0-9]{6})', filename).group(1))
(xL, yL, xR, yR) = (int(i) for i in coords_string.split(' '))
return (face, add + filename + '.jpg', (xL, yL), (xR, yR))
def split3_prop(arr):
break1 = int(round(len(arr) * TRAIN))
break2 = int(round(len(arr) * (TRAIN + VERIFY)))
return [arr[:break1], arr[break1:break2], arr[break2:]]
def split3_breaks(arr, last_train, last_veri):
[arr1, arr2, arr3] = [[], [], []]
for i in range(len(arr)):
face = arr[i][0]
if (face <= last_train):
arr1.append(arr[i])
elif (face <= last_veri):
arr2.append(arr[i])
else:
arr3.append(arr[i])
return [arr1, arr2, arr3]
def create_dataset():
(normal_d, bg_d, aging_d, glasses_d) = \
(open(root + i + eye_data, 'r') for i in (normal, bg, aging, glasses))
(normal_path_coords, bg_path_coords,
aging_path_coords, glasses_path_coords) = ([], [], [], [])
for line in normal_d:
normal_path_coords.append(fixline(normal, line))
for line in aging_d:
aging_path_coords.append(fixline(aging, line))
for line in bg_d:
if ('T0_BR' in line):
bg_path_coords.append(fixline(bg, line))
for line in glasses_d:
if ('EN_A1' in line or 'EN_A2' in line):
glasses_path_coords.append(fixline(glasses, line))
normal_final = split3_prop(normal_path_coords)
last_train = normal_final[0][-1][0]
last_veri = normal_final[1][-1][0]
bg_final = split3_breaks(bg_path_coords, last_train, last_veri)
aging_final = split3_breaks(aging_path_coords, last_train, last_veri)
glasses_final = split3_breaks(glasses_path_coords, last_train, last_veri)
kwds = dict(zip(('normal', 'background', 'aging', 'glasses'),
(normal_final, bg_final, aging_final, glasses_final)))
np.savez('storage/data.npz', **kwds)
normal_d.close()
bg_d.close()
aging_d.close()
glasses_d.close()
def bisect_left(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if a[mid][0] < x: lo = mid+1
else: hi = mid
return lo
def bisect_right(a, x, lo=0, hi=None):
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
while lo < hi:
mid = (lo+hi)//2
if x < a[mid][0]: hi = mid
else: lo = mid+1
return lo
def find_all(arr, x):
return arr[bisect_left(arr, x):bisect_right(arr, x)]
def pset(arr): # Excluding empty set
double_gen = (itertools.combinations(arr, r)
for r in range(1, len(arr) + 1))
return [item for sublist in double_gen for item in sublist]
def create_datapairs():
files = np.load('storage/data.npz')
datapairs = [[], [], []]
for i in range(3):
for (face, path, eL, eR) in files['normal'][i]:
normalfaces = [(face, path, eL, eR)]
normalfaces += find_all(files['background'][i], face)
normalfaces += find_all(files['aging'][i], face)
normal_pset = pset(normalfaces)
glasses_pset = pset(find_all(files['glasses'][i], face))
datapairs_s = list(itertools.product(normal_pset, normal_pset))
datapairs_s += list(itertools.product(glasses_pset, normal_pset))
datapairs[i] += datapairs_s
kwds = dict(zip(('training', 'validation', 'testing'), datapairs))
np.savez('storage/datapairs.npz', **kwds)
files.close()
def create_cache():
files = np.load('storage/data.npz')
for collection in ['background', 'glasses','aging', 'normal']:
for i in range(3):
for (face, filepath, eyeLeft, eyeRight) in files[collection][i]:
img = Image.open(root + filepath).convert('L')
img = normalize.CropFace(img, eyeLeft, eyeRight)
img.save(cache + filepath)
files.close()
def prepare_imagepair(datapair):
(in_data, out_data) = datapair
in_img = np.zeros(
(normalize.FINAL_HEIGHT, normalize.FINAL_WIDTH),
dtype='float32')
out_img = np.zeros(
(normalize.FINAL_HEIGHT, normalize.FINAL_WIDTH),
dtype='float32')
for (_, filepath, eyeLeft, eyeRight) in in_data:
img = Image.open(cache + filepath)
img = np.asarray(img, dtype='float32') / (len(in_data))
in_img += img
for (_, filepath, eyeLeft, eyeRight) in out_data:
img = Image.open(cache + filepath)
img = np.asarray(img, dtype='float32') / (len(out_data))
out_img += img
in_pca = pca.proj_reconst(in_img, pca_basis['efaces'], pca_basis['mu'])
return (np.asarray([
(in_img - 127.5) / 127.5,
(in_pca - 127.5) / 127.5, mask_img], dtype='float32'),
np.asarray([(out_img - 127.5) / 127.5], dtype='float32'))
def encode_raw_input_image(raw_input_img):
in_img = np.asarray(raw_input_img)
in_pca = pca.proj_reconst(in_img, pca_basis['efaces'], pca_basis['mu'])
return np.asarray([
(in_img - 127.5) / 127.5,
(in_pca - 127.5) / 127.5, mask_img], dtype='float32')
def decode_output_image(encoded_output_img):
return Image.fromarray(encoded_output_img[0] * 127.5 + 127.5)