utils.py
6.65 KB
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
import os
from textwrap import dedent
def images_options(df_val, args):
'''
Manage the options for the images downloader.
:param df_val: DataFrame Value.
:param args: argument parser.
:return: modified df_val
'''
if args.image_IsOccluded is not None:
rejectedID = df_val.ImageID[df_val.IsOccluded != int(args.image_IsOccluded)].values
df_val = df_val[~df_val.ImageID.isin(rejectedID)]
if args.image_IsTruncated is not None:
rejectedID = df_val.ImageID[df_val.IsTruncated != int(args.image_IsTruncated)].values
df_val = df_val[~df_val.ImageID.isin(rejectedID)]
if args.image_IsGroupOf is not None:
rejectedID = df_val.ImageID[df_val.IsGroupOf != int(args.image_IsGroupOf)].values
df_val = df_val[~df_val.ImageID.isin(rejectedID)]
if args.image_IsDepiction is not None:
rejectedID = df_val.ImageID[df_val.IsDepiction != int(args.image_IsDepiction)].values
df_val = df_val[~df_val.ImageID.isin(rejectedID)]
if args.image_IsInside is not None:
rejectedID = df_val.ImageID[df_val.IsInside != int(args.image_IsInside)].values
df_val = df_val[~df_val.ImageID.isin(rejectedID)]
return df_val
def mkdirs(Dataset_folder, csv_folder, classes, type_csv):
'''
Make the folder structure for the system.
:param Dataset_folder: Self explanatory
:param csv_folder: folder path of csv files
:param classes: list of classes to download
:param type_csv: train, validation, test or all
:return: None
'''
directory_list = ['train', 'validation', 'test']
if not type_csv == 'all':
for class_name in classes:
if not Dataset_folder.endswith('_nl'):
folder = os.path.join(Dataset_folder, type_csv, class_name, 'Label')
else:
folder = os.path.join(Dataset_folder, type_csv, class_name)
if not os.path.exists(folder):
os.makedirs(folder)
filelist = [f for f in os.listdir(folder) if f.endswith(".txt")]
for f in filelist:
os.remove(os.path.join(folder, f))
else:
for directory in directory_list:
for class_name in classes:
if not Dataset_folder.endswith('_nl'):
folder = os.path.join(Dataset_folder, directory, class_name, 'Label')
else:
folder = os.path.join(Dataset_folder, directory, class_name, 'Label')
if not os.path.exists(folder):
os.makedirs(folder)
filelist = [f for f in os.listdir(folder) if f.endswith(".txt")]
for f in filelist:
os.remove(os.path.join(folder, f))
if not os.path.exists(csv_folder):
os.makedirs(csv_folder)
def progression_bar(total_images, index):
'''
Print the progression bar for the download of the images.
:param total_images: self explanatory
:param index: self explanatory
:return: None
'''
# for windows os
if os.name == 'nt':
from ctypes import windll, create_string_buffer
h = windll.kernel32.GetStdHandle(-12)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
if res:
import struct
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom, maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
columns = right - left + 1
rows = bottom - top + 1
else:
columns, rows = 80, 25 # can't determine actual size - return default values
# for linux/gnu os
else:
rows, columns = os.popen('stty size', 'r').read().split()
toolbar_width = int(columns) - 10
image_index = index
index = int(index / total_images * toolbar_width)
print(' ' * (toolbar_width), end='\r')
bar = "[{}{}] {}/{}".format('-' * index, ' ' * (toolbar_width - index), image_index, total_images)
print(bar.rjust(int(columns)), end='\r')
def show_classes(classes):
'''imag
Show the downloaded classes in the selected folder during visualization mode
'''
for n in classes:
print("- {}".format(n))
print("\n")
def logo(command):
'''
Print the logo for the downloader and the visualizer when selected
'''
bc = bcolors
print(bc.OKGREEN + """
___ _____ ______ _ _
.' `.|_ _||_ _ `. | | | |
/ .-. \ | | | | `. \ _ __ | |__| |_
| | | | | | | | | |[ \ [ ]|____ _|
\ `-' /_| |_ _| |_.' / \ \/ / _| |_
`.___.'|_____||______.' \__/ |_____|
""" + bc.ENDC)
if command == 'downloader':
print(bc.OKGREEN + '''
_____ _ _
(____ \ | | | |
_ \ \ ___ _ _ _ ____ | | ___ ____ _ | | ____ ____
| | | / _ \| | | | _ \| |/ _ \ / _ |/ || |/ _ )/ ___)
| |__/ / |_| | | | | | | | | |_| ( ( | ( (_| ( (/ /| |
|_____/ \___/ \____|_| |_|_|\___/ \_||_|\____|\____)_|
''' + bc.ENDC)
if command == 'visualizer':
print(bc.OKGREEN + """
_ _ _ _ _
| | | (_) | (_)
| | | |_ ___ _ _ ____| |_ _____ ____ ____
\ \/ /| |/___) | | |/ _ | | (___ ) _ )/ ___)
\ / | |___ | |_| ( ( | | | |/ __( (/ /| |
\/ |_(___/ \____|\_||_|_|_(_____)____)_|
""" + bc.ENDC)
if command == 'downloader_ill':
print(bc.OKGREEN + '''
_____ _ _ _____ _ _
| __ \ | | | | |_ _| | | |
| | | | _____ ___ __ | | ___ __ _ __| | ___ _ __ | | | | | |
| | | |/ _ \ \ /\ / / '_ \| |/ _ \ / _` |/ _` |/ _ \ '__| | | | | | |
| |__| | (_) \ V V /| | | | | (_) | (_| | (_| | __/ | _| |_| |____| |____
|_____/ \___/ \_/\_/ |_| |_|_|\___/ \__,_|\__,_|\___|_| |_____|______|______|
''' + bc.ENDC)
class bcolors:
HEADER = '\033[95m'
INFO = ' [INFO] | '
OKBLUE = '\033[94m[DOWNLOAD] | '
WARNING = '\033[93m [WARN] | '
FAIL = '\033[91m [ERROR] | '
OKGREEN = '\033[92m'
ENDC = '\033[0m'