VOC数据集将XML转为对应的train.txt,trainval.txt,val.txt,test.txt

 

 

 

在我自己的实验中,/home/xiaobumi/SSD-Tensorflow-master/VOC2007/Annotations/下为所有图片对应的xml文件(包含训练和检测),通过下方代码可以成功分为训练和检测的txt文件。

import os
import random

xmlfilepath = r'/home/xiaobumi/SSD-Tensorflow-master/VOC2007/Annotations'
saveBasePath = r"/home/xiaobumi/dataset/txt"

trainval_percent = 0.7
train_percent = 0.7
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)

print("train and val size", tv)
print("traub suze", tr)
ftrainval = open(os.path.join(saveBasePath, 'trainval.txt'), 'w')
ftest = open(os.path.join(saveBasePath, 'test.txt'), 'w')
ftrain = open(os.path.join(saveBasePath, 'train.txt'), 'w')
fval = open(os.path.join(saveBasePath, 'val.txt'), 'w')

for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        ftrainval.write('/home/xiaobumi/SSD-Tensorflow-master/VOC2007/Annotations/'+name)
        if i in train:
            ftrain.write('/home/xiaobumi/SSD-Tensorflow-master/VOC2007/Annotations/'+name)
        else:
            fval.write('/home/xiaobumi/SSD-Tensorflow-master/VOC2007/Annotations/'+name)
    else:
        ftest.write('/home/xiaobumi/SSD-Tensorflow-master/VOC2007/Annotations/'+name)

ftrainval.close()
ftrain.close()
fval.close()
ftest.close()

 


版权声明:本文为xiaobumi123原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。