labelimage标注label批量修改标签

发布时间:2024年01月10日
import os
import xml.etree.ElementTree as ET

def modify_xml_labels(xml_folder):
    modified_files = []
    for root, dirs, files in os.walk(xml_folder):
        for file in files:
            if file.endswith(".xml"):
                xml_path = os.path.join(root, file)
                tree = ET.parse(xml_path)
                root_elem = tree.getroot()
                modified = False
                for obj in root_elem.findall("object"):
                    name = obj.find("name")
                    if name.text == "faultd":
                        name.text = "fault"
                        modified = True
                    elif name.text == "nohelmet_manned":
                        name.text = "manned_without_helmet"
                        modified = True
                    elif name.text == "helmet_manned":
                        name.text = "manned_helmet"
                        modified = True
                    elif name.text == "nohelmet":
                        name.text = "no_helmet"
                        modified = True
                    elif name.text == "递罚单":
                        name.text = "deliver_documents"
                        modified = True
                    elif name.text == "递证件":
                        name.text = "deliver_documents"
                        modified = True
                    elif name.text == "RMB_100":
                        name.text = "rmb"
                        modified = True
                if modified:
                    tree.write(xml_path)
                    modified_files.append(file)
    return modified_files


# 定义xml文件夹路径
xml_folder = r"D:\PaddleOCR-release-2.7\xml"

# 执行xml文件的标签修改,并输出被修改的文件名信息
modified_files = modify_xml_labels(xml_folder)
for file in modified_files:
    print(file)

# 统计被修改的文件数量
num_of_modified_files = len(modified_files)
print("共修改了", num_of_modified_files, "个文件")
文章来源:https://blog.csdn.net/qq_49627063/article/details/135508938
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。