#示例xml
<annotation>
<folder>10-12</folder>
<filename>s_01_zhong2_1206_201709181010-61df44ea9dac31bfd5418032a0d32d0f.jpeg</filename>
<path>C:\Users\admin\Desktop\class_image\class_image\zhong2\1206\20170918\10-12\s_01_zhong2_1206_201709181010-61df44ea9dac31bfd5418032a0d32d0f.jpeg</path>
<source>
<database>Unknown</database>
</source>
<size>
<width>1280</width>
<height>720</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>3-9</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>199</xmin>
<ymin>320</ymin>
<xmax>306</xmax>
<ymax>469</ymax>
</bndbox>
</object>
</annotation>
读取文件
from xml.dom.minidom import parse
def main():
dirPath = './zhang/1'
for imgPath in os.listdir(dirPath):
suffix = imgPath[-4:]
if suffix == 'jpeg':
path = os.path.join(dirPath, imgPath)
xmlPath = path[:-4]+'xml'
print(xmlPath)
# 读取文件
dom = parse(xmlPath)
data = dom.documentElement
# 获取 object
obs = data.getElementsByTagName('object')
for ob in obs:
name = ob.getElementsByTagName('name')[0].childNodes[0].nodeValue
bndbox = ob.getElementsByTagName('bndbox')[0]
xmin = bndbox.getElementsByTagName('xmin')[0].childNodes[0].nodeValue
xmax = bndbox.getElementsByTagName('xmax')[0].childNodes[0].nodeValue
ymin = bndbox.getElementsByTagName('ymin')[0].childNodes[0].nodeValue
ymax = bndbox.getElementsByTagName('ymax')[0].childNodes[0].nodeValue
print(name,xmin,xmax,ymin,ymax)