Spaces:
Build error
Build error
commit
Browse files- yolov6/data/voc2yolo.py +100 -0
yolov6/data/voc2yolo.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import xml.etree.ElementTree as ET
|
2 |
+
from tqdm import tqdm
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import argparse
|
6 |
+
|
7 |
+
# VOC dataset (refer https://github.com/ultralytics/yolov5/blob/master/data/VOC.yaml)
|
8 |
+
# VOC2007 trainval: 446MB, 5012 images
|
9 |
+
# VOC2007 test: 438MB, 4953 images
|
10 |
+
# VOC2012 trainval: 1.95GB, 17126 images
|
11 |
+
|
12 |
+
VOC_NAMES = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
|
13 |
+
'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
|
14 |
+
|
15 |
+
|
16 |
+
def convert_label(path, lb_path, year, image_id):
|
17 |
+
def convert_box(size, box):
|
18 |
+
dw, dh = 1. / size[0], 1. / size[1]
|
19 |
+
x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2]
|
20 |
+
return x * dw, y * dh, w * dw, h * dh
|
21 |
+
in_file = open(os.path.join(path, f'VOC{year}/Annotations/{image_id}.xml'))
|
22 |
+
out_file = open(lb_path, 'w')
|
23 |
+
tree = ET.parse(in_file)
|
24 |
+
root = tree.getroot()
|
25 |
+
size = root.find('size')
|
26 |
+
w = int(size.find('width').text)
|
27 |
+
h = int(size.find('height').text)
|
28 |
+
for obj in root.iter('object'):
|
29 |
+
cls = obj.find('name').text
|
30 |
+
if cls in VOC_NAMES and not int(obj.find('difficult').text) == 1:
|
31 |
+
xmlbox = obj.find('bndbox')
|
32 |
+
bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
|
33 |
+
cls_id = VOC_NAMES.index(cls) # class id
|
34 |
+
out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')
|
35 |
+
|
36 |
+
|
37 |
+
def gen_voc07_12(voc_path):
|
38 |
+
'''
|
39 |
+
Generate voc07+12 setting dataset:
|
40 |
+
train: # train images 16551 images
|
41 |
+
- images/train2012
|
42 |
+
- images/train2007
|
43 |
+
- images/val2012
|
44 |
+
- images/val2007
|
45 |
+
val: # val images (relative to 'path') 4952 images
|
46 |
+
- images/test2007
|
47 |
+
'''
|
48 |
+
dataset_root = os.path.join(voc_path, 'voc_07_12')
|
49 |
+
if not os.path.exists(dataset_root):
|
50 |
+
os.makedirs(dataset_root)
|
51 |
+
|
52 |
+
dataset_settings = {'train': ['train2007', 'val2007', 'train2012', 'val2012'], 'val':['test2007']}
|
53 |
+
for item in ['images', 'labels']:
|
54 |
+
for data_type, data_list in dataset_settings.items():
|
55 |
+
for data_name in data_list:
|
56 |
+
ori_path = os.path.join(voc_path, item, data_name)
|
57 |
+
new_path = os.path.join(dataset_root, item, data_type)
|
58 |
+
if not os.path.exists(new_path):
|
59 |
+
os.makedirs(new_path)
|
60 |
+
|
61 |
+
print(f'[INFO]: Copying {ori_path} to {new_path}')
|
62 |
+
for file in os.listdir(ori_path):
|
63 |
+
shutil.copy(os.path.join(ori_path, file), new_path)
|
64 |
+
|
65 |
+
|
66 |
+
def main(args):
|
67 |
+
voc_path = args.voc_path
|
68 |
+
for year, image_set in ('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test'):
|
69 |
+
imgs_path = os.path.join(voc_path, 'images', f'{image_set}')
|
70 |
+
lbs_path = os.path.join(voc_path, 'labels', f'{image_set}')
|
71 |
+
|
72 |
+
try:
|
73 |
+
with open(os.path.join(voc_path, f'VOC{year}/ImageSets/Main/{image_set}.txt'), 'r') as f:
|
74 |
+
image_ids = f.read().strip().split()
|
75 |
+
if not os.path.exists(imgs_path):
|
76 |
+
os.makedirs(imgs_path)
|
77 |
+
if not os.path.exists(lbs_path):
|
78 |
+
os.makedirs(lbs_path)
|
79 |
+
|
80 |
+
for id in tqdm(image_ids, desc=f'{image_set}{year}'):
|
81 |
+
f = os.path.join(voc_path, f'VOC{year}/JPEGImages/{id}.jpg') # old img path
|
82 |
+
lb_path = os.path.join(lbs_path, f'{id}.txt') # new label path
|
83 |
+
convert_label(voc_path, lb_path, year, id) # convert labels to YOLO format
|
84 |
+
if os.path.exists(f):
|
85 |
+
shutil.move(f, imgs_path) # move image
|
86 |
+
except Exception as e:
|
87 |
+
print(f'[Warning]: {e} {year}{image_set} convert fail!')
|
88 |
+
|
89 |
+
gen_voc07_12(voc_path)
|
90 |
+
|
91 |
+
|
92 |
+
|
93 |
+
if __name__ == '__main__':
|
94 |
+
parser = argparse.ArgumentParser()
|
95 |
+
parser.add_argument('--voc_path', default='VOCdevkit')
|
96 |
+
|
97 |
+
args = parser.parse_args()
|
98 |
+
print(args)
|
99 |
+
|
100 |
+
main(args)
|