-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathface_image.py
executable file
·293 lines (269 loc) · 9.11 KB
/
face_image.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
from easydict import EasyDict as edict
import os
import json
import numpy as np
def load_property(data_dir):
prop = edict()
for line in open(os.path.join(data_dir, 'property')):
vec = line.strip().split(',')
assert len(vec)==3
prop.num_classes = int(vec[0])
prop.image_size = [int(vec[1]), int(vec[2])]
return prop
def get_dataset_webface(input_dir):
clean_list_file = input_dir+"_clean_list.txt"
ret = []
for line in open(clean_list_file, 'r'):
vec = line.strip().split()
assert len(vec)==2
fimage = edict()
fimage.id = vec[0].replace("\\", '/')
fimage.classname = vec[1]
fimage.image_path = os.path.join(input_dir, fimage.id)
ret.append(fimage)
return ret
def get_dataset_celeb(input_dir):
clean_list_file = input_dir+"_clean_list.txt"
ret = []
dir2label = {}
for line in open(clean_list_file, 'r'):
line = line.strip()
if not line.startswith('./m.'):
continue
line = line[2:]
vec = line.split('/')
assert len(vec)==2
if vec[0] in dir2label:
label = dir2label[vec[0]]
else:
label = len(dir2label)
dir2label[vec[0]] = label
fimage = edict()
fimage.id = line
fimage.classname = str(label)
fimage.image_path = os.path.join(input_dir, fimage.id)
ret.append(fimage)
return ret
def _get_dataset_celeb(input_dir):
list_file = input_dir+"_original_list.txt"
ret = []
for line in open(list_file, 'r'):
vec = line.strip().split()
assert len(vec)==2
fimage = edict()
fimage.id = vec[0]
fimage.classname = vec[1]
fimage.image_path = os.path.join(input_dir, fimage.id)
ret.append(fimage)
return ret
def get_dataset_facescrub(input_dir):
ret = []
label = 0
person_names = []
for person_name in os.listdir(input_dir):
person_names.append(person_name)
person_names = sorted(person_names)
for person_name in person_names:
subdir = os.path.join(input_dir, person_name)
if not os.path.isdir(subdir):
continue
for _img in os.listdir(subdir):
fimage = edict()
fimage.id = os.path.join(person_name, _img)
fimage.classname = str(label)
fimage.image_path = os.path.join(subdir, _img)
fimage.landmark = None
fimage.bbox = None
ret.append(fimage)
label += 1
return ret
def get_dataset_megaface(input_dir):
ret = []
label = 0
for prefixdir in os.listdir(input_dir):
_prefixdir = os.path.join(input_dir, prefixdir)
for subdir in os.listdir(_prefixdir):
_subdir = os.path.join(_prefixdir, subdir)
if not os.path.isdir(_subdir):
continue
for img in os.listdir(_subdir):
if not img.endswith('.jpg.jpg') and img.endswith('.jpg'):
fimage = edict()
fimage.id = os.path.join(prefixdir, subdir, img)
fimage.classname = str(label)
fimage.image_path = os.path.join(_subdir, img)
json_file = fimage.image_path+".json"
data = None
fimage.bbox = None
fimage.landmark = None
if os.path.exists(json_file):
with open(json_file, 'r') as f:
data = f.read()
data = json.loads(data)
assert data is not None
if 'bounding_box' in data:
fimage.bbox = np.zeros( (4,), dtype=np.float32 )
bb = data['bounding_box']
fimage.bbox[0] = bb['x']
fimage.bbox[1] = bb['y']
fimage.bbox[2] = bb['x']+bb['width']
fimage.bbox[3] = bb['y']+bb['height']
#print('bb')
if 'landmarks' in data:
landmarks = data['landmarks']
if '1' in landmarks and '0' in landmarks and '2' in landmarks:
fimage.landmark = np.zeros( (3,2), dtype=np.float32 )
fimage.landmark[0][0] = landmarks['1']['x']
fimage.landmark[0][1] = landmarks['1']['y']
fimage.landmark[1][0] = landmarks['0']['x']
fimage.landmark[1][1] = landmarks['0']['y']
fimage.landmark[2][0] = landmarks['2']['x']
fimage.landmark[2][1] = landmarks['2']['y']
#print('lm')
ret.append(fimage)
label+=1
return ret
def get_dataset_fgnet(input_dir):
ret = []
label = 0
for subdir in os.listdir(input_dir):
_subdir = os.path.join(input_dir, subdir)
if not os.path.isdir(_subdir):
continue
for img in os.listdir(_subdir):
if img.endswith('.JPG'):
fimage = edict()
fimage.id = os.path.join(_subdir, img)
fimage.classname = str(label)
fimage.image_path = os.path.join(_subdir, img)
json_file = fimage.image_path+".json"
data = None
fimage.bbox = None
fimage.landmark = None
if os.path.exists(json_file):
with open(json_file, 'r') as f:
data = f.read()
data = json.loads(data)
assert data is not None
if 'bounding_box' in data:
fimage.bbox = np.zeros( (4,), dtype=np.float32 )
bb = data['bounding_box']
fimage.bbox[0] = bb['x']
fimage.bbox[1] = bb['y']
fimage.bbox[2] = bb['x']+bb['width']
fimage.bbox[3] = bb['y']+bb['height']
#print('bb')
if 'landmarks' in data:
landmarks = data['landmarks']
if '1' in landmarks and '0' in landmarks and '2' in landmarks:
fimage.landmark = np.zeros( (3,2), dtype=np.float32 )
fimage.landmark[0][0] = landmarks['1']['x']
fimage.landmark[0][1] = landmarks['1']['y']
fimage.landmark[1][0] = landmarks['0']['x']
fimage.landmark[1][1] = landmarks['0']['y']
fimage.landmark[2][0] = landmarks['2']['x']
fimage.landmark[2][1] = landmarks['2']['y']
#print('lm')
#fimage.landmark = None
ret.append(fimage)
label+=1
return ret
def get_dataset_ytf(input_dir):
ret = []
label = 0
person_names = []
for person_name in os.listdir(input_dir):
person_names.append(person_name)
person_names = sorted(person_names)
for person_name in person_names:
_subdir = os.path.join(input_dir, person_name)
if not os.path.isdir(_subdir):
continue
for _subdir2 in os.listdir(_subdir):
_subdir2 = os.path.join(_subdir, _subdir2)
if not os.path.isdir(_subdir2):
continue
_ret = []
for img in os.listdir(_subdir2):
fimage = edict()
fimage.id = os.path.join(_subdir2, img)
fimage.classname = str(label)
fimage.image_path = os.path.join(_subdir2, img)
fimage.bbox = None
fimage.landmark = None
_ret.append(fimage)
ret += _ret
label+=1
return ret
def get_dataset_clfw(input_dir):
ret = []
label = 0
for img in os.listdir(input_dir):
fimage = edict()
fimage.id = img
fimage.classname = str(0)
fimage.image_path = os.path.join(input_dir, img)
fimage.bbox = None
fimage.landmark = None
ret.append(fimage)
return ret
def get_dataset_common(input_dir, min_images = 1):
ret = []
label = 0
person_names = []
for person_name in os.listdir(input_dir):
person_names.append(person_name)
person_names = sorted(person_names)
for person_name in person_names:
_subdir = os.path.join(input_dir, person_name)
if not os.path.isdir(_subdir):
continue
_ret = []
for img in os.listdir(_subdir):
fimage = edict()
fimage.id = os.path.join(person_name, img)
fimage.classname = str(label)
fimage.image_path = os.path.join(_subdir, img)
fimage.bbox = None
fimage.landmark = None
_ret.append(fimage)
if len(_ret)>=min_images:
ret += _ret
label+=1
return ret
def get_dataset_trillionpairs(input_dir):
ret = []
label = 0
for prefixdir in os.listdir(input_dir):
_prefixdir = os.path.join(input_dir, prefixdir)
for subdir in os.listdir(_prefixdir):
_subdir = os.path.join(_prefixdir, subdir)
if not os.path.isdir(_subdir):
continue
for img in os.listdir(_subdir):
if img.endswith('.jpg'):
fimage = edict()
fimage.id = os.path.join(prefixdir, subdir, img)
fimage.classname = str(label)
fimage.image_path = os.path.join(_subdir, img)
ret.append(fimage)
label+=1
return ret
def get_dataset(name, input_dir):
if name=='webface' or name=='lfw' or name=='vgg':
return get_dataset_common(input_dir)
if name=='celeb':
return get_dataset_celeb(input_dir)
if name=='facescrub':
return get_dataset_facescrub(input_dir)
if name=='megaface':
return get_dataset_megaface(input_dir)
if name=='trillionpairs':
return get_dataset_trillionpairs(input_dir)
if name=='fgnet':
return get_dataset_fgnet(input_dir)
if name=='ytf':
return get_dataset_ytf(input_dir)
if name=='clfw':
return get_dataset_clfw(input_dir)
return None