当前位置:网站首页>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
四、结果如下:
边栏推荐
- 4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
- 数据治理平台项目总结和分析
- Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
- 【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
- 七夕节,我用代码制作了表白信封
- 出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
- docker安装mysql与宿主机相差8小时的问题。
- 转:管理是对可能性的热爱,管理者要有闯进未知的勇气
- 劝退背后。
- Introduction to mq application scenarios
猜你喜欢
2千兆光+6千兆电导轨式网管型工业级以太网交换机支持X-Ring冗余环网一键环网交换机
SQL query String field less than 10 how to check
drools from download to postman request success
Deep learning -- CNN clothing image classification, for example, discussed how to evaluate neural network model
7-1 LVS+NAT load balancing cluster, NAT mode deployment
【MD5】采用MD5+盐的加密方式完成注册用户和登录账号
学会iframe并用其解决跨域问题
ADC噪声全面分析 -03- 利用噪声分析进行实际设计
Functions, recursion and simple dom operations
y86.第四章 Prometheus大厂监控体系及实战 -- prometheus存储(十七)
随机推荐
【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network
unity框架之缓存池
数组相关 内容 解析
转:管理是对可能性的热爱,管理者要有闯进未知的勇气
drools从下载到postman请求成功
类如何只能静态分配和只能动态分配
FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
什么是数字孪生智慧城市应用场景
Explain detailed explanation and practice
XSS相关知识点
中信证券网上开户怎么开的?安全吗?
Functions, recursion and simple dom operations
Shell 函数
mysql index notes
技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
SVM介绍以及实战
MySQL query optimization and tuning
PL/SQL Some Advanced Fundamental
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型