当前位置:网站首页>Converts XML tags to TXT format (voc conversion for yolo convenient training)
Converts XML tags to TXT format (voc conversion for yolo convenient training)
2022-08-04 04:15:00 【The wind blows the fallen leaves and flowers】
将xml标签转换为txt(voc格式转换为yolo方便进行训练)
文章目录
一、前言
After retrieving someone else's,It doesn't feel as good as mine,Contribute your own code
二、程序
import xml.etree.ElementTree as ET
import os
from os import listdir, getcwd
classes = ["bed", "water horse bucket"] # Change your label
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('Enter the absolute address of the image folder:')
labelPath=input('输入xmlThe absolute address of the marked folder:')
#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)
三、Run through the demo
1、修改classesfor your datasetclasses
2、运行:Enter the absolute address of the image and annotation folder
四、结果如下:
边栏推荐
- 看DevExpress丰富图表样式,如何为基金公司业务创新赋能
- This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
- 42. 接雨水
- 2.15 keil使用电脑端时间日期
- 7-2 LVS+DR Overview and Deployment
- 8. Haproxy builds a web cluster
- docker安装mysql与宿主机相差8小时的问题。
- base address: environment variable
- Take care of JVM performance optimization (own note version)
- Enterprise live broadcast is on the rise: Witnessing focused products, micro-like embracing ecology
猜你喜欢
随机推荐
帮助企业实现数字化转型成功的八项指导原则
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
Stop behind.
This Thursday evening at 19:00, the fourth live broadcast of knowledge empowerment丨The realization of equipment control of OpenHarmony smart home project
如何动态添加script依赖的脚本
张量篇-应用案例
汇编语言之栈
2022杭电多校联赛第五场 题解
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
pnpm 是凭什么对 npm 和 yarn 降维打击的
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
【21天学习挑战赛】图像的旋转问题(二维数组)
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
基地址:环境变量
缓存穿透、缓存击穿、缓存雪崩以及解决方案
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
八年软件测试工程师带你了解-测试岗进阶之路
机器学习模型的“可解释性”