-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathdataset_builder.py
323 lines (292 loc) · 15.9 KB
/
dataset_builder.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python
""" DatasetBuilder: aggregate utilities to build large images datasets"""
from __future__ import print_function
import os
from shutil import copy2
from scipy import ndimage, misc
import numpy as np
__author__ = "Amine BENDAHMANE (@AmineHorseman)"
__email__ = "[email protected]"
__license__ = "GPL"
__date__ = "May 6nd, 2016"
class DatasetBuilder(object):
""" Aggregate utilities to build large datasets (renaming files, spliting categories,
creating labels, loading data...) """
merge_files_counter = 1
rename_files_counter = 1
data = []
labels = []
def __init__(self):
print("\nPreparing DatasetBuilder...")
@staticmethod
def check_folder_existance(folderpath, throw_error_if_no_folder=False, display_msg=True):
""" check if a folder exists.
If throw_error_if_no_folder = True and the folder does not exist:
the method will print an error message and stop the program,
Otherwise:
the method will create the folder
"""
if not os.path.exists(folderpath):
if throw_error_if_no_folder:
print("Error: folder '" + folderpath + "' does not exist")
exit()
else:
os.makedirs(folderpath)
if display_msg:
print("Target folder '", folderpath, "' does not exist...")
print(" >> Folder created")
@classmethod
def rename_files(cls, source_folder, target_folder, extensions=('.jpg', '.jpeg', '.png')):
""" list subfolders recursively and rename files according to
the following the pattern: 1.jpg, 2.jpg..."""
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# copy files and rename:
print("Renaming files '", source_folder, "' files...")
cls.rename_files_counter = 1
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.rename_files(source_folder + '/' + filename,
target_folder + '/' + filename,
extensions=extensions)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
copy2(source_folder + "/" + filename,
target_folder + "/" + str(cls.rename_files_counter))
cls.rename_files_counter += 1
else:
for extension in extensions:
if filename.endswith(extension):
copy2(source_folder + "/" + filename,
target_folder + "/" + str(cls.rename_files_counter) + extension)
cls.rename_files_counter += 1
@classmethod
def reshape_images(cls, source_folder, target_folder, height=128, width=128,
extensions=('.jpg', '.jpeg', '.png')):
""" copy images and reshape them"""
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# read images and reshape:
print("Resizing '", source_folder, "' images...")
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.reshape_images(source_folder + '/' + filename,
target_folder + '/' + filename,
height, width, extensions=extensions)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
copy2(source_folder + "/" + filename,
target_folder + "/" + filename)
image = ndimage.imread(target_folder + "/" + filename, mode="RGB")
image_resized = misc.imresize(image, (height, width))
misc.imsave(target_folder + "/" + filename, image_resized)
else:
for extension in extensions:
if filename.endswith(extension):
copy2(source_folder + "/" + filename,
target_folder + "/" + filename)
image = ndimage.imread(target_folder + "/" + filename, mode="RGB")
image_resized = misc.imresize(image, (height, width))
misc.imsave(target_folder + "/" + filename, image_resized)
@classmethod
def crop_images(cls, source_folder, target_folder, height=128, width=128,
extensions=('.jpg', '.jpeg', '.png')):
""" copy images and center crop them"""
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# read images and crop:
print("Cropping '", source_folder, "' images...")
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.crop_images(source_folder + '/' + filename,
target_folder + '/' + filename,
height, width, extensions=extensions)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
copy2(source_folder + "/" + filename,
target_folder + "/" + filename)
image = ndimage.imread(target_folder + "/" + filename, mode="RGB")
[width_original, height_original, _] = image.shape
offset_w = (width_original - width) / 2
offset_h = (width_original - width) / 2
image_cropped = image[offset_w : width + offset_w, offset_h : height + offset_h, :]
misc.imsave(target_folder + "/" + filename, image_cropped)
else:
for extension in extensions:
if filename.endswith(extension):
copy2(source_folder + "/" + filename,
target_folder + "/" + filename)
image = ndimage.imread(target_folder + "/" + filename, mode="RGB")
[width_original, height_original, _] = image.shape
offset_w = (width_original - width) / 2
offset_h = (width_original - width) / 2
image_cropped = image[offset_w : width + offset_w, offset_h : height + offset_h, :]
misc.imsave(target_folder + "/" + filename, image_cropped)
@classmethod
def merge_folders(cls, source_folder, target_folder,
extensions=('.jpg', '.jpeg', '.png')):
""" merge images in separated folders in one single folder
The images will be renamed"""
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# copy files and rename:
print("Merging '", source_folder, "' files...")
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.merge_folders(source_folder + '/' + filename,
target_folder, extensions=extensions)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
copy2(source_folder + "/" + filename,
target_folder + "/" + str(cls.merge_files_counter))
cls.merge_files_counter += 1
else:
for extension in extensions:
if filename.endswith(extension):
copy2(source_folder + "/" + filename,
target_folder + "/" + str(cls.merge_files_counter) + extension)
cls.merge_files_counter += 1
@classmethod
def convert_to_grayscale(cls, source_folder, target_folder,
extensions=('.jpg', '.jpeg', '.png')):
""" convert images from RGB to Grayscale"""
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# read images and reshape:
print("Convert '", source_folder, "' images to grayscale...")
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.convert_to_grayscale(source_folder + '/' + filename,
target_folder + '/' + filename,
extensions=extensions)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
copy2(source_folder + "/" + filename,
target_folder + "/" + filename)
image = ndimage.imread(target_folder + "/" + filename, flatten=True)
misc.imsave(target_folder + "/" + filename, image)
else:
for extension in extensions:
if filename.endswith(extension):
copy2(source_folder + "/" + filename,
target_folder + "/" + filename)
image = ndimage.imread(target_folder + "/" + filename, flatten=True)
misc.imsave(target_folder + "/" + filename, image)
@classmethod
def convert_format(cls, source_folder, target_folder,
extensions=('.jpg', '.jpeg', '.png'), new_extension='.jpg'):
""" change images from one format to another (eg. change png files to jpeg) """
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# read images and reshape:
print("Change format of '", source_folder, "' files...")
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.convert_format(source_folder + '/' + filename,
target_folder + '/' + filename,
extensions=extensions, new_extension=new_extension)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
copy2(source_folder + "/" + filename,
target_folder + "/" + filename + new_extension)
image = ndimage.imread(target_folder + "/" + filename + new_extension)
misc.imsave(target_folder + "/" + filename + new_extension, image)
else:
for extension in extensions:
if filename.endswith(extension):
new_filename = os.path.splitext(filename)[0] + new_extension
copy2(source_folder + "/" + filename,
target_folder + "/" + new_filename)
image = ndimage.imread(target_folder + "/" + new_filename)
misc.imsave(target_folder + "/" + new_filename, image)
@classmethod
def convert_to_array(cls, source_folder, target_folder, create_labels_file=False,
flatten=False, extensions=('.jpg', '.jpeg', '.png')):
""" Read all images in subfolders and convert them to a single array """
# check source_folder and target_folder:
cls.check_folder_existance(source_folder, throw_error_if_no_folder=True)
cls.check_folder_existance(target_folder, display_msg=False)
if source_folder[-1] == "/":
source_folder = source_folder[:-1]
if target_folder[-1] == "/":
target_folder = target_folder[:-1]
# read images and concatenate:
print("Converting '", source_folder, "' images...")
for filename in os.listdir(source_folder):
if os.path.isdir(source_folder + '/' + filename):
cls.convert_to_array(source_folder + '/' + filename, target_folder,
create_labels_file=create_labels_file, extensions=extensions)
else:
if extensions == '' and os.path.splitext(filename)[1] == '':
image = ndimage.imread(source_folder + "/" + filename, mode="RGB")
if (flatten):
cls.data.append(image.flatten())
else:
cls.data.append(image)
if create_labels_file:
cls.labels.append(source_folder.replace('/', '_'))
else:
for extension in extensions:
if filename.endswith(extension):
image = ndimage.imread(source_folder + "/" + filename, mode="RGB")
if (flatten):
cls.data.append(image.flatten())
else:
cls.data.append(image)
if create_labels_file:
cls.labels.append(source_folder.replace('/', '_'))
@classmethod
def convert_to_single_file(cls, source_folder, target_folder, create_labels_file=False,
flatten=False, extensions=('.jpg', '.jpeg', '.png')):
""" Convert dataset images to a single file (array of images)
The algorithm generates labels automatically according to subfolders"""
# convert files in source_folder to data array and labels array:
print("Converting images to single file...")
cls.data = []
cls.labels = []
cls.convert_to_array(source_folder, target_folder, create_labels_file=create_labels_file,
flatten=flatten, extensions=extensions)
# convert string labels to numeric type:
if create_labels_file:
print("Converting Labels to numeric type...")
keys = list(set(cls.labels)) # unique list of labels
numeric_labels = dict(zip(keys, range(len(keys))))
for i in xrange(len(cls.labels)):
cls.labels[i] = numeric_labels[cls.labels[i]]
# save files:
print(" Saving...")
np.save(target_folder + '/data.npy', cls.data)
print(" > Images saved to: ", target_folder + '/data.npy')
if create_labels_file:
np.save(target_folder + '/labels.npy', cls.labels)
print(" > Labels saved to: ", target_folder + '/labels.npy')