-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathvoc_annotation.py
38 lines (30 loc) · 1.25 KB
/
voc_annotation.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
import os
import xml.etree.ElementTree as ET
# change your own spilt in voc_classes.txt
with open('data/voc_classes.txt') as f:
classes = f.readlines()
classes = [c.strip() for c in classes]
def convert_annotation(image_id, list_file):
"""convert annotations from xml files to txt files"""
in_file = open('data/VOCdevkit/VOC2012/Annotations/%s.xml' % image_id)
tree = ET.parse(in_file)
root = tree.getroot()
list_file.write('data/VOCdevkit/VOC2012/JPEGImages/%s.jpg' % image_id)
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
# in the form (x1, y1, x2, y2)
b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text),
int(xmlbox.find('ymax').text))
list_file.write(' ' + ','.join([str(a) for a in b]) + ',' + str(cls_id))
list_file.write('\n')
if __name__ == '__main__':
xml_files = os.listdir('data/VOCdevkit/VOC2012/Annotations')
train_file = open('data/train.txt', 'w')
for xml_file in xml_files:
img_id = xml_file.split('.')[0]
convert_annotation(img_id, train_file)
train_file.close()