forked from cv516Buaa/tph-yolov5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_voc_to_yolo.py
210 lines (192 loc) · 10.4 KB
/
convert_voc_to_yolo.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
import os
import shutil
import xml.etree.ElementTree as ET
import argparse
import sys
import yaml
parser = argparse.ArgumentParser(description="Convert VOC dataset (with correct directory structure) to YOLO dataset")
parser.add_argument("dataset_dir", metavar="dataset-dir", help="Directory of VOC dataset")
parser.add_argument("--output", help="Output directory (otherwise it will overwrite dataset)")
args = parser.parse_args()
args.dataset_dir = os.path.abspath(os.path.expanduser(os.path.expandvars(args.dataset_dir)))
if not args.output:
args.output = args.dataset_dir
else:
args.output = os.path.abspath(os.path.expanduser(os.path.expandvars(args.output)))
if not os.path.exists(args.output):
os.mkdir(args.output)
if not os.path.exists(args.dataset_dir):
print("Please supply a valid directory", file=sys.stderr)
exit = True
if not os.path.exists(os.path.join(args.dataset_dir, "Annotations")):
print("Missing Annotations directory", file=sys.stderr)
exit = True
if not os.path.exists(os.path.join(args.dataset_dir, "ImageSets")):
print("Missing ImageSets directory", file=sys.stderr)
exit = True
if not os.path.exists(os.path.join(args.dataset_dir, "JPEGImages")):
print("Missing JPEGImages directory", file=sys.stderr)
exit = True
if not os.path.exists(os.path.join(args.dataset_dir, "ImageSets", "Main")):
print(f"Missing {os.path.join(args.dataset_dir, 'ImageSets', 'Main')} directory", file=sys.stderr)
exit = True
if not os.path.exists(os.path.join(args.dataset_dir, "labels.txt")):
print("Missing labels.txt file", file=sys.stderr)
exit = True
if exit == True:
sys.exit(1)
if os.path.exists(os.path.join(args.output, 'train')):
shutil.rmtree(os.path.join(args.output, 'train'))
os.mkdir(os.path.join(args.output, 'train'))
if os.path.exists(os.path.join(args.output, 'test')):
shutil.rmtree(os.path.join(args.output, 'test'))
os.mkdir(os.path.join(args.output, 'test'))
if os.path.exists(os.path.join(args.output, 'val')):
shutil.rmtree(os.path.join(args.output, 'val'))
os.mkdir(os.path.join(args.output, 'val'))
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 convert_annotation(annotation_file, output_file, classes):
with open(output_file, 'w') as out_file:
tree = ET.parse(annotation_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
def populateDir(dirname):
with open(os.path.join(args.dataset_dir, "ImageSets", "Main", f"{dirname}.txt"),'r') as f:
for base_name in [x.strip() for x in f.readlines()]:
annotation_path = os.path.join(args.dataset_dir, "Annotations", base_name + ".xml")
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".jpg")
if not os.path.exists(annotation_path):
print(f"Could not find {annotation_path}")
continue
if not os.path.exists(image_path):
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".JPG")
if not os.path.exists(image_path):
print(f"Could not find {image_path}")
continue
shutil.copy(image_path, os.path.join(args.output, dirname))
convert_annotation(annotation_path, os.path.join(args.output, dirname, base_name + '.txt'), classes)
classes = []
with open(os.path.join(args.dataset_dir, "labels.txt"), 'r') as f:
classes = [x.strip() for x in f.readlines()]
categories = os.listdir(os.path.join(args.dataset_dir, "ImageSets", "Main"))
if len(categories) > 0:
if "test.txt" in categories and "train.txt" in categories and "val.txt" in categories:
print("Found test, train and val")
populateDir('test')
populateDir('train')
populateDir('val')
elif "test.txt" in categories and "trainval.txt" in categories:
print("Found test and trainval")
print("Splitting up trainval in train and val with 90/10")
populateDir('test')
with open(os.path.join(args.dataset_dir, "ImageSets", "Main", "trainval.txt"),'r') as f:
file_list = [x.strip() for x in f.readlines()]
for i,base_name in enumerate(file_list):
if i <= 0.9 * len(file_list):
annotation_path = os.path.join(args.dataset_dir, "Annotations", base_name + ".xml")
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".jpg")
if not os.path.exists(annotation_path):
print(f"Could not find {annotation_path}")
continue
if not os.path.exists(image_path):
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".JPG")
if not os.path.exists(image_path):
print(f"Could not find {image_path}")
continue
shutil.copy(image_path, os.path.join(args.output, "train"))
convert_annotation(annotation_path, os.path.join(args.output, "train", base_name + '.txt'), classes)
else:
annotation_path = os.path.join(args.dataset_dir, "Annotations", base_name + ".xml")
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".jpg")
if not os.path.exists(annotation_path):
print(f"Could not find {annotation_path}")
continue
if not os.path.exists(image_path):
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".JPG")
if not os.path.exists(image_path):
print(f"Could not find {image_path}")
continue
shutil.copy(image_path, os.path.join(args.output, "val"))
convert_annotation(annotation_path, os.path.join(args.output, "val", base_name + '.txt'), classes)
else:
print("Did not find the right image sets, creating own division 75/18/7")
print(f'Using {os.path.join(args.dataset_dir, "ImageSets", "Main", categories[0])} as image set')
with open(os.path.join(args.dataset_dir, "ImageSets", "Main", categories[0]),'r') as f:
file_list = [x.strip() for x in f.readlines()]
for i,base_name in enumerate(file_list):
if i <= 0.75 * len(file_list):
annotation_path = os.path.join(args.dataset_dir, "Annotations", base_name + ".xml")
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".jpg")
if not os.path.exists(annotation_path):
print(f"Could not find {annotation_path}")
continue
if not os.path.exists(image_path):
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".JPG")
if not os.path.exists(image_path):
print(f"Could not find {image_path}")
continue
shutil.copy(image_path, os.path.join(args.output, "train"))
convert_annotation(annotation_path, os.path.join(args.output, "train", base_name + '.txt'), classes)
elif i <= 0.93:
annotation_path = os.path.join(args.dataset_dir, "Annotations", base_name + ".xml")
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".jpg")
if not os.path.exists(annotation_path):
print(f"Could not find {annotation_path}")
continue
if not os.path.exists(image_path):
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".JPG")
if not os.path.exists(image_path):
print(f"Could not find {image_path}")
continue
shutil.copy(image_path, os.path.join(args.output, "test"))
convert_annotation(annotation_path, os.path.join(args.output, "test", base_name + '.txt'), classes)
else:
annotation_path = os.path.join(args.dataset_dir, "Annotations", base_name + ".xml")
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".jpg")
if not os.path.exists(annotation_path):
print(f"Could not find {annotation_path}")
continue
if not os.path.exists(image_path):
image_path = os.path.join(args.dataset_dir, "JPEGImages", base_name + ".JPG")
if not os.path.exists(image_path):
print(f"Could not find {image_path}")
continue
shutil.copy(image_path, os.path.join(args.output, "val"))
convert_annotation(annotation_path, os.path.join(args.output, "val", base_name + '.txt'), classes)
with open(os.path.join(args.output, os.path.basename(args.output) + ".yaml"), 'w') as f:
yamlcontent = [
{'path': args.output},
{'train': 'train'},
{'val': 'val'},
{'test': 'test'},
{'nc': len(classes)},
{'names': classes}
]
yaml.safe_dump(yamlcontent, f)
print(f"Finished converting dataset, make sure to update the {os.path.basename(args.output) + '.yaml'} file if you change the dataset location!")
else:
print(f"Need at least one file in {os.path.join(args.dataset_dir, 'ImageSets', 'Main')}", file=sys.stderr)
sys.exit(1)