当前位置:网站首页>将xml标签转换为txt(voc格式转换为yolo方便进行训练)
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
2022-08-04 03:47:00 【风吹落叶花飘荡】
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
文章目录
一、前言
检索一下其他人的之后,感觉没有我的好用,将自己的代码贡献一下
二、程序
import xml.etree.ElementTree as ET
import os
from os import listdir, getcwd
classes = ["bed", "water horse bucket"] # 换上你标签
def convert(size, box):
dw = 1.0 / size[0]
dh = 1.0 / 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(image_name,labelPath):
# in_file = open(os.path.join(labelPath,image_name[:-3] + 'xml')) # xml文件路径
out_file = open(os.path.join(labelPath+'TXT',image_name[:-3] + 'txt'), 'w') # 转换后的txt文件存放路径
in_file = open(os.path.join(labelPath,image_name[:-3] + 'xml')) # xml文件路径
xml_text = in_file.read()
root = ET.fromstring(xml_text)
in_file.close()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
# print(root.iter('object'))
for obj in root.iter('object'):
cls = obj.find('name').text
if cls not in classes:
print(cls)
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)
#print(bb)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
wd = getcwd()
if __name__ == '__main__':
imgNmae=input('输入图像文件夹的绝对地址:')
labelPath=input('输入xml标注的文件夹的绝对地址:')
#imgNmae='E:\数据集\新建文件夹//bed//bed\data\img'
#labelPath='E:\数据集\新建文件夹//bed/bed\data\labels'
if not os.path.isdir(labelPath+'TXT'):
os.mkdir(labelPath+'TXT')
for image_path in os.listdir(imgNmae): # 每一张图片都对应一个xml文件这里写xml对应的图片的路径
#image_name = image_path.split('\\')[-1]
print(image_path)
convert_annotation(image_path,labelPath)
三、跑通演示
1、修改classes为你的数据集的classes
2、运行:输入图像与标注文件夹绝对地址
四、结果如下:
边栏推荐
- 使用serve搭建本地服务器
- 6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
- 自定义通用分页标签01
- 什么是数字孪生智慧城市应用场景
- ingress 待完善
- 张量篇-应用案例
- 深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型
- 一文详解DHCP原理及配置
- SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
- Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
猜你喜欢
如果禁用了安全启动,GNOME 就会发出警告
docker+网桥+redis主从+哨兵模式
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
mq应用场景介绍
sqoop ETL tool
Based on the statistical QDirStat Qt directory
外卖店优先级
逻辑漏洞----其他类型
Learn iframes and use them to solve cross-domain problems
【源码】使用深度学习训练一个游戏
随机推荐
拿捏JVM性能优化(自己笔记版本)
仿牛客论坛项目梳理
机器学习模型的“可解释性”
C language -- ring buffer
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
PHP高级开发案例(1):使用MYSQL语句跨表查询无法导出全部记录的解决方案
STM8S105k4t6c---------------Light up LED
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
SQL injection in #, - +, - % 20, % 23 is what mean?
unsafe.Pointer, pointer, reference in golang
JVM的内存模型简介
Mockito单元测试
How class only static allocation and dynamic allocation
【项目实现】Boost搜索引擎
Mockito unit testing
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
帮助企业实现数字化转型成功的八项指导原则
十一种概率分布
Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology