赞
踩
trainval_percent = 0.95
train_percent = 0.85
xmlfilepath = ‘./datasets/VOC2007/Annotations’
txtsavepath = ‘.datasets/VOC2007/ImageSets/Main’
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)
ftrainval = open(‘./datasets/VOC2007/ImageSets/Main/trainval.txt’, ‘w’)
ftest = open(‘./datasets/VOC2007/ImageSets/Main/test.txt’, ‘w’)
ftrain = open(‘./datasets/VOC2007/ImageSets/Main/train.txt’, ‘w’)
fval = open(‘./datasets/VOC2007/ImageSets/Main/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()
**如果你是使用Labelme生成的.json格式的标签文件,你想要使用VOC格式训练网络,那么你需要使用下面这个小工具将.json格式转换为.xml格式**
#########################4、对.json格式的标签文件进行处理#######################
import os
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split
#1.标签路径
labelme_path = “./xxx/” # 原始xxx标注数据路径,需要更换成自己的数据集名称
saved_path = “./datasets/VOC2007/” # 保存路径
#2.创建要求文件夹
if not os.path.exists(saved_path + “Annotations”):
os.makedirs(saved_path + “Annotations”)
if not os.path.exists(saved_path + “JPEGImages/”):
os.makedirs(saved_path + “JPEGImages/”)
if not os.path.exists(saved_path + “ImageSets/Main/”):
os.makedirs(saved_path + “ImageSets/Main/”)
#3.获取待处理文件
files = glob(labelme_path + “*.json”)
files = [i.split(“/”)[-1].split(“.json”)[0] for i in files]
#4.读取标注信息并写入 xml
for json_file_ in files:
json_filename = labelme_path + json_file_ + “.json”
json_file = json.load(open(json_filename,“r”,encoding=“utf-8”))
height, width, channels = cv2.imread(labelme_path + json_file_ +“.jpg”).shape
with codecs.open(saved_path + “Annotations/”+json_file_ + “.xml”,“w”,“utf-8”) as xml:
xml.write(‘\n’)
xml.write(‘\t’ + ‘UAV_data’ + ‘\n’)
xm
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。