概述
xml文件常用于检测任务的标注,里面一般会包含bndox。
关于xml的其他信息可以参考:https://www.cnblogs.com/miniren/p/5092019.html
import xml.dom.minidom
#载入xml文件
DomTree = xml.dom.minidom.parse(xmlfile)
annotation = DomTree.documentElement
# 获取全部的object,并且裁剪出该部分进行保存
objectlist = annotation.getElementsByTagName('object')
# 因为object有很多个,组成一个list
for object in objectlist:
# 下面这两句是得到该object的name,注意两个尖括号夹起来的都是str类型
name = object.getElementsByTagName('name')
name = name[0].childNodes[0].data
bndbox = object.getElementsByTagName('bndbox')
for box in bndbox:
# 比如这个xmin标签加夹起来的数字也是一个str类型
x1 = box.getElementsByTagName('xmin')
x1 = int(x1[0].childNodes[0].data)
y1 = box.getElementsByTagName('ymin')
y1 = int(y1[0].childNodes[0].data)
x2 = box.getElementsByTagName('xmax')
x2 = int(x2[0].childNodes[0].data)
y2 = box.getElementsByTagName('ymax')
y2 = int(y2[0].childNodes[0].data)
w = x2 - x1
h = y2 - y1
img = Image.open(imgfile)
width, height = img.size
minX = max(0, x1)
minY = max(0, y1)
maxX = min(x2, width)
maxY = min(y2, height)
cropbox = (minX, minY, maxX, maxY)
cropedimg = img.crop(cropbox)
cropedimg.save(os.path.join(ProcessedPath, id+'_'+name+ext))
下面是xml文件的举例:
<annotation verified="no">
<folder>明天</folder>
<filename>20190318113128797073_1_warp</filename>
<path>E:未完成明天20190318113128797073_1_warp.jpg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>594</width>
<height>375</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>name</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>118</xmin>
<ymin>39</ymin>
<xmax>198</xmax>
<ymax>71</ymax>
</bndbox>
</object>
<object>
<name>gender</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>115</xmin>
<ymin>92</ymin>
<xmax>141</xmax>
<ymax>115</ymax>
</bndbox>
</object>
<object>
<name>nation</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>232</xmin>
<ymin>90</ymin>
<xmax>256</xmax>
<ymax>116</ymax>
</bndbox>
</object>
<object>
<name>birthdate</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>118</xmin>
<ymin>132</ymin>
<xmax>306</xmax>
<ymax>159</ymax>
</bndbox>
</object>
<object>
<name>address1</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>116</xmin>
<ymin>182</ymin>
<xmax>359</xmax>
<ymax>206</ymax>
</bndbox>
</object>
<object>
<name>address2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>117</xmin>
<ymin>211</ymin>
<xmax>187</xmax>
<ymax>238</ymax>
</bndbox>
</object>
<object>`
<name>idnum</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>207</xmin>
<ymin>293</ymin>
<xmax>524</xmax>
<ymax>324</ymax>
</bndbox>
</object>
</annotation>
最后
以上就是迅速斑马为你收集整理的读取xml文件的全部内容,希望文章能够帮你解决读取xml文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复