2015-07-21 01:11:01 +03:00
|
|
|
import xml.etree.ElementTree as ET
|
|
|
|
import pickle
|
|
|
|
import os
|
|
|
|
from os import listdir, getcwd
|
|
|
|
from os.path import join
|
|
|
|
|
2022-09-20 06:16:06 +03:00
|
|
|
sets = [('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test')]
|
2015-07-21 01:11:01 +03:00
|
|
|
|
2022-09-20 06:16:06 +03:00
|
|
|
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
|
|
|
|
"sofa", "train", "tvmonitor"]
|
2015-07-21 01:11:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def convert(size, box):
|
2022-09-20 06:16:06 +03:00
|
|
|
dw = 1. / (size[0])
|
|
|
|
dh = 1. / (size[1])
|
|
|
|
x = (box[0] + box[1]) / 2.0 - 1
|
|
|
|
y = (box[2] + box[3]) / 2.0 - 1
|
2015-07-21 01:11:01 +03:00
|
|
|
w = box[1] - box[0]
|
|
|
|
h = box[3] - box[2]
|
2022-09-20 06:16:06 +03:00
|
|
|
x = x * dw
|
|
|
|
w = w * dw
|
|
|
|
y = y * dh
|
|
|
|
h = h * dh
|
|
|
|
return (x, y, w, h)
|
|
|
|
|
2015-07-21 01:11:01 +03:00
|
|
|
|
|
|
|
def convert_annotation(year, image_id):
|
2022-09-20 06:16:06 +03:00
|
|
|
in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml' % (year, image_id))
|
|
|
|
out_file = open('VOCdevkit/VOC%s/labels/%s.txt' % (year, image_id), 'w')
|
|
|
|
tree = ET.parse(in_file)
|
2015-07-21 01:11:01 +03:00
|
|
|
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
|
2022-09-20 06:16:06 +03:00
|
|
|
if cls not in classes or int(difficult) == 1:
|
2015-07-21 01:11:01 +03:00
|
|
|
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))
|
2022-09-20 06:16:06 +03:00
|
|
|
bb = convert((w, h), b)
|
2015-07-21 01:11:01 +03:00
|
|
|
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
|
|
|
|
|
2022-09-20 06:16:06 +03:00
|
|
|
|
2015-07-21 01:11:01 +03:00
|
|
|
wd = getcwd()
|
|
|
|
|
|
|
|
for year, image_set in sets:
|
2022-09-20 06:16:06 +03:00
|
|
|
if not os.path.exists('VOCdevkit/VOC%s/labels/' % (year)):
|
|
|
|
os.makedirs('VOCdevkit/VOC%s/labels/' % (year))
|
|
|
|
image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt' % (year, image_set)).read().strip().split()
|
|
|
|
list_file = open('%s_%s.txt' % (year, image_set), 'w')
|
2015-07-21 01:11:01 +03:00
|
|
|
for image_id in image_ids:
|
2022-09-20 06:16:06 +03:00
|
|
|
list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n' % (wd, year, image_id))
|
2015-07-21 01:11:01 +03:00
|
|
|
convert_annotation(year, image_id)
|
|
|
|
list_file.close()
|
|
|
|
|
2017-03-27 09:42:30 +03:00
|
|
|
os.system("cat 2007_train.txt 2007_val.txt 2012_train.txt 2012_val.txt > train.txt")
|
|
|
|
os.system("cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt > train.all.txt")
|