当前位置:网站首页>将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、运行:输入图像与标注文件夹绝对地址
四、结果如下:
边栏推荐
- 一个属于程序员的七夕节!
- 基于Qt的目录统计QDirStat
- y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
- 6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
- How class only static allocation and dynamic allocation
- A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
- [Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
- 移动支付线上线下支付场景
- 数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书
- 2003. 每棵子树内缺失的最小基因值 DFS
猜你喜欢
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
LeetCode每日一题(2285. Maximum Total Importance of Roads)
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
if,case,for,while
Basic form validation process
6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
从图文展示到以云为核,第五代验证码独有的策略情报能力
Sfdp 超级表单开发平台 V6.0.5 正式发布
RSS订阅微信公众号初探-feed43
随机推荐
Hey, I had another fight with HR in the small group!
怎么把elastic中的异常登录ip和日志自动导出或抓取到数据库中?
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
The general SQL injection flow (sample attached)
Architecture of the actual combat camp module three operations
LeetCode每日一题(2285. Maximum Total Importance of Roads)
sql注入一般流程(附例题)
docker+网桥+redis主从+哨兵模式
XSS related knowledge points
DIY电工维修如何拆卸和安装开关面板插座
SQL injection in #, - +, - % 20, % 23 is what mean?
函数,递归以及dom简单操作
Polygon zkEVM network node
逻辑漏洞----其他类型
劝退背后。
十一种概率分布
SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropri
跨境电商看不到另一面:商家刷单、平台封号、黑灰产牟利