当前位置:网站首页>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文件的全部内容,在使用过程中出现什么问题,可以在评论区留言。
边栏推荐
- Why can't programmers who can only program become excellent developers?
- 哈夫曼树:(1)输入各字符及其权值(2)构造哈夫曼树(3)进行哈夫曼编码(4)查找HC[i],得到各字符的哈夫曼编码
- 【C语言】详解指针的初阶和进阶以及注意点(1)
- Tidb cross data center deployment topology
- Mavn builds nexus private server
- CDN 在游戏领域的应用
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
- TiDB 集群最小部署的拓扑架构
- C thread transfer parameters
- Implementation of n queen in C language
猜你喜欢
Table responsive layout tips
[noi Simulation Competition] scraping (dynamic planning)
[development environment] install the visual studio community 2013 development environment (download the installation package of visual studio community 2013 with update 5 version)
学习使用php将时间戳转换为大写日期的方法代码示例
Map介绍
Map introduction
Tidb data migration tool overview
. Net core logging system
[untitled] leetcode 2321 Maximum score of concatenated array
05_队列
随机推荐
Niuke Practice 101
04_ Stack
Map introduction
[C language] explain the initial and advanced levels of the pointer and points for attention (1)
LeetCode_ String_ Simple_ 412.Fizz Buzz
原则、语言、编译、解释
MFC A对话框调用B对话框函数并传参
二叉树的遍历方式主要有:先序遍历、中序遍历、后序遍历、层次遍历。先序、中序、后序其实指的是父节点被访问的次序。若在遍历过程中,父节点先于它的子节点被访问,就是先序遍历;
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language
Mfc a dialog calls B dialog function and passes parameters
Learn the method code example of converting timestamp to uppercase date using PHP
Internet Explorer officially retired
HUSTPC2022
C#延时、在线程中开启定时器、获取系统时间
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Application and practice of Jenkins pipeline
16_Redis_Redis持久化
12_Redis_Bitmap_命令
AtCoder Beginner Contest 254