当前位置:网站首页>VOC格式标签转YOLO格式
VOC格式标签转YOLO格式
2022-08-09 07:30:00 【qq_52217283】
VOC转Yolo格式代码:
import os
import xml.etree.ElementTree as ET
# 将x1, y1, x2, y2转换成yolo所需要的x, y, w, h格式
def voc_to_yolo_wh(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[2]) / 2 * dw
y = (box[1] + box[3]) / 2 * dh
w = (box[2] - box[0]) * dw
h = (box[3] - box[1]) * dh
return x, y, w, h # 返回的都是标准化后的值
# 获取所以xml里面的classes类别,返回(列表/数组)
def get_cls_list(xml_path):
result_list = []
for file in os.listdir(xml_path):
tree = ET.parse(xml_path + file)
root = tree.getroot()
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in result_list:
result_list.append(cls)
return result_list
def voc_to_yolo(xml_path, save_path, classes):
# 可以打印看看该路径是否正确
if not os.path.exists(xml_path):
print("XML路径不存在")
return []
if len(os.listdir(xml_path)) == 0:
print("XML文件目录为空")
return []
if not os.path.exists(save_path):
os.makedirs(save_path)
if not classes:
classes = get_cls_list(xml_path)
# 遍历每一个xml文件
for file in os.listdir(xml_path):
label_file = xml_path + file
save_file = save_path + file.replace('xml', 'txt')
out_file = open(save_file, 'w')
# print(label_file)
# 开始解析xml文件
tree = ET.parse(label_file)
root = tree.getroot()
size = root.find('size') # 图片的shape值
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
# 将名称转换为id下标
cls_id = classes.index(cls)
# 获取整个bounding box框
bnd_box = obj.find('bndbox')
# xml给出的是x1, y1, x2, y2
box = [float(bnd_box.find('xmin').text), float(bnd_box.find('ymin').text), float(bnd_box.find('xmax').text),
float(bnd_box.find('ymax').text)]
# 将x1, y1, x2, y2转换成yolo所需要的x_center, y_center, w, h格式
bbox = voc_to_yolo_wh((w, h), box)
# print(save_file)
# 写入目标文件中,格式为 id x y w h
out_file.write(str(cls_id) + " " + " ".join(str(x) for x in bbox) + '\n')
return classes
if __name__ == '__main__':
# 这里要改成自己数据集路径
xml_path_ = 'Annotations/xml/'
# 这里要改成转换后保存的数据集路径
save_path_ = 'Annotations/labels/'
# 数据集标签,为空则自动获取标签,只需要在函数返回结果里获取标签就可以了
Class_Name = []
result_classes = voc_to_yolo(xml_path_, save_path_, Class_Name)
print("数据集标签:", result_classes)
边栏推荐
猜你喜欢
高项 03 项目立项管理
什么是分布式事务
【修电脑】系统重装但IP不变后VScode Remote SSH连接失败解决
web自动化测试有哪些工具和框架?
分布式事务产生的原因
Laravel文档阅读笔记-Rendering JSON(对JS变量进行赋值)
【nuxt】服务器部署步骤
【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
SA-Siam:用于实时目标跟踪的双重连体网络A Twofold Siamese Network for Real-Time Object Tracking
【Oracle 11g】Redhat 6.5 安装 Oracle11g
随机推荐
找出数组中不重复的值php
排序第三节——交换排序(冒泡排序+快速排序+快排的优化)(5个视频讲解)
redis学习笔记
eyb:Redis学习(2)
Pytorch 训练技巧
Classes and Structures
高项 03 项目立项管理
买口罩(0-1背包)
【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
DSP+ARM+FPGA高速PCIE/千兆网口信号仿真介绍
灵活好用的sql monitoring 脚本 part7
MySQL高级特性之分布式(XA)事务的介绍
(本章节完结)排序第五节——非比较排序(计数排序+基数排序+桶排序)(附有自己的视频讲解)
力扣208,实现Trie(前缀树)
力扣 636. 函数的独占时间
leetcode:55. 跳跃游戏
stm32定时器之简单封装
Invoker 2019CCPC秦皇岛站I题 简单DP
常用测试用例设计方法之正交实验法详解
【修电脑】系统重装但IP不变后VScode Remote SSH连接失败解决