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, "个文件")