深度学习标注文件格式转换

json转xml

原始数据集文件夹中图片格式为bmp,标注文件为json,图片和标注文件放在同一个文件夹下面,将json转为xml格式,图片和标注文件分别存放在一个文件夹下面。

python 复制代码
headstr = """\
<annotation>
    <folder>VOC</folder>
    <filename>%s</filename>
    <source>
        <database>My Database</database>
        <annotation>COCO</annotation>
        <image>flickr</image>
        <flickrid>NULL</flickrid>
    </source>
    <owner>
        <flickrid>NULL</flickrid>
        <name>company</name>
    </owner>
    <size>
        <width>%d</width>
        <height>%d</height>
        <depth>%d</depth>
    </size>
    <segmented>0</segmented>
"""
objstr = """\
    <object>
        <name>%s</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>%d</xmin>
            <ymin>%d</ymin>
            <xmax>%d</xmax>
            <ymax>%d</ymax>
        </bndbox>
    </object>
"""

tailstr = '''\
</annotation>
'''

def write_xml(xml_path,head,labels,bboxs,tail):
    f = open(xml_path,'w')
    f.write(head)
    for i in range(len(labels)):
        bbox = bboxs[i]
        f.write(objstr % (labels[i],bbox[0],bbox[1],bbox[2],bbox[3]))
    f.write(tail)

def json_to_xml(json_file, xml_file):
    with open(json_file, 'r') as f:
        data = json.load(f)
        
    labels = []
    bboxs = []
    for shape in data['shapes']:
        label = shape['label']
        labels.append(label)
        xmin = int(shape['points'][0][0])
        ymin = int(shape['points'][0][1])
        xmax = int(shape['points'][1][0])
        ymax = int(shape['points'][1][1])
        bboxs.append([xmin, ymin, xmax, ymax])

    image_name = data['imagePath']
    imageWidth = data['imageWidth']
    imageHeight = data['imageHeight']
    head = headstr % (image_name,imageWidth, imageHeight, 3)
    tail = tailstr
    write_xml(xml_file, head, labels, bboxs, tail)

import os, shutil
from tqdm import tqdm
ori_path = "E:/projects/20240702181159-1Fs/"
img_path = "E:/projects/datasets/img/"
xml_path = "E:/projects/datasets/ann/"
for file in tqdm(os.listdir(ori_path)):
    if '.json' in file:
        json_to_xml(os.path.join(ori_path, file), os.path.join(xml_path, file.replace('json', 'xml')))
    else:
        shutil.copy(os.path.join(ori_path, file), os.path.join(img_path, file))
相关推荐
ZISHU_987几秒前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉
hh95010 分钟前
gent Plan x DeepSeek Harness:多 Agent 协作场景下的中央编排实践
人工智能·wpf·adg·火山引擎·agent plan·adg成都社区
cczixun25 分钟前
2026 智能体平台落地实战:从 POC 验证到生产上线,避开选型与交付陷阱
人工智能·落地·选型·智能体·2026
IT_陈寒25 分钟前
Redis内存警告竟是因为这个不起眼的配置项
前端·人工智能·后端
CHY_12831 分钟前
VSO.ai 系列(二):AI验证回归优化实战
人工智能·synopsys·vso.ai·验证加速
2401_8900956143 分钟前
武汉人工智能应用软件开发实施异常怎么排查?模拟环境与验证步骤
人工智能·案例复盘·武汉自动意志科技·智钳claw·企业ai智能体·ai漫剧生成
泰迪智能科技011 小时前
三种人社职业技能等级证书报考指南对比:人工智能训练师 / 计算机程序设计员 / 服务机器人应用技术员
人工智能·百度·机器人
森山冶仁1 小时前
AI 原生数据治理技术栈:大模型 + 向量库 + 知识图谱怎么选
人工智能·知识图谱·数据治理
记忆张量MemTensor1 小时前
产品更新|MemOS 现已支持 DeepSeek Harness 长期记忆接入
人工智能·typescript·开源·agent
FII工业富联科技服务1 小时前
WRC 2026技术拆解:从人类示范到真机反馈,机器人“大脑”如何完成训练闭环?
人工智能·机器人·自动化·制造