介绍
json转xml格式,代码1代码2适用于两种json格式
其他转换:
自动xml转txt,最简单,不报错
代码
代码1
最常见的json格式,coco数据集标准格式,包含images,categories,annotations,
以下是instances_val2017.json的具体内容:
{
"images": [
{
"file_name": "20230707_151435.png",
"height": 1080,
"width": 1920,
"id": 1
},
{
"file_name": "20230707_151559.png",
"height": 1080,
"width": 1920,
"id": 2
},
{
"file_name": "20230710_101945.png",
"height": 1080,
"width": 1920,
"id": 3
},
{
"file_name": "20230710_101949.png",
"height": 1080,
"width": 1920,
"id": 4
}
],
"categories": [
{
"supercategory": "3_2",
"id": 1,
"name": "3_2"
},
{
"supercategory": "9_1",
"id": 2,
"name": "9_1"
},
{
"supercategory": "2_1",
"id": 3,
"name": "2_1"
},
{
"supercategory": "8",
"id": 4,
"name": "8"
},
{
"supercategory": "10_1",
"id": 5,
"name": "10_1"
},
{
"supercategory": "10_2",
"id": 6,
"name": "10_2"
},
{
"supercategory": "9_2",
"id": 7,
"name": "9_2"
},
{
"supercategory": "2_2",
"id": 8,
"name": "2_2"
},
{
"supercategory": "3_1",
"id": 9,
"name": "3_1"
}
],
"annotations": [
{
"image_id": 1,
"bbox": [
761,
409,
191,
422
],
"category_id": 7,
"iscrowd": 0,
"id": 1,
"area": 80602
},
{
"image_id": 1,
"bbox": [
924,
395,
177,
427
],
"category_id": 7,
"iscrowd": 0,
"id": 2,
"area": 75579
},
{
"image_id": 1,
"bbox": [
1094,
393,
163,
430
],
"category_id": 2,
"iscrowd": 0,
"id": 3,
"area": 70090
},
{
"image_id": 1,
"bbox": [
1262,
399,
154,
431
],
"category_id": 2,
"iscrowd": 0,
"id": 4,
"area": 66374
},
{
"image_id": 2,
"bbox": [
818,
257,
214,
458
],
"category_id": 7,
"iscrowd": 0,
"id": 5,
"area": 98012
},
{
"image_id": 2,
"bbox": [
998,
263,
197,
470
],
"category_id": 7,
"iscrowd": 0,
"id": 6,
"area": 92590
},
{
"image_id": 2,
"bbox": [
1188,
261,
173,
475
],
"category_id": 2,
"iscrowd": 0,
"id": 7,
"area": 82175
},
{
"image_id": 2,
"bbox": [
1364,
255,
176,
477
],
"category_id": 2,
"iscrowd": 0,
"id": 8,
"area": 83952
},
{
"image_id": 3,
"bbox": [
766,
386,
200,
434
],
"category_id": 2,
"iscrowd": 0,
"id": 9,
"area": 86800
},
{
"image_id": 3,
"bbox": [
932,
390,
198,
445
],
"category_id": 2,
"iscrowd": 0,
"id": 10,
"area": 88110
},
{
"image_id": 3,
"bbox": [
1109,
343,
189,
479
],
"category_id": 5,
"iscrowd": 0,
"id": 11,
"area": 90531
},
{
"image_id": 3,
"bbox": [
1306,
347,
192,
495
],
"category_id": 6,
"iscrowd": 0,
"id": 12,
"area": 95040
},
{
"image_id": 4,
"bbox": [
820,
299,
206,
432
],
"category_id": 2,
"iscrowd": 0,
"id": 13,
"area": 88992
},
{
"image_id": 4,
"bbox": [
986,
281,
186,
446
],
"category_id": 2,
"iscrowd": 0,
"id": 14,
"area": 82956
},
{
"image_id": 4,
"bbox": [
1164,
293,
189,
485
],
"category_id": 5,
"iscrowd": 0,
"id": 15,
"area": 91665
},
{
"image_id": 4,
"bbox": [
1358,
269,
186,
484
],
"category_id": 6,
"iscrowd": 0,
"id": 16,
"area": 90024
}
]
}
转换代码
import os
import json
import xml.dom.minidom as minidom
CLASSES=['9','8' ,'7']
def convert_json_to_xml(json_file, output_folder):
with open(json_file, "r") as f:
json_data = json.load(f)
# Create XML document
doc = minidom.Document()
# Create root element
root = doc.createElement("annotation")
doc.appendChild(root)
# Add object information for each detection
bboxes = json_data["bboxes"]
labels = json_data["labels"]
scores = json_data["scores"]
for bbox, label, score in zip(bboxes, labels, scores):
object_element = doc.createElement("object")
root.appendChild(object_element)
name = doc.createElement("name")
name.appendChild(doc.createTextNode(CLASSES[int(label)]))
object_element.appendChild(name)
score_element = doc.createElement("score")
score_element.appendChild(doc.createTextNode(str(score)))
object_element.appendChild(score_element)
bndbox = doc.createElement("bndbox")
object_element.appendChild(bndbox)
xmin = doc.createElement("xmin")
xmin.appendChild(doc.createTextNode(str(int(bbox[0]))))
bndbox.appendChild(xmin)
ymin = doc.createElement("ymin")
ymin.appendChild(doc.createTextNode(str(int(bbox[1]))))
bndbox.appendChild(ymin)
xmax = doc.createElement("xmax")
xmax.appendChild(doc.createTextNode(str(int(bbox[2]))))
bndbox.appendChild(xmax)
ymax = doc.createElement("ymax")
ymax.appendChild(doc.createTextNode(str(int(bbox[3]))))
bndbox.appendChild(ymax)
# Save XML file with formatting
json_filename = os.path.basename(json_file)
xml_filename = os.path.splitext(json_filename)[0] + ".xml"
xml_path = os.path.join(output_folder, xml_filename)
with open(xml_path, "w", encoding="utf-8") as f:
doc.writexml(f, addindent="\t", newl="\n")
print(f"Converted {json_file} to {xml_path}")
def batch_convert_json_to_xml(input_folder, output_folder):
os.makedirs(output_folder, exist_ok=True)
for file_name in os.listdir(input_folder):
if file_name.endswith(".json"):
json_file = os.path.join(input_folder, file_name)
convert_json_to_xml(json_file, output_folder)
# 批量转换JSON文件为VOC XML格式
input_folder = r"C:\Users\GW\Desktop\mmd\outputs\preds"
output_folder = r"C:\Users\GW\Desktop\mmd\outputs\xmls"
batch_convert_json_to_xml(input_folder, output_folder)
代码2
目标检测生成的json文件,格式如下:
{"labels": [
["9", [852, 100, 1052, 525], 0.8942],
["9", [1009, 74, 1195, 502], 0.932],
["9", [1172, 59, 1347, 509], 0.8437],
["9", [1331, 50, 1497, 475], 0.9314]]}
分别对应classes,bbox,score
转换代码
import os
import json
import xml.dom.minidom as minidom
def convert_json_to_xml(json_file, output_folder):
with open(json_file, "r") as f:
json_data = json.load(f)
# Create XML document
doc = minidom.Document()
# Create root element
root = doc.createElement("annotation")
doc.appendChild(root)
# Add filename
filename = os.path.splitext(os.path.basename(json_file))[0] + ".png"
filename_element = doc.createElement("filename")
filename_element.appendChild(doc.createTextNode(filename))
root.appendChild(filename_element)
# Add object information for each detection
for output in json_data["labels"]:
label, bbox, score = output
object_element = doc.createElement("object")
root.appendChild(object_element)
name = doc.createElement("name")
# label is already a correct class name, no need for int conversion
name.appendChild(doc.createTextNode(label))
object_element.appendChild(name)
score_element = doc.createElement("score")
score_element.appendChild(doc.createTextNode(str(score)))
object_element.appendChild(score_element)
bndbox = doc.createElement("bndbox")
object_element.appendChild(bndbox)
xmin = doc.createElement("xmin")
xmin.appendChild(doc.createTextNode(str(int(bbox[0]))))
bndbox.appendChild(xmin)
ymin = doc.createElement("ymin")
ymin.appendChild(doc.createTextNode(str(int(bbox[1]))))
bndbox.appendChild(ymin)
xmax = doc.createElement("xmax")
xmax.appendChild(doc.createTextNode(str(int(bbox[2]))))
bndbox.appendChild(xmax)
ymax = doc.createElement("ymax")
ymax.appendChild(doc.createTextNode(str(int(bbox[3]))))
bndbox.appendChild(ymax)
# Save XML file with formatting
json_filename = os.path.basename(json_file)
xml_filename = os.path.splitext(json_filename)[0] + ".xml"
xml_path = os.path.join(output_folder, xml_filename)
with open(xml_path, "w", encoding="utf-8") as f:
doc.writexml(f, addindent="\t", newl="\n")
print(f"Converted {json_file} to {xml_path}")
def batch_convert_json_to_xml(input_folder, output_folder):
os.makedirs(output_folder, exist_ok=True)
for file_name in os.listdir(input_folder):
if file_name.endswith(".json"):
json_file = os.path.join(input_folder, file_name)
convert_json_to_xml(json_file, output_folder)
# 批量转换JSON文件为VOC XML格式
input_folder = r"C:\Users\GW\Desktop\20230725\2-2"
output_folder = r"C:\Users\GW\Desktop\20230725\2-2"
batch_convert_json_to_xml(input_folder, output_folder)