-
Notifications
You must be signed in to change notification settings - Fork 13
/
setup_sets.py
156 lines (120 loc) · 5.64 KB
/
setup_sets.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
import os
import shutil
import glob
from shutil import copyfile
print("\n Start Processing!")
# parent directory (change parent directory for your own)
parent_dir = "../traffic-signs-detection/"
# training, validation and test sets folders (arbitrary paths)
train_dir = parent_dir + "train"
validation_dir = parent_dir + "valid"
test_dir = parent_dir + "test"
data_dir = parent_dir + "data"
weights_dir = parent_dir + "weights"
##############################################################
################ COPY CLASSNAMES IN OBJ.NAMES ################
##############################################################
# create data folder
data_path = os.path.join(parent_dir, data_dir)
if not os.path.exists(data_path):
os.mkdir(data_path)
else:
pass
# go to copy the labels into obj.names files
src = train_dir + "/_darknet.labels"
dst = data_dir + "/obj.names"
shutil.copyfile(src, dst)
# create folder "obj"
directory = "obj"
path_obj = os.path.join(data_dir, directory)
if not os.path.exists(path_obj):
os.mkdir(path_obj)
else:
pass
##############################################################
###################### TRAINING DATA #########################
##############################################################
# get the training images then copy them to the obj folder
train_imgs_src = train_dir
train_imgs_dst = data_dir + "/obj"
# create a "train" folder into "obj" folder
obj_train_dir = "train_processed"
path_train_dst = os.path.join(train_imgs_dst, obj_train_dir)
os.mkdir(path_train_dst)
# copy images from the source to the "train folder" in "obj" folder
for jpgfile in glob.iglob(os.path.join(train_imgs_src, "*.jpg")):
shutil.copy(jpgfile, path_train_dst)
##############################################################
#################### VALIDATION DATA #########################
##############################################################
# get the validation images then copy them to the obj folder
valid_imgs_src = validation_dir
valid_imgs_dst = data_dir + "/obj"
# create a "valid" folder into "obj" folder
obj_val_dir = "valid_processed"
path_valid_dst = os.path.join(valid_imgs_dst, obj_val_dir)
os.mkdir(path_valid_dst)
# copy images from the source to the "valid folder" in "obj" folder
for jpgfile in glob.iglob(os.path.join(valid_imgs_src, "*.jpg")):
shutil.copy(jpgfile, path_valid_dst)
##############################################################
####################### TEST DATA ############################
##############################################################
# get the validation images then copy them to the obj folder
test_imgs_src = test_dir
test_imgs_dst = data_dir + "/obj"
# create a "valid" folder into "obj" folder
obj_test_dir = "test_processed"
path_test_dst = os.path.join(test_imgs_dst, obj_test_dir)
os.mkdir(path_test_dst)
# copy images from the source to the "valid folder" in "obj" folder
for jpgfile in glob.iglob(os.path.join(test_imgs_src, "*.jpg")):
shutil.copy(jpgfile, path_test_dst)
##############################################################
###################### LABELS DATA ###########################
##############################################################
# copy training images' labels from the source to the "train folder" in "obj" folder
for labels in glob.iglob(os.path.join(train_imgs_src, "*.txt")):
shutil.copy(labels, path_train_dst)
# copy validation images' labels from the source to the "valid folder" in "obj" folder
for labels in glob.iglob(os.path.join(valid_imgs_src, "*.txt")):
shutil.copy(labels, path_valid_dst)
# copy test images' labels from the source to the "test folder" in "obj" folder
for labels in glob.iglob(os.path.join(test_imgs_src, "*.txt")):
shutil.copy(labels, path_test_dst)
##############################################################
##############################################################
# creating backup folder
backup_folder = "backup"
backup_dir = os.path.join(parent_dir, backup_folder)
if not os.path.exists(backup_dir):
os.mkdir(backup_dir)
else:
pass
##############################################################
############### WRITTING FILENAMES IN OBJ.DATA ###############
##############################################################
# writting filenames in the obj.data file
with open((parent_dir+'data/obj.data'), 'w') as out:
out.write('classes = 4\n')
out.write('train = ../data/train.txt\n')
out.write('valid = ../data/valid.txt\n')
out.write('test = ../data/test.txt\n')
out.write('names = ../data/obj.names\n')
out.write('backup = ../backup/')
##############################################################
############## WRITTING IMG NAMES IN TXT FILES ###############
##############################################################
# write the train.txt file with a list of the training images stored in /data/obj/train
with open('../traffic-signs-detection/data/train.txt', 'w') as out:
for img in [f for f in os.listdir(train_dir) if f.endswith('jpg')]:
out.write(('../data/obj/train_processed/') + img + '\n')
# write the valid.txt file with a list of the validation images stored in /data/obj/valid
with open('../traffic-signs-detection/data/valid.txt', 'w') as out:
for img in [f for f in os.listdir(validation_dir) if f.endswith('jpg')]:
out.write(('../data/obj/valid_processed/') + img + '\n')
# write the valid.txt file with a list of the validation images stored in /data/obj/valid
with open('../traffic-signs-detection/data/test.txt', 'w') as out:
for img in [f for f in os.listdir(test_dir) if f.endswith('jpg')]:
out.write(('../data/obj/test_processed/') + img + '\n')
print("\n Processing complete!")