当前位置:网站首页>yolo格式数据集处理(xml转txt)
yolo格式数据集处理(xml转txt)
2022-07-02 12:01:00 【浅念念52】
前言
YOLO网络的数据集是txt文本,当我们想训练一些模型,在网上找的数据都是xml格式,这时候我们需要对数据进行处理,得到我们想要的数据格式。
一、数据处理流程
1.读取xml文件,解析xml 得到图片的宽,高,标定框的坐标信息
2.数据归一化
3.写入txt文件
二、xml文件数据格式

上图所示,是我截取xml文件的部分数据,我们只需要得到size中的width,height和bndbox中的坐标信息。
三、代码
import os
import glob
import xml.etree.ElementTree as ET
xml_file=r'E:\桌面\资料\cv4\数据集\voc数据集\Annotations'
l=['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor']
def convert(box,dw,dh):
x=(box[0]+box[2])/2.0
y=(box[1]+box[3])/2.0
w=box[2]-box[0]
h=box[3]-box[1]
x=x/dw
y=y/dh
w=w/dw
h=h/dh
return x,y,w,h
def f(name_id):
xml_o=open(r'E:\桌面\资料\cv4\数据集\voc数据集\Annotations\%s.xml'%name_id)
txt_o=open(r'E:\桌面\资料\cv4\数据集\voc数据集\labels1\%s.txt'%name_id,'w')
pares=ET.parse(xml_o)
root=pares.getroot()
objects=root.findall('object')
size=root.find('size')
dw=int(size.find('width').text)
dh=int(size.find('height').text)
for obj in objects :
c=l.index(obj.find('name').text)
bnd=obj.find('bndbox')
b=(float(bnd.find('xmin').text),float(bnd.find('ymin').text),
float(bnd.find('xmax').text),float(bnd.find('ymax').text))
x,y,w,h=convert(b,dw,dh)
write_t="{} {:.5f} {:.5f} {:.5f} {:.5f}\n".format(c,x,y,w,h)
txt_o.write(write_t)
xml_o.close()
txt_o.close()
name=glob.glob(os.path.join(xml_file,"*.xml"))
for i in name :
name_id=os.path.basename(i)[:-4]
f(name_id)
总结
以上就是xml转txt文件的全部内容,在使用过程中出现什么问题,可以在评论区留言。
边栏推荐
- SQL 后计算的利器 SPL
- Have you learned the wrong usage of foreach
- Printf function and scanf function in C language
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
- MFC 控制台打印,弹出对话框
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- Common English abbreviations for data analysis (I)
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- 可视化搭建页面工具的前世今生
- AtCoder Beginner Contest 254
猜你喜欢

21_ Redis_ Analysis of redis cache penetration and avalanche

c语言入门--数组

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

C语言习题---(数组)

数据分析思维分析方法和业务知识——业务指标
![[untitled] leetcode 2321 Maximum score of concatenated array](/img/a3/54d0e83f02ef0d0d8d269351c35b39.png)
[untitled] leetcode 2321 Maximum score of concatenated array

做好抗“疫”之路的把关人——基于RK3568的红外热成像体温检测系统

Map介绍

【C语音】详解指针进阶和注意点(2)

Data analysis thinking analysis methods and business knowledge - business indicators
随机推荐
Printf function and scanf function in C language
LeetCode 209. Minimum length subarray
C# richTextBox控制显示最大行数
Tidb data migration tool overview
[noi simulation] Elis (greedy, simulation)
C# 线程传参
Arithmetic operations and related exercises in C language
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
Mfc a dialog calls B dialog function and passes parameters
实用调试技巧
N皇后问题的解决
可视化搭建页面工具的前世今生
How to write sensor data into computer database
.NET Core 日志系统
XML Configuration File
TiDB 软件和硬件环境建议配置
2021-2022学年编译原理考试重点[华侨大学]
16_Redis_Redis持久化
How to test tidb with sysbench
20_Redis_哨兵模式