四 数据集处理之使xml文件中filename与文件名一致(批量)

# 成功使得filename与文件名一致
import os
# input_dir='./xml'
input_dir = r"D:/xml-test/4"  # xml路径
import xml.etree.ElementTree as ET
for filename in os.listdir(input_dir):
    file_path = os.path.join(input_dir, filename)
    out_path = 'D:/xml-test/annotation'+'/'+filename
    # 生成新的路径
    dom = ET.parse(file_path)
    root = dom.getroot()
    for ann in root.iter('annotation'):
        # 获取filename节点
        ann.find('filename').text = filename[:-4]+'.bmp'
    dom.write(out_path, xml_declaration=True)

 

posted @ 2021-05-11 20:14  下着雨  阅读(292)  评论(0编辑  收藏  举报