实现目标
根据labelme或labelimg生成的xml文件中,有对应图像的路径。当别人帮你打标签时,对应图像的路径是别人电脑上面的,并非是你想要的。
说明 | 路径 | 备注 |
---|
原始xml文件存放路径 | D:\aaa\source/ | 别人帮你打的标签 |
现在xml文件存放路径 | D:\aaa\target/ | 你自己想要放的位置 |
图像路径 | D:\aaa\img/ | 假设你最开始图像就是在这里 |
代码
import xml.etree.ElementTree as ET
import os
# xml文件原始存放路径
source_path = r"D:\aaa\yuanshi/"
# 修改后的xml文件存放路径
target_path = r"D:\aaa\target/"
img_path = r"D:\aaa\img/"
files = os.listdir(source_path)
for xmlFile in files:
if xmlFile.endswith('.xml'):
tree = ET.ElementTree(file=source_path + xmlFile)
root = tree.getroot()
root[0].text = 'images'
root[2].text = img_path + xmlFile
root[2].text = root[2].text.replace('xml', 'jpg')
tree.write(target_path + xmlFile)
print('全部替换完成')