forked from EscVM/OIDv4_ToolKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
241 lines (191 loc) · 9.43 KB
/
main.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
# ---------------
# Date: 7/19/2018
# Place: Biella/Torino
# Author: EscVM & TArt
# Project: OID v4
# ---------------
"""
OID v4 Downloader
Download specific classes of the huge online dataset Open Image Dataset.
Licensed under the MIT License (see LICENSE for details)
------------------------------------------------------------
Usage:
"""
from sys import exit
from textwrap import dedent
from modules.parser import *
from modules.utils import *
from modules.downloader import *
from modules.show import *
from modules.csv_downloader import *
ROOT_DIR = ''
DEFAULT_OID_DIR = os.path.join(ROOT_DIR, 'OID')
if __name__ == '__main__':
args = parser_arguments()
if not args.Dataset:
dataset_dir = os.path.join(DEFAULT_OID_DIR, 'Dataset')
csv_dir = os.path.join(DEFAULT_OID_DIR, 'csv_folder')
else:
dataset_dir = os.path.join(DEFAULT_OID_DIR, args.Dataset)
csv_dir = os.path.join(DEFAULT_OID_DIR, 'csv_folder')
name_file_class = 'class-descriptions-boxable.csv'
CLASSES_CSV = os.path.join(csv_dir, name_file_class)
if args.command == 'downloader':
logo(args.command)
if args.type_csv is None:
print('Missing type_csv argument.')
exit(1)
if args.classes is None:
print('Missing classes argument.')
exit(1)
if args.multiclasses is None:
args.multiclasses = 0
folder = ['train', 'validation', 'test']
file_list = ['train-annotations-bbox.csv', 'validation-annotations-bbox.csv', 'test-annotations-bbox.csv']
if args.classes[0].endswith('.txt'):
with open(args.classes[0]) as f:
args.classes = f.readlines()
args.classes = [x.strip() for x in args.classes]
else:
args.classes = [arg.replace('_', ' ') for arg in args.classes]
if args.multiclasses == '0':
mkdirs(dataset_dir, csv_dir, args.classes, args.type_csv)
for classes in args.classes:
print("[INFO] Downloading {}.".format(classes))
class_name = classes
error_csv(name_file_class, csv_dir)
df_classes = pd.read_csv(CLASSES_CSV, header=None)
class_code = df_classes.loc[df_classes[1] == class_name].values[0][0]
if args.type_csv == 'train':
name_file = file_list[0]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[0], dataset_dir, class_name, class_code)
else:
download(args, df_val, folder[0], dataset_dir, class_name, class_code, threads = int(args.n_threads))
elif args.type_csv == 'validation':
name_file = file_list[1]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[1], dataset_dir, class_name, class_code)
else:
download(args, df_val, folder[1], dataset_dir, class_name, class_code, threads = int(args.n_threads))
elif args.type_csv == 'test':
name_file = file_list[2]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[2], dataset_dir, class_name, class_code)
else:
download(args, df_val, folder[2], dataset_dir, class_name, class_code, threads = int(args.n_threads))
elif args.type_csv == 'all':
for i in range(3):
name_file = file_list[i]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[i], dataset_dir, class_name, class_code)
else:
download(args, df_val, folder[i], dataset_dir, class_name, class_code, threads = int(args.n_threads))
else:
print('[ERROR] csv file not specified')
exit(1)
elif args.multiclasses == '1':
class_list = args.classes
print("[INFO] Downloading {} together.".format(class_list))
multiclass_name = ['_'.join(class_list)]
mkdirs(dataset_dir, csv_dir, multiclass_name, args.type_csv)
error_csv(name_file_class, csv_dir)
df_classes = pd.read_csv(CLASSES_CSV, header=None)
class_dict = {}
for class_name in class_list:
class_dict[class_name] = df_classes.loc[df_classes[1] == class_name].values[0][0]
for class_name in class_list:
if args.type_csv == 'train':
name_file = file_list[0]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[0], dataset_dir, class_name, class_dict[class_name], class_list)
else:
download(args, df_val, folder[0], dataset_dir, class_name, class_dict[class_name], class_list, int(args.n_threads))
elif args.type_csv == 'validation':
name_file = file_list[1]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[1], dataset_dir, class_name, class_dict[class_name], class_list)
else:
download(args, df_val, folder[1], dataset_dir, class_name, class_dict[class_name], class_list, int(args.n_threads))
elif args.type_csv == 'test':
name_file = file_list[2]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[2], dataset_dir, class_name, class_dict[class_name], class_list)
else:
download(args, df_val, folder[2], dataset_dir, class_name, class_dict[class_name], class_list, int(args.n_threads))
elif args.type_csv == 'all':
for i in range(3):
name_file = file_list[i]
df_val = TTV(csv_dir, name_file)
if not args.n_threads:
download(args, df_val, folder[i], dataset_dir, class_name, class_dict[class_name], class_list)
else:
download(args, df_val, folder[i], dataset_dir, class_name, class_dict[class_name], class_list, int(args.n_threads))
elif args.command == 'visualizer':
logo(args.command)
flag = 0
while (True):
if flag == 0:
print("Which folder do you want to visualize (train, test, validation)? <exit>")
image_dir = input("> ")
flag = 1
if image_dir == 'exit':
exit(1)
class_image_dir = os.path.join(dataset_dir, image_dir)
print("Which class? <exit>")
show_classes(os.listdir(class_image_dir))
class_name = input("> ")
if class_name == 'exit':
exit(1)
download_dir = os.path.join(dataset_dir, image_dir, class_name)
label_dir = os.path.join(dataset_dir, image_dir, class_name, 'Label')
if not os.path.isdir(download_dir):
print("[ERROR] Images folder not found")
exit(1)
if not os.path.isdir(label_dir):
print("[ERROR] Labels folder not found")
exit(1)
index = 0
print(dedent("""
--------------------------------------------------------
INFO:
- Press 'd' to select next image
- Press 'a' to select previous image
- Press 'e' to select a new class
- Press 'w' to select a new folder
- Press 'q' to exit
You can resize the window if it's not optimal
--------------------------------------------------------
"""))
show(class_name, download_dir, label_dir,len(os.listdir(download_dir))-1, index)
while True:
progression_bar(len(os.listdir(download_dir))-1, index+1)
k = cv2.waitKey(0) & 0xFF
if k == ord('d'):
cv2.destroyAllWindows()
if index < len(os.listdir(download_dir)) - 1:
index += 1
show(class_name, download_dir, label_dir,len(os.listdir(download_dir))-1, index)
elif k == ord('a'):
cv2.destroyAllWindows()
if index > 0:
index -= 1
show(class_name, download_dir, label_dir,len(os.listdir(download_dir))-1, index)
elif k == ord('e'):
cv2.destroyAllWindows()
break
elif k == ord('w'):
flag = 0
cv2.destroyAllWindows()
break
elif k == ord('q'):
cv2.destroyAllWindows()
exit(1)
break