|------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| import os import json import xml.etree.ElementTree as ET def convert_json_to_xml(json_data): root = ET.Element("annotation") filename = ET.SubElement(root, "filename") filename.text = json_data["imagePath"] object_num = ET.SubElement(root, "object_num") object_num.text = str(len(json_data["shapes"])) size = ET.SubElement(root, "size") width = ET.SubElement(size, "width") width.text = str(json_data["imageWidth"]) height = ET.SubElement(size, "height") height.text = str(json_data["imageHeight"]) for shape in json_data["shapes"]: object_element = ET.SubElement(root, "object") name = ET.SubElement(object_element, "name") name.text = shape["label"] difficult = ET.SubElement(object_element, "difficult") difficult.text = "0" bndbox = ET.SubElement(object_element, "bndbox") xmin = ET.SubElement(bndbox, "xmin") xmin.text = str(int(shape["points"][0][0])) ymin = ET.SubElement(bndbox, "ymin") ymin.text = str(int(shape["points"][0][1])) xmax = ET.SubElement(bndbox, "xmax") xmax.text = str(int(shape["points"][1][0])) ymax = ET.SubElement(bndbox, "ymax") ymax.text = str(int(shape["points"][1][1])) return ET.tostring(root, encoding="unicode") current_dir = os.getcwd() parent_dir = os.path.abspath(os.path.join(current_dir, os.pardir)) output_dir = os.path.join(parent_dir, "xml_output") if not os.path.exists(output_dir): os.makedirs(output_dir) for file in os.listdir(current_dir): if file.endswith(".json"): with open(os.path.join(current_dir, file), "r") as json_file: json_data = json.load(json_file) xml_data = convert_json_to_xml(json_data) xml_filename = file.replace(".json", ".xml") output_path = os.path.join(output_dir, xml_filename) with open(output_path, "w") as xml_file: xml_file.write(xml_data) print("所有JSON文件已经转换为XML并保存在文件夹 xml_output 中")
|
数据集标注json转xml脚本
未经允许不得转载:工具盒子 » 数据集标注json转xml脚本