制作VOC数据集时生成,,代码

时间:2025-04-02 09:09:31
import os
import random
import sys

if len() < 2:
    print("no directory specified, please input target directory")
    exit()

root_path = [1]

xmlfilepath = root_path + '/Annotations'

txtsavepath = root_path + '/ImageSets/Main'

if not (root_path):
    print("cannot find such directory: " + root_path)
    exit()

if not (txtsavepath):
    (txtsavepath)

trainval_percent = 0.9
train_percent = 0.8
total_xml = (xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = (list, tv)
train = (trainval, tr)

print("train and val size:", tv)
print("train size:", tr)

ftrainval = open(txtsavepath + '/', 'w')
ftest = open(txtsavepath + '/', 'w')
ftrain = open(txtsavepath + '/', 'w')
fval = open(txtsavepath + '/', 'w')

for i in list:
    name = total_xml[i][:-4] + '\n'
    if i in trainval:
        (name)
        if i in train:
            (name)
        else:
            (name)
    else:
        (name)

()
()
()
()

定位到文件,输入

python   path

是你自己命名的,path路径指定到Annotations