#!/usr/bin/env python
# vim:set fileencoding=utf-8:
#读取xml文件存入xml_list
#随机读取xml_list的内容,按照比例存储在对应的txt中
#保存txt文件
import os, random
import copy#use copy.deepcopy to copy list
#xml的路径以及分配后的txt存储路径
xml_path = '/home/lys/py-faster-rcnn/data/mydata/image_with_object/Annotations/'
txt_path = '/home/lys/py-faster-rcnn/data/mydata/image_with_object/Main/'
test_percent = 0.2#测试集所占比例
val_percent = 0.2#验证集所占比例
xml_list = os.listdir(xml_path)
#remove the extend .xml
xml_list = [xml.split('.')[0] for xml in xml_list]
xml_len = len(xml_list)
#allot randomly
test = random.sample(xml_list, int(xml_len * test_percent))
trainval = copy.deepcopy(xml_list)
for xml in test:
trainval.remove(xml)
val = random.sample(trainval, int(len(trainval) * val_percent))
train = copy.deepcopy(trainval)
for xml in val:
train.remove(xml)
#renew txt and write
trainval_path = open(os.path.join(txt_path,'trainval.txt'),'w+')
train_path = open(os.path.join(txt_path,'train.txt
分配训练和测试数据集(python)——Faster-RCNN
最新推荐文章于 2022-12-14 23:56:24 发布