在我自己的实验中,/home/xiaobumi/SSD-Tensorflow-master/VOC/Annotations/下为所有图片对应的xml文件(包含训练和检测),通过下方代码可以成功分为训练和检测的txt文件。
import osimport randomxmlfilepath = r'/home/xiaobumi/SSD-Tensorflow-master/VOC/Annotations'saveBasePath = r"/home/xiaobumi/dataset/txt"trainval_percent = 0.7train_percent = 0.7total_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/VOC/Annotations/'+name)if i in train:ftrain.write('/home/xiaobumi/SSD-Tensorflow-master/VOC/Annotations/'+name)else:fval.write('/home/xiaobumi/SSD-Tensorflow-master/VOC/Annotations/'+name)else:ftest.write('/home/xiaobumi/SSD-Tensorflow-master/VOC/Annotations/'+name)ftrainval.close()ftrain.close()fval.close()ftest.close()
如果觉得《VOC数据集将XML转为对应的train.txt trainval.txt val.txt test.txt》对你有帮助,请点赞、收藏,并留下你的观点哦!