-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_preprocessing_script.py
More file actions
221 lines (171 loc) · 6.73 KB
/
Copy pathdata_preprocessing_script.py
File metadata and controls
221 lines (171 loc) · 6.73 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
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
# -*- coding: utf-8 -*-
"""data-preprocessing-script.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/16-k5IL9GYypFAux7FCtOx9RtziSObV3f
# Data Preprocessing Script to Yolo Formate
### The following code is for the Script to prepare this ROW dataset into A YOLO object detection configeration dataset.
### We go into a series of steps from data preprocessing to finally training the model.
> 1. Download the dataset into your local device
> 2. Run this script and assert that `src`, `dst` paths are set to the local folder paths
> 3. this will transfere this row data into a ready to train dataset using object detection yolo formate
> 4. Build the yolomodel and run the notebook model here : ""
### Run This Script to :
> * Convert the .XML Labels Files Format to YOLO Formate (class xCenter yCenter width height)
> * Build The YOLO Folders Architecture
### This cell convert .xml to yolo formate by detecting the <object> to count the number of objects detected in the image ,then store the class number and encode it to be [0, 1, 2, 3, 4, 5]
Note that you can change the dataset path and the distenation through the first 2 lines of
code
"""
# #########################################################
Root_src = r"/kaggle/input/egyptian-currency"
Dst_src = r"/kaggle/working/"
# #########################################################
import os
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
return (x,y,w,h)
def find_between(s, first, last):
try:
start = s.index(first) + len(first)
end = s.index(last, start)
return s[start:end]
except ValueError:
return ""
def remove(data, section):
data.replace(section,' ')
return data
def freq(str):
str = str.split()
str2 = []
for i in str:
if i not in str2:
str2.append(i)
for i in range(0, len(str2)):
if str2[i] == "</object>":
return str.count(str2[i])
# #ReadyLabelsFolder
dst = os.path.join(Dst_src,"YOLO Labels")
os.makedirs(dst)
for foldername in os.listdir(Root_src):
src = os.path.join(Root_src, foldername)
# iterate over files in
# that directory
for filename in os.listdir(src):
file = os.path.join(src, filename)
# checking if it is a file
if os.path.isfile(file) and file.lower().endswith('.xml'):
f = open(file, "r")
data = f.read()
numOfClasses = freq(data)
for i in range(0, numOfClasses):
Class = find_between(data, "<name>", "</name>")
if Class == "5Egp":
Class = '0'
elif Class == "10Egp":
Class = '1'
elif Class == "20Egp":
Class = '2'
elif Class == "50Egp":
Class = '3'
elif Class == "100Egp":
Class = '4'
elif Class == "200Egp":
Class = '5'
xmin = find_between(data, "<xmin>", "</xmin>")
xmax = find_between(data, "<xmax>", "</xmax>")
ymin = find_between(data, "<ymin>", "</ymin>")
ymax = find_between(data, "<ymax>", "</ymax>")
width = find_between(data, "<width>", "</width>")
height = find_between(data, "<height>", "</height>")
ret = convert([int(width), int(height)], [float(xmin), float(xmax), float(ymin), float(ymax)])
newFileName = filename.split('.')[0]
newFileName += '.txt'
newFolder = os.path.join(dst,newFileName)
f = open(newFolder, "w")
f.write(Class + ' ' + str(ret[0]) + ' ' + str(ret[1]) + ' ' + str(ret[2]) + ' ' + str(ret[3]) + '\n')
rmSection = find_between(data, "<object>", "</object>")
data = data.replace(rmSection, " ", 1)
f.close()
print("Labels Moved to Yolo Labels")
"""### This cell Selects the .JPG files from the 10 Folders of the dataset and store them all in a single Folder named "YOLO Images" to be easy in dividing the data to train, val and test"""
import shutil
import os
#Images from original folder to my folder
img_dst = os.path.join(Dst_src,"YOLO Images")
os.makedirs(img_dst)
images_count = 0
for foldername in os.listdir(Root_src):
src = os.path.join(Root_src, foldername)
print (src, "Start")
if(foldername != "YOLO Images"):
for filename in os.listdir(src):
file = os.path.join(src, filename)
# checking if it is a file
if os.path.isfile(file) and file.lower().endswith('.jpg'):
images_count = images_count + 1
dst_path = os.path.join(img_dst,filename)
shutil.copy(file, dst_path)
print("Images moved to YOLO Images")
print(images_count)
"""### Here we start building the architecture by creating Train and Test Folders, then images and labels Folders"""
train_path = os.path.join(Dst_src,"Train")
os.makedirs(train_path)
label_path = os.path.join(train_path,"labels")
os.makedirs(label_path)
images_path = os.path.join(train_path,"images")
os.makedirs(images_path)
TN_path = os.path.join(label_path,"train")
VL_path = os.path.join(label_path,"val")
os.makedirs(TN_path)
os.makedirs(VL_path)
TN_path_ = os.path.join(images_path,"train")
VL_path_ = os.path.join(images_path,"val")
os.makedirs(TN_path_)
os.makedirs(VL_path_)
test_path = os.path.join(Dst_src,"Test")
os.makedirs(test_path)
label_path_ = os.path.join(test_path,"labels")
os.makedirs(label_path_)
images_path_ = os.path.join(test_path,"images")
os.makedirs(images_path_)
"""### Here we start to divid the data to 80% Training, 10% Validation and 10% Testing
1) Images
"""
counter = 0
for filename in os.listdir(img_dst):
counter += 1
s = os.path.join(img_dst,filename)
if counter > 0 and counter < images_count * 0.8:
d = os.path.join(TN_path_,filename)
shutil.move(s, d)
elif counter > images_count * 0.8 and counter < images_count * 0.9:
d = os.path.join(VL_path_,filename)
shutil.move(s, d)
else:
d = os.path.join(images_path_,filename)
shutil.move(s, d)
"""2) Labels"""
counter = 0
for filename in os.listdir(dst):
counter = counter + 1
s = os.path.join(dst,filename)
if counter > 0 and counter < images_count * 0.8:
d = os.path.join(TN_path,filename)
shutil.move(s, d)
elif counter > images_count * 0.8 and counter < images_count * 0.9:
d = os.path.join(VL_path,filename)
shutil.move(s, d)
else:
d = os.path.join(label_path_,filename)
shutil.move(s, d)
print("All Done !!")