大家应该都有把标签名字写错但是没发现的时候,在训练数据的时候发现有问题了,但是重新找比较困难,我在打讯飞智能车和睿抗的时候就遇到过,为了避免labelimg重新检查就写了这个程序,只需将错误标签名替换成正确标签名即可(所有代码只在ubuntu上进行了使用,其他平台不能保证正确)
import os
import xml.etree.ElementTree as ET
def modify_xml_files_in_directory(directory_path, old_value, new_value):
# 遍历目录中的所有文件
for filename in os.listdir(directory_path):
if filename.endswith('.xml'):
file_path = os.path.join(directory_path, filename)
modify_xml_file(file_path, old_value, new_value)
def modify_xml_file(file_path, old_value, new_value):
try:
# 解析XML文件
tree = ET.parse(file_path)
root = tree.getroot()
# 遍历所有元素
for elem in root.iter():
# 检查标签内容是否为旧值,如果是则替换为新值
if elem.text == old_value:
elem.text = new_value
# 将修改后的XML保存回同一文件
tree.write(file_path, encoding='utf-8', xml_declaration=True)
print(f"文件 {file_path} 已修改")
except Exception as e:
print(f"修改文件 {file_path} 时出错: {e}")
# 示例使用
directory_path = "Annotations" # 替换为包含XML文件的目录路径
old_value = 'papper' # 要替换的旧值
new_value = 'paper' # 替换后的新值
modify_xml_files_in_directory(directory_path, old_value, new_value)

被折叠的 条评论
为什么被折叠?



