使用背景:多个文件夹及标签.xml文件(labelImg标注)
实现思路:
1.将所有的原始数据.jpg .png .xml全放到一个文件夹
2.图像均转为.jpg格式,放入图像文件夹
3.标签由.xml转为yolov5需要的格式.txt
具体实现:
"""
time:20220729
writer:yohn
function:制作自己的原始数据集,转换成yolo需要的格式
"""
import os
import shutil
import cv2
import xml.etree.ElementTree as ET
#待处理数据文件夹,将图像与标签都放到这个文件夹中
path='.\\imgs'
jpgs_p='.\\images'#存放.jpg
xmls_p='.\\xmls'#存放.xml
label_p='.\\labels' #存放标签.txt
do_flg=0
if not os.path.exists(jpgs_p):#指定要创建的目录
os.mkdir(jpgs_p)
if not os.path.exists(xmls_p):#指定要创建的目录
os.mkdir(xmls_p)
if not os.path.exists(label_p):#指定要创建的目录
os.mkdir(label_p)
for root,dirs,files in os.walk(path,topdown=True):
nums1=0;
nums2=0;
print("开始原数据转换")
for name in files:
#print(name)
str_end = name.split('.')[-1]
if(str_end == 'xml'):
xml_1=os.path.join(root,name)
xml_2 = os.path.join(xmls_p ,name)
#print(xml_1)
#print(xml_2)
shutil.copyfile(xml_1,xml_2)
nums1 += 1
elif(str_end == 'jpg' or str_end == 'JPG'):
jpg_1 = os.path.join(root, name)
jpg_2 = os.path.join(jpgs_p, name)
shutil.copyfile(jpg_1, jpg_2)
nums2 += 1
elif(str_end == 'png'):
jpg_1 = os.path.join(root, name)
jpg_2 = os.path.join(jpgs_p, name.split('.')[0]+'.jpg')
#print(jpg_2)
img=cv2.imread(jpg_1)
#cv2.imshow(name,img)
cv2.imwrite(jpg_2,img)
nums2 += 1
else:
print("未完成转换",name)
print(nums1+nums2)
print("图像/标签总数: ",len(files)," 已完成转换: ",nums1 + nums2," 未完成转换:",len(files)-nums1-nums2)
if(nums1!=nums2):
print("Error! 图像与标签数目不匹配")
else:
do_flg=1
def convert(size, box):
x_center = (box[0] + box[1]) / 2.0
y_center = (box[2] + box[3]) / 2.0
x = x_center / size[0]
y = y_center / size[1]
w = (box[1] - box[0]) / size[0]
h = (box[3] - box[2]) / size[1]
return (x, y, w, h)
def convert_annotation(xml_files_path, save_txt_files_path, classes):
xml_files = os.listdir(xml_files_path)
#print(xml_files)
print("开始标签转换")
for xml_name in xml_files:
#print(xml_name)
xml_file = os.path.join(xml_files_path, xml_name)
out_txt_path = os.path.join(save_txt_files_path, xml_name.split('.')[0] + '.txt')
out_txt_f = open(out_txt_path, 'w')
tree = ET.parse(xml_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
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))
# b=(xmin, xmax, ymin, ymax)
#print(w, h, b)
bb = convert((w, h), b)
out_txt_f.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
if __name__ == "__main__":
classes = ['person', 'face','hand','garb','larwas','conwas','foowas','recyc'] #8类
# 1、voc格式的xml标签文件路径
xml_files1 = r'.\\xmls'
# 2、转化为yolo格式的txt标签文件存储路径
save_txt_files1 = r'.\\labels'
if(do_flg):
convert_annotation(xml_files1, save_txt_files1, classes)
else:
print("图像与标签数目不匹配,请检查原数据!")
print("转换label完成!保存文件至",save_txt_files1)
使用:将数据集划分为训练集、测试集及验证集,在此测试验证放一个了
实现:
1.设置划分数据集的比例,新建不同等级文件夹存放文件
2.读取图像文件夹,分割字符串获取图像名称
3.按照占比将数据分别复制到相应的子文件夹
具体实现:
"""
time:20220729
writer:yohn
function:将数据集分为训练集、测试集和验证集,在此就将测试与验证归为一个了
"""
import os
import shutil
train_p=".\\train"
val_p=".\\val"
imgs_p="images"
labels_p="labels"
#创建训练集
if not os.path.exists(train_p):#指定要创建的目录
os.mkdir(train_p)
tp1=os.path.join(train_p,imgs_p)
tp2=os.path.join(train_p,labels_p)
print(tp1,tp2)
if not os.path.exists(tp1):#指定要创建的目录
os.mkdir(tp1)
if not os.path.exists(tp2): # 指定要创建的目录
os.mkdir(tp2)
#创建测试集文件夹
if not os.path.exists(val_p):#指定要创建的目录
os.mkdir(val_p)
vp1=os.path.join(val_p,imgs_p)
vp2=os.path.join(val_p,labels_p)
print(vp1,vp2)
if not os.path.exists(vp1):#指定要创建的目录
os.mkdir(vp1)
if not os.path.exists(vp2): # 指定要创建的目录
os.mkdir(vp2)
#数据集路径
path1=".\\images"
path2=".\\labels"
#划分数据集,设置数据集数量占比
proportion_ = 0.95 #训练集占比
for root,dirs,files in os.walk(path1,topdown=True):
#print(files) #此处是将所有文件名字一次性取出
nums=0
nums_T=int(len(files)*proportion_)
print("开始数据集划分...")
for file in files:
name = file.split('.')[0]
if(nums <= nums_T):
jpg_1 = os.path.join(path1,file)
jpg_2 = os.path.join(train_p,imgs_p,file)
txt_1 = os.path.join(path2, name + '.txt')
txt_2 = os.path.join(train_p, labels_p, name + '.txt')
shutil.copyfile(jpg_1,jpg_2)
shutil.copyfile(txt_1,txt_2)
nums+=1
else:
jpg_1 = os.path.join(path1, file)
jpg_2 = os.path.join(val_p, imgs_p, file)
txt_1 = os.path.join(path2, name + '.txt')
txt_2 = os.path.join(val_p, labels_p, name + '.txt')
shutil.copyfile(jpg_1, jpg_2)
shutil.copyfile(txt_1, txt_2)
nums+=1
print(nums)
print("数据集划分完成: 总数量:",len(files)," 训练集数量:",nums_T," 测试集数量:",len(files)-nums_T)