在使用YOLO_V3的代码时,输入的数据格式与平常我们使用的数据格式有一定的区别。
此版本代码需要得到dataset.txt文本数据,格式如下:
xxx/xxx.jpg 18.19 6.32 424.13 421.83 20 323.86 2.65 640.0 421.94 20
xxx/xxx.jpg 55.38 132.63 519.84 380.4 16
# image_path x_min y_min x_max y_max class_id x_min y_min ... class_id
那么,如何将VOC2007数据集转换成这个文本呢?
import xml.etree.ElementTree as ET
import os
classes = ['aeroplane', 'bicycle','bird','boat','bottle','bus','car', 'cat', 'chair','cow','diningtable','dog','horse','motorbike','person', 'pottedplant','sheep',
'sofa','train','tvmonitor']
def convert_annotation(image_id):
in_file = open('/home/xxxxx/xxxxx/YOLO_V3/VOC2007/Annotations/%s.xml' % image_id)
out_file = open('dataset.txt', 'a') # 生成txt格式文件
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
out_file.write('/home/xxxxx/xxxxxx/YOLO_V3/VOC2007/JPEGImages/{}.jpg'.format(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')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
out_file.write(" " + str(b[0]) + " " + str(b[2]) +" "+str(b[1])+" "+str(b[3])+" "+str(cls_id))
out_file.write("\n")
imgname_list = []
part_name = 'trainval.txt' # test.txt
with open(os.path.join('/home/xxxxx/xxxxx/YOLO_V3/VOC2007/', 'ImageSets/Main/'+part_name)) as f:
all_lines = f.readlines()
for a_line in all_lines:
imgname_list.append(a_line.split()[0].strip())
print(len(imgname_list))
for image_id in imgname_list:
convert_annotation(image_id)
转换成的文本格式如下:
/home/xxxxx/xxxxx/YOLO_V3/VOC2007/JPEGImages/003120.jpg 233.0 115.0 580.0 355.0 3
/home/xxxxx/xxxxx/YOLO_V3/VOC2007/JPEGImages/004133.jpg 59.0 66.0 185.0 176.0 5 24.0 176.0 169.0 267.0 5 101.0 157.0 275.0 246.0 5 293.0 202.0 470.0 291.0 5
/home/xxxxx/xxxxx/YOLO_V3/VOC2007/JPEGImages/003837.jpg 346.0 160.0 496.0 365.0 17 156.0 297.0 592.0 641.0 12
/home/xxxxx/xxxxx/YOLO_V3/VOC2007/JPEGImages/002284.jpg 65.0 295.0 319.0 680.0 10 299.0 195.0 455.0 649.0 10 410.0 207.0 532.0 554.0 10 523.0 209.0 670.0 706.0 10
/home/xxxxx/xxxxx/YOLO_V3/VOC2007/JPEGImages/001897.jpg 118.0 12.0 413.0 507.0 8 210.0 22.0 488.0 557.0 8 338.0 77.0 603.0 657.0 8 438.0 57.0 658.0 687.0 8