-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup_datasets.py
290 lines (220 loc) · 12 KB
/
setup_datasets.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
import os
"""
This module contains functions which allow to download and setup the datasets used for this project.
"""
def download_and_setup_test_dataset():
"""
Downloading the test dataset.
"""
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/test_database.tar')
datasets_path = "datasets"
if not os.path.exists(datasets_path):
os.makedirs(datasets_path)
# put the test dataset in datasets/test
os.system("tar xf test_database.tar -C 'datasets' --one-top-level && mv test_database.tar datasets/test")
def download_and_setup_small_dataset():
"""
Downloading the small dataset.
"""
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_small.tar')
datasets_path = "datasets"
if not os.path.exists(datasets_path):
os.makedirs(datasets_path)
# put the small dataset in datasets/small
os.system(
"tar xf defi1certif-datasets-fire_small.tar -C 'datasets' --one-top-level && mv "
"datasets/defi1certif-datasets-fire_small datasets/small")
def download_and_setup_medium_dataset():
"""
Downloading the medium dataset.
"""
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_medium.tar.001')
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_medium.tar.002')
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_medium.tar.003')
datasets_path = "datasets"
if not os.path.exists(datasets_path):
os.makedirs(datasets_path)
# recombine the tar files
os.system("cat defi1certif-datasets-fire_medium.tar.001 defi1certif-datasets-fire_medium.tar.002 "
"defi1certif-datasets-fire_medium.tar.003 >> defi1certif-datasets-fire_medium.tar")
# put the medium dataset in datasets/medium
os.system("tar xf defi1certif-datasets-fire_medium.tar -C 'datasets' --one-top-level && mv "
"datasets/defi1certif-datasets-fire_medium datasets/medium")
def download_and_setup_large_dataset():
"""
Downloading the large dataset.
"""
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_big.tar.001')
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_big.tar.002')
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_big.tar.003')
os.system(
'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/defi1certif-datasets-fire_big.tar.004')
datasets_path = "datasets"
if not os.path.exists(datasets_path):
os.makedirs(datasets_path)
# recombine the tar files
os.system("cat defi1certif-datasets-fire_big.tar.001 defi1certif-datasets-fire_big.tar.002 "
"defi1certif-datasets-fire_big.tar.003 defi1certif-datasets-fire_big.tar.004 >> "
"defi1certif-datasets-fire_big.tar")
# put the large dataset in datasets/large
os.system("tar xf defi1certif-datasets-fire_big.tar -C 'datasets' --one-top-level && mv "
"datasets/defi1certif-datasets-fire_big datasets/large")
def setup_full_dataset():
"""
Downloads and sets up all datasets in a single folder named all.
A folder per class is created.
"""
download_and_setup_small_dataset()
download_and_setup_medium_dataset()
download_and_setup_large_dataset()
# creating the folder to merge datasets
if not os.path.exists("datasets/all"):
os.makedirs("datasets/all")
if not os.path.exists("datasets/all/fire"):
os.makedirs("datasets/all/fire")
if not os.path.exists("datasets/all/no_fire"):
os.makedirs("datasets/all/no_fire")
if not os.path.exists("datasets/all/start_fire"):
os.makedirs("datasets/all/start_fire")
# moving images from the small dataset to the full dataset
os.system("find datasets/small/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
os.system("find datasets/small/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
os.system("find datasets/small/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
# moving images from the medium dataset to the full dataset
os.system("find datasets/medium/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
os.system("find datasets/medium/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
os.system("find datasets/medium/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
# moving images from the large dataset to the full dataset
os.system("find datasets/large/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
os.system("find datasets/large/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
os.system("find datasets/large/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
size_rep_dict = {'small': 0, 'medium': 3, 'big': 4}
def necessary_setup_fire_detection(size='small'):
datasets_path = 'datasets' + '/' + size
fire = not (os.path.exists(datasets_path + '/fire') and
len(os.listdir(datasets_path + '/fire')) != 0)
no_fire = not (os.path.exists(datasets_path + '/no_fire') and
len(os.listdir(datasets_path + '/no_fire')) != 0)
start_fire = not (os.path.exists(datasets_path + '/start_fire') and
len(os.listdir(datasets_path + '/start_fire')) != 0)
return fire and no_fire and start_fire
def download_and_setup_dataset_fire_detection(size='small', verbose=0): # size in {'small', 'medium', 'big'}
def verprint(s):
if verbose:
print(s, flush=True)
necessary = necessary_setup_fire_detection(size)
if necessary:
if size != 'all':
# get number of repetitions
rep = size_rep_dict[size]
prefix = 'wget https://github.com/belarbi2733/keras_yolov3/releases/download/1/'
inter_command = 'defi1certif-datasets-fire_' + size + '.tar'
# get dataset
if rep:
for i in range(1, rep + 1):
suffix = '.00' + str(i)
command = prefix + inter_command + suffix
verprint('+ executing: ' + command)
os.system(command)
# recombine when needed
recombine_command = 'cat'
for i in range(1, rep + 1):
suffix = '.00' + str(i)
recombine_command += ' ' + inter_command + suffix
recombine_command += ' > ' + inter_command
verprint('+ executing: ' + recombine_command)
os.system(recombine_command)
# clean behind you
clean_command = 'rm'
for i in range(1, rep + 1):
suffix = '.00' + str(i)
clean_command += ' ' + inter_command + suffix
verprint('+ executing: ' + clean_command)
os.system(clean_command)
else:
verprint('+ executing: ' + prefix + inter_command)
os.system(prefix + inter_command)
# created dirs
datasets_path = "datasets"
verprint("- attempting to create 'datasets' directory")
if os.path.exists(datasets_path) == False:
verprint("- creating 'datasets' directory")
os.makedirs(datasets_path)
else:
verprint("- 'datasets' directory already exists")
# put the each dataset in its asociate folder
prefix = 'tar xf '
suffix = " -C 'datasets' --one-top-level && mv datasets/defi1certif-datasets-fire_" + size + " datasets/" + size
# execute
command = prefix + inter_command + suffix
verprint('+ executing: ' + command)
os.system(command)
# clean behind you
if os.path.exists("datasets/defi1certif-datasets-fire_" + size):
verprint('+ executing: ' + 'rm -r datasets/defi1certif-datasets-fire_' + size)
os.system('rm -r datasets/defi1certif-datasets-fire_' + size)
verprint('- ' + size + ' dataset successfully setup')
else:
for key in size_rep_dict:
download_and_setup_dataset_fire_detection(size=key)
def setup_full_dataset_fire_detection(verbose=0):
"""
Combines all datasets in a single folder.
:return:
"""
def verprint(s):
if verbose:
print(s, flush=True)
verprint('- fusioning all datasets')
verprint('- creating directories')
# creating the folder to merge datasets
if not os.path.exists("datasets/all"):
os.makedirs("datasets/all")
if not os.path.exists("datasets/all/fire"):
os.makedirs("datasets/all/fire")
if not os.path.exists("datasets/all/no_fire"):
os.makedirs("datasets/all/no_fire")
if not os.path.exists("datasets/all/start_fire"):
os.makedirs("datasets/all/start_fire")
verprint('- moving files')
# moving images from the small dataset to the full dataset
verprint('+ executing: ' + "find datasets/small/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
os.system("find datasets/small/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
verprint('+ executing: ' + "find datasets/small/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
os.system("find datasets/small/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
verprint(
'+ executing: ' + "find datasets/small/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
os.system("find datasets/small/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
# moving images from the medium dataset to the full dataset
verprint('+ executing: ' + "find datasets/medium/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
os.system("find datasets/medium/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
verprint('+ executing: ' + "find datasets/medium/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
os.system("find datasets/medium/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
verprint(
'+ executing: ' + "find datasets/medium/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
os.system("find datasets/medium/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
# moving images from the large dataset to the full dataset
verprint('+ executing: ' + "find datasets/big/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
os.system("find datasets/big/fire -type f -print0 | xargs -0 mv -t datasets/all/fire/")
verprint('+ executing: ' + "find datasets/big/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
os.system("find datasets/big/no_fire -type f -print0 | xargs -0 mv -t datasets/all/no_fire/")
verprint('+ executing: ' + "find datasets/big/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
os.system("find datasets/big/start_fire -type f -print0 | xargs -0 mv -t datasets/all/start_fire/")
verprint("- files moved to:'datasets/all/")
space = len("- files moved to:'datasets/all/")
verprint(' ' * space + "fire'")
verprint(' ' * space + "no_fire'")
verprint(' ' * space + "start_fire'")
verprint('- done')
def download_and_setup_full_dataset_fire_detection():
download_and_setup_dataset_fire_detection('all')
setup_full_dataset_fire_detection()
download_and_setup_full_dataset_fire_detection()