import os
import json
import xml.etree.ElementTree as ET
def convert_voc_to_ls(voc_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for xml_file in os.listdir(voc_folder):
if xml_file.endswith(".xml"):
tree = ET.parse(os.path.join(voc_folder, xml_file))
root = tree.getroot()
image_file = root.find('filename').text
width = int(root.find('size/width').text)
height = int(root.find('size/height').text)
ls_annotations = []
for obj in root.findall('object'):
label = obj.find('name').text
bndbox = obj.find('bndbox')
xmin = int(bndbox.find('xmin').text) / width * 100
ymin = int(bndbox.find('ymin').text) / height * 100
xmax = int(bndbox.find('xmax').text) / width * 100
ymax = int(bndbox.find('ymax').text) / height * 100
annotation = {
"id": f"{label}-{xmin}-{ymin}-{xmax}-{ymax}",
"type": "rectanglelabels",
"value": {
"x": xmin,
"y": ymin,
"width": xmax - xmin,
"height": ymax - ymin,
"rotation": 0,
"rectanglelabels": [label]
},
"origin": "manual",
"to_name": "image",
"from_name": "label",
"image_rotation": 0,
"original_width": width,
"original_height": height
}
ls_annotations.append(annotation)
ls_data = {
"data": {
"image": image_file
},
"annotations": [
{
"result": ls_annotations
}
]
}
output_file = os.path.join(output_folder, f"{os.path.splitext(xml_file)[0]}.json")
with open(output_file, 'w') as f:
json.dump([ls_data], f, indent=2)
# 使用示例
convert_voc_to_ls("/path/to/voc/xml/files", "/path/to/output/json/files")