yolov5数据集制作 数据集分割+voc格式转txt格式代码
先上数据集分割代码 网上没找到合适的 自己写了一个 分割比例为0.6 :0.2 :0.2
import os
import random
oriImgDir = "./change14img"
oriXmlDir = "./change14xml" #源文件夹
desTestImgDir = "./insect/images/test" #目标文件夹
desValImgDir = "./insect/images/val"
desTrainImgDir = "./insect/images/train"
desTestXmlDir = "./insect/labels/test"
desValXmlDir = "./insect/labels/val"
desTrainXmlDir = "./insect/labels/train"
# 获取目录下文件名清单
flist = os.listdir(oriImgDir)
random.shuffle(flist)
length = len(flist)
val = int(length * 0.2)
test = int(length * 0.2)
count = 0
while (count < length):
# 移动测试集
while test:
item = flist[count]
fname = os.path.splitext(item)[0] # 获得不带后缀的文件名
os.rename(oriImgDir + "/" + item, desTestImgDir+"/14"+str(count)+".jpg") # 图片
os.rename(oriXmlDir + "/" + fname + ".xml", desTestXmlDir+"/14"+str(count)+".xml")
test -= 1
count += 1
# 移动验证集
while val:
item = flist[count]
fname = os.path.splitext(item)[0] # 获得不带后缀的文件名
os.rename(oriImgDir + "/" + item, desValImgDir+"/14"+str(count)+".jpg") # 图片
os.rename(oriXmlDir + "/" + fname + ".xml", desValXmlDir+"/14"+str(count)+".xml")
val -= 1
count += 1
# 剩下的移到训练集
item = flist[count]
fname = os.path.splitext(item)[0]
os.rename(oriImgDir + "/" + item, desTrainImgDir+"/14"+str(count)+".jpg") # 图片
os.rename(oriXmlDir + "/" + fname + ".xml", desTrainXmlDir+"/14"+str(count)+".xml")
count += 1
比例可以自己改,不需要验证集的话把第二个while循环去掉即可
os.rename的第二个参数是移动过去之后的文件名,可以改,如果源文件夹有很多个且有重名的情况,第二个参数就需要设计成让移过去之后新的文件名不会重复
os函数不支持跨磁盘移动文件
voc标签转txt格式代码 网上找的 源代码地址:
https://github.com/DLLXW/objectDetectionDatasets
'''
2020/6/15,标注文件转换xml转txt(vol to yolo)转完后需添加labels文件,即数字序号对应的标签名。
'''
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
classes = ['0', '1', '2', '3', '4', '5', '6', '7', '9', '10', '12', '13', '14']
def convert(size, box):
dw = 1./(size[0])
dh = 1./(size[1])
x = (box[0] + box[1])/2.0 - 1
y = (box[2] + box[3])/2.0 - 1
w = box[1] - box[0]
h = box[3] - box[2]
x = x*dw
w = w*dw
y = y*dh
h = h*dh
if w>=1:
w=0.99
if h>=1:
h=0.99
return (x,y,w,h)
def convert_annotation(rootpath,xmlname):
xmlfile = os.path.join(xmlpath,xmlname)
with open(xmlfile, "r", encoding='UTF-8') as in_file:
txtname = xmlname[:-4]+'.txt'
print(txtname)
txtpath = 'yolov5txt/train'#生成的.txt文件会被保存在这里
if not os.path.exists(txtpath):
os.makedirs(txtpath)
txtfile = os.path.join(txtpath,txtname)
with open(txtfile, "w+" ,encoding='UTF-8') as out_file:
tree=ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
out_file.truncate()
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult)==1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
if __name__ == "__main__":
xmlpath='insect/labels/train'
list=os.listdir(xmlpath)
for i in range(0,len(list)) :
path = os.path.join(xmlpath,list[i])
if ('.xml' in path)or('.XML' in path):
convert_annotation(xmlpath,list[i])
print('done', i)
else:
print('not xml file',i)
class列表里放的是类别,改成你自己的类别名称,类别如果没添加进去的话就会出现转化出来的txt是空的的情况