import os
import random
trainval_percent = 0.8
train_percrent = 0.8
xmlfilepath = '/home/james/DronCV/VOC2019/Annotations' #change to your Annotations path
txtsavepath = '/home/james/DronCV/VOC2019/ImageSets/Main' #change to your path
total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num*trainval_percent)
tr = int(tv*trainval_percent)
trainval = random.sample(list,tv)
train = random.sample(trainval,tr)
ftrainval = open(txtsavepath + '/trainval.txt','w')
ftest = open(txtsavepath + '/test.txt','w')
ftrain = open(txtsavepath + '/train.txt','w')
fval = open(txtsavepath + '/val.txt','w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()