深度学习数据整理——Python读写xml文件

在训练CNN或是其他的深度学习网络时,经常要做的一件事便是准备数据,这是一件比较“烦人”的事情,此处以Python为基础整理几个经常能用到的数据处理脚本以及需要注意的地方。

Python读写xml文件

xml是常见的数据标注格式,下面给出利用Python读写xml文件的脚本,首先是利用Python读取xml文件的脚本:

@requires_authorization
import xml.etree.cElementTree as et
'''
调用xml库中的相关方法可以快速实现xml文件中的信息的读取
此处读取的xml文件的格式为VOC中的数据标注格式,以下脚本展示了
读取所有的bounding box的坐标的方法,相应的可以按照以下方式
读取其他的信息。
'''

tree=et.parse("000001.xml")
root=tree.getroot()

filename=root.find('filename').text
print filename

for Object in root.findall('object'):
    name=Object.find('name').text
    print name
    bndbox=Object.find('bndbox')
    xmin=bndbox.find('xmin').text
    ymin=bndbox.find('ymin').text
    xmax=bndbox.find('xmax').text
    ymax=bndbox.find('ymax').text
    print xmin,ymin,xmax,ymax

下面给出利用Python编辑xml文件的脚本,并存储到相应的文件中的脚本

@requires_authorization
from lxml.etree import Element,SubElement,tostring
import pprint
from xml.dom.minidom import parseString
import sys
'''
调用lxml库中的相关方法可以快速实现xml文件中的信息的编辑与存储
在以下的脚本中,首先将输出重定向到文件(注意文件的打开方式),然后构建
xml中的相关的节点信息,最后将构建好的xml信息输出到文件,可以看到以lxml进行xml书写的方法还是比较简单的。
'''

savedStdout=sys.stdout

f=open("test.xml","w+")
sys.stdout=f

node_root=Element('annotation')

node_folder=SubElement(node_root,'folder')
node_folder.text="GTSDB"

node_filename=SubElement(node_root,'filename')
node_filename.text="000001.jpg"

node_size=SubElement(node_root,"szie")
node_width = SubElement(node_size, 'width')
node_width.text = '500'

node_height = SubElement(node_size, 'height')
node_height.text = '375'

node_depth = SubElement(node_size, 'depth')
node_depth.text = '3'

node_object = SubElement(node_root, 'object')
node_name = SubElement(node_object, 'name')
node_name.text = 'mouse'
node_difficult = SubElement(node_object, 'difficult')
node_difficult.text = '0'
node_bndbox = SubElement(node_object, 'bndbox')
node_xmin = SubElement(node_bndbox, 'xmin')
node_xmin.text = '99'
node_ymin = SubElement(node_bndbox, 'ymin')
node_ymin.text = '358'
node_xmax = SubElement(node_bndbox, 'xmax')
node_xmax.text = '135'
node_ymax = SubElement(node_bndbox, 'ymax')
node_ymax.text = '375'

xml = tostring(node_root, pretty_print=True)  
dom = parseString(xml)
print xml 
f.close()
sys.stdout=savedStdout

最后给出一个编写VOC标注文件的脚本

@requires_authorization

'''
注意:此脚本不可运行,只是给出了VOC标注格式的案例,方便大家复制,其中的filename的信息,以及图片的大小信息,目标的标注位置信息没有给出,在操作时根据自己的需求填写即可!
'''

 node_root=Element('annotation')
 node_folder=SubElement(node_root,'folder')
 node_folder.text="VOC2007"
 node_filename=SubElement(node_root,'filename')
 node_filename.text=filename 
 node_source=SubElement(node_root,'source')
 node_database=SubElement(node_source,'database')
 node_database.text='The VOC2007 Database'
 node_annotation2=SubElement(node_source,'annotation')
 node_image=SubElement(node_source,'image')
 node_image.text="flickr"
 node_flickrid=SubElement(node_source,'flickrid')
 node_flickrid.text="NULL"
 node_owner=SubElement(node_root,'owner')
 node_flickrid2=SubElement(node_owner,'flickrid')
 node_flickrid2.text="xcz"
 node_name=SubElement(node_owner,'name')
 node_name.text="xcz"
 node_size=SubElement(node_root,'size')
 node_width=SubElement(node_size,'width')
 node_height=SubElement(node_size,'height')
 node_depth=SubElement(node_size,'depth')
 node_segmented=SubElement(node_root,'segmented')
 node_segmented.text='0'
 node_depth.text='3'
 img_size=root.find('size')
 node_width.text=img_size.find('width').text
 node_height.text=img_size.find('height').text

猜你喜欢

转载自blog.csdn.net/xczexcel/article/details/75145026
今日推荐