如下(已验证)
import os
import os.path
import xml.dom.minidom
path =‘…/datasets/surface/Annotations’
files = os.listdir(path) # 得到文件夹下所有文件名称
s = []
count = 0
for xmlFile in files: # 遍历文件夹
if not os.path.isdir(xmlFile): # 判断是否是文件夹,不是文件夹才打开
name1 = xmlFile.split(‘.’)[0]
dom = xml.dom.minidom.parse(path + ‘/’ + xmlFile)
root = dom.documentElement
def get_data_vaule(style, typename, typevalue, valuename):
nodelist = root.getElementsByTagName(style) # 根据标签的名字获得节点列表
for node in nodelist:
if typevalue == node.getAttribute(typename):
node_name = node.getElementsByTagName(valuename)
value = node_name[0].childNodes[0].nodeValue
return value
return
newpath = root.getElementsByTagName('path')
class_name = get_data_vaule('object', "", "", 'name')
print('class_name:', class_name)
#
# newpath[0].firstChild.data = 'VOCdevkit\VOC2012\JPEGImages' + '\\' + name1 + '.jpg'
#
# with open(os.path.join(path, xmlFile), 'w') as fh:
# dom.writexml(fh)
# print('写入name/pose OK!')
count = count + 1
得到的结果图