当前位置:网站首页>Yolo format data set processing (XML to txt)
Yolo format data set processing (XML to txt)
2022-07-02 15:24:00 【Shallow thoughts 52】
List of articles
Preface
YOLO The data set of the network is txt Text , When we want to train some models , The data found on the Internet are xml Format , At this time, we need to process the data , Get the data format we want .
One 、 Data processing flow
1. Read xml file , analysis xml Get the width of the picture , high , Coordinate information of calibration frame
2. Data normalization
3. write in txt file
Two 、xml File data format

The picture above , I intercepted it xml Some data of the file , We just need to get size Medium width,height and bndbox Coordinate information in .
3、 ... and 、 Code
import os
import glob
import xml.etree.ElementTree as ET
xml_file=r'E:\ desktop \ Information \cv4\ Data sets \voc Data sets \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:\ desktop \ Information \cv4\ Data sets \voc Data sets \Annotations\%s.xml'%name_id)
txt_o=open(r'E:\ desktop \ Information \cv4\ Data sets \voc Data sets \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)
summary
That's all xml turn txt The whole content of the document , What problems occur during use , Leave a comment in the comments section .
边栏推荐
猜你喜欢
随机推荐
搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
YOLOV5 代码复现以及搭载服务器运行
How to test tidb with sysbench
【C语音】详解指针进阶和注意点(2)
04_ Stack
16_Redis_Redis持久化
Dragonfly low code security tool platform development path
15_ Redis_ Redis. Conf detailed explanation
表格响应式布局小技巧
FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)
vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)
The past and present lives of visual page building tools
Map introduction
How to choose a third-party software testing organization for automated acceptance testing of mobile applications
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
TiDB混合部署拓扑
03_線性錶_鏈錶
05_队列
19_Redis_宕机后手动配置主机
Record an error report, solve the experience, rely on repetition









