-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_yolo_dataset_modified.py
executable file
·178 lines (139 loc) · 5.57 KB
/
convert_yolo_dataset_modified.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
# -*- coding: utf-8 -*-
# this file is dedicated to convert annotated text file to YOLO format
import os
from os import walk, getcwd
from PIL import Image
dirName = "/media/atif/BE0E05910E0543BD/University of Bremen MSc/problem_solving/convert_yolo_folder/Images/" # All images are stored here in sub_folder. Each sub_folder means each class
folder_name_string = [] # to store all the names of the sub_folder in a list. It is the process of gathering all the class name as the sub_folders name are nothing but class name
ent = os.listdir(dirName)
for t in ent:
folder_name_string.append(t)
print(folder_name_string)
folder_name_string.sort() # sorting class name alphabetically
print(folder_name_string)
classes = []
for index, values in enumerate(folder_name_string):
# print(index)
index_1 = index+1
# print(index_1)
index_2 = "00"+str(index_1)
print(index_2)
classes.append(index_2)
print(classes)
#folder_name_string = ["calgonit_finish","calgonit_finish_klarspueler","calgonit_maschine_pfleger"]
#classes = ["001","002","003"]
#classes = ["001","002","003","004","005","006","007","008","009","0010","0011","0012","0013","0014","0015","0016","0017","0018","0019"]
#classes = ["001","002"]
def convert(size, box):
dw = 1./size[0]
dh = 1./size[1]
x = (box[0] + box[1])/2.0
y = (box[2] + box[3])/2.0
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)
print('folder_name \t class(number to be pressed)')
for idx,val in enumerate (folder_name_string):
print(folder_name_string[idx],'\t\t',classes[idx])
"""-------------------------------------------------------------------"""
""" Configure Paths"""
print("Give the folder number using 1 or 2 digit.")
print("For example, if the folder name is 001 then put 1, for 005 put 5 but if name is 0012 then put 12")
folder_number = input("folder_number: ")
print(folder_number)
folder_num_here = "00"+folder_number
#print(folder_num_here)
index_of_this_folder_im_classes = classes.index(folder_num_here)
#print(index_of_this_folder_im_classes)
main_folder_name = folder_name_string[index_of_this_folder_im_classes]
#print(type(main_folder_name),' ',main_folder_name)
mypath = "/media/atif/BE0E05910E0543BD/University of Bremen MSc/problem_solving/convert_yolo_folder/Labels/"+main_folder_name+"/"
outpath = "/media/atif/BE0E05910E0543BD/University of Bremen MSc/problem_solving/convert_yolo_folder/Labels/out/"+main_folder_name+"/"
#mypath = "/home/atif/machine_learning_stuff/YOLO_learning/YOLO-Annotation-Tool/Labels/00"+folder_number+"/"
#outpath = "/home/atif/machine_learning_stuff/YOLO_learning/YOLO-Annotation-Tool/Labels/out/00"+folder_number+"/"
#mypath = "/home/atif/machine_learning_stuff/YOLO_learning/YOLO-Annotation-Tool/Labels/001/"
#outpath = "/home/atif/machine_learning_stuff/YOLO_learning/YOLO-Annotation-Tool/Labels/out/001/"
if not os.path.exists(outpath):
os.makedirs(outpath)
print('created : ', outpath)
else:
print(' has already created',outpath)
pass
#print(mypath)
#print(outpath)
cls = "00"+folder_number # class or class number will be same as the folder number
#cls = "001"
#print(type(cls))
if cls not in classes:
print("invalid class")
exit(0)
cls_id = classes.index(cls)
wd = getcwd()
list_file = open('%s/%s_list.txt'%(wd, main_folder_name), 'w')
print(wd)
print(list_file)
""" Get input text file list """
txt_name_list = []
for (dirpath, dirnames, filenames) in walk(mypath):
txt_name_list.extend(filenames)
print("break")
break
# print(txt_name_list)
print("TILL HERE OKK")
""" Process """
for txt_name in txt_name_list:
# txt_file = open("Labels/stop_sign/001.txt", "r")
""" Open input text files """
txt_path = mypath + txt_name
print("Input:" + txt_path)
txt_file = open(txt_path, "r")
lines = txt_file.read().split('\n') #for ubuntu, use "\r\n" instead of "\n"
""" Open output text files """
txt_outpath = outpath + txt_name
print("I am here Output:" + txt_outpath)
txt_outfile = open(txt_outpath, "a")
# print("iiiii")
""" Convert the data to YOLO format """
ct = 0
for line in lines:
#print('lenth of line is: ')
#print(len(line))
#print('\n')
elems = line.split(' ')
if(len(elems) >= 2):
#if(len(line) >= 2):
ct = ct + 1
print(line + "\n")
elems = line.split(' ')
print(elems)
xmin = elems[0]
xmax = elems[2]
ymin = elems[1]
ymax = elems[3]
print(elems[0])
#
img_path = str('%s/Images/%s/%s.png'%(wd, main_folder_name, os.path.splitext(txt_name)[0]))
# img_path = str('%s/Images/%s/%s.png'%(wd, cls, os.path.splitext(txt_name)[0]))
#t = magic.from_file(img_path)
#wh= re.search('(\d+) x (\d+)', t).groups()
im=Image.open(img_path)
w= int(im.size[0])
h= int(im.size[1])
#w = int(xmax) - int(xmin)
#h = int(ymax) - int(ymin)
# print(xmin)
print(w, h)
print(float(xmin), float(xmax), float(ymin), float(ymax))
b = (float(xmin), float(xmax), float(ymin), float(ymax))
bb = convert((w,h), b)
print(bb)
txt_outfile.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
""" Save those images with bb into list"""
if(ct != 0):
print("ok")
list_file.write('%s/images/%s/%s.png\n'%(wd, cls, os.path.splitext(txt_name)[0]))
list_file.close()