一、VOC格式
二、yolo格式
三、VOC转yolo格式
将已经下载好的VOC数据集图片全放在JPEGImages文件夹里面,如果想要VOC所有的数据集,就将其他版本的数据集拷贝进来(比如我是将VOC2007的所有数据集拷贝了进来),xml文件放到Annotations文件夹下
- 目录机构
- 注意如果有其他拷贝进来的数据集,要将main文件夹下的train.txt、val.txt、trainval.txt、test.txt全部合并。
- main.py代码
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = [('2012', 'train'), ('2012', 'val'), ('2012', 'test')]
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus",
"car", "cat", "chair", "cow", "diningtable", "dog", "horse",
"motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(year, image_id):
in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml' % (year, image_id))
out_file = open('VOCdevkit/VOC%s/labels/%s.txt' % (year, image_id), 'w')
tree = ET.parse(in_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))
bb = convert((w, h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
for year, image_set in sets:
if not os.path.exists('VOCdevkit/VOC%s/labels/' % (year)):
os.makedirs('VOCdevkit/VOC%s/labels/' % (year))
image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'
% (year, image_set)).read().strip().split()
list_file = open('%s_%s.txt' % (year, image_set), 'w')
for image_id in image_ids:
list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg\n'
% (wd, year, image_id))
convert_annotation(year, image_id)
list_file.close()
- 注意核对自己的路径
- 运行程序之后的目录结构
会多出来一个文件夹+四个文件
labels下面是所有的VOC转化为yolo之后的txt文件
2012_test.txt、2012_train.txt、2012_val.txt里面存放的是测试集、训练集、验证集的图片存放路径
注:本文是参考这篇博客的基础上自己写的文章,感兴趣的朋友也可以看看
另外:
只讲xml格式转换为yolo格式(文件名是xml的文件名)
import re
import os
def to_one(name_list, xmin, ymin, xmax, ymax, width, height, name):
data = []
num = 0
for x1, y1, x2, y2 in zip(xmin, ymin, xmax, ymax):
x1 = float(x1)
y1 = float(y1)
x2 = float(x2)
y2 = float(y2)
w1 = float(width[0])
h1 = float(height[0])
x = (x2 - x1) / 2 + x1
y = (y2 - y1) / 2 + y1
w = x2 - x1
h = y2 - y1
x = x / w1
y = y / h1
w = w / w1
h = h / h1
data.append(' '.join([str(num), str(x), str(y), str(w), str(h),'\n']))
# num += 1
# print(data)
with open("%s.txt" % name, 'w') as f:
f.writelines(data)
def xml_to_yolo(path):
files_list = os.listdir(path)
files_path = []
for file in files_list:
files_path.append(os.path.join(path, file))
for file in files_path:
with open(file, 'r') as f:
data = f.read()
name_list = re.findall('<name>(.*?)</name>', data)
xmin = re.findall('<xmin>(.*?)</xmin>', data)
ymin = re.findall('<ymin>(.*?)</ymin>', data)
xmax = re.findall('<xmax>(.*?)</xmax>', data)
ymax = re.findall('<ymax>(.*?)</ymax>', data)
width = re.findall('<width>(.*?)</width>', data)
height = re.findall('<height>(.*?)</height>', data)
to_one(name_list, xmin, ymin, xmax, ymax, width, height,file.split('\\')[6].split('.')[0])
def make_dir():
if os.path.exists('.//lables'):
os.chdir(".//lables")
else:
os.makedirs(".//lables")
os.chdir(".//lables")
if __name__ == '__main__':
i = 1
make_dir()
xml_to_yolo('E:\\pythonProject\\pascalVOC2yolov5\\VOCdevkit\\VOC2012\\Annotations')
print('完成')
版权声明:本文为CSDN博主「-添砖Java」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42911028/article/details/121787164
暂无评论