当前位置:网站首页>将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、运行:输入图像与标注文件夹绝对地址
四、结果如下:
边栏推荐
- Returns the maximum number of palindromes in a string
- How to automatically export or capture abnormal login ip and logs in elastic to the database?
- 2022年最新海南建筑八大员(材料员)模拟考试试题及答案
- KingbaseES数据库启动失败,报“内存段超过可用内存”
- Exclude_reserved_words 排除关键字
- new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
- db2中kettle报错 Field [XXX] is required and couldn‘t be found 解决方法
- 复制带随机指针的链表
- kingbaseES V8R2/R3 表在指定表空间,为何显示为默认表空间?
- MySQL query optimization and tuning
猜你喜欢
随机推荐
DIY电工维修如何拆卸和安装开关面板插座
Introduction to mq application scenarios
基于Qt的目录统计QDirStat
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
View mysql deadlock syntax
Reproduce 20-character short domain name bypass
【项目实现】Boost搜索引擎
Architecture of the actual combat camp module three operations
网络工程师入门必懂华为认证体系,附系统学习路线分享
八年软件测试工程师带你了解-测试岗进阶之路
kingbaseES V8R2/R3 表在指定表空间,为何显示为默认表空间?
mq应用场景介绍
if,case,for,while
基地址:环境变量
缓存穿透、缓存击穿、缓存雪崩以及解决方案
Eight guiding principles to help businesses achieve digital transformation success
类如何只能静态分配和只能动态分配
MRS: Alluxio的使用介绍
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
Basic form validation process