python批量处理数据脚本——目标检测数据标签的labelme格式转VOC格式

python 复制代码
import os
import json
from xml.etree.ElementTree import Element, SubElement, tostring, ElementTree

def labelme_to_voc(json_path, output_dir):
    with open(json_path, 'r') as f:
        labelme_data = json.load(f)

    img_filename = labelme_data['imagePath']
    img_width = labelme_data['imageWidth']
    img_height = labelme_data['imageHeight']
    shapes = labelme_data['shapes']

    # Create VOC XML structure
    root = Element('annotation')

    folder = SubElement(root, 'folder')
    folder.text = 'VOC'  # Customize folder name as needed

    filename = SubElement(root, 'filename')
    filename.text = os.path.basename(img_filename)

    size = SubElement(root, 'size')
    width = SubElement(size, 'width')
    width.text = str(img_width)
    height = SubElement(size, 'height')
    height.text = str(img_height)
    depth = SubElement(size, 'depth')
    depth.text = '3'  # Assuming RGB images

    for shape in shapes:
        label = shape['label']
        points = shape['points']

        object_elem = SubElement(root, 'object')
        name = SubElement(object_elem, 'name')
        name.text = label

        pose = SubElement(object_elem, 'pose')
        pose.text = 'Unspecified'

        truncated = SubElement(object_elem, 'truncated')
        truncated.text = '0'

        difficult = SubElement(object_elem, 'difficult')
        difficult.text = '0'

        bndbox = SubElement(object_elem, 'bndbox')
        xmin = SubElement(bndbox, 'xmin')
        xmin.text = str(min(points[0][0], points[1][0]))
        ymin = SubElement(bndbox, 'ymin')
        ymin.text = str(min(points[0][1], points[1][1]))
        xmax = SubElement(bndbox, 'xmax')
        xmax.text = str(max(points[0][0], points[1][0]))
        ymax = SubElement(bndbox, 'ymax')
        ymax.text = str(max(points[0][1], points[1][1]))

    # Save the VOC XML file
    xml_path = os.path.join(output_dir, os.path.splitext(os.path.basename(img_filename))[0] + '.xml')
    tree = ElementTree(root)
    tree.write(xml_path)

# Example usage
labelme_json_path = 'path/to/labelme.json'
output_directory = 'path/to/output'
labelme_to_voc(labelme_json_path, output_directory)

这只是一张图片的标签转换,要是一个数据集,则进行listdir遍历目录下的每个json标签即可。

相关推荐
python_chai10 分钟前
Python多进程并发编程:深入理解Lock与Semaphore的实战应用与避坑指南
开发语言·python·高并发·多进程··信号量
Cheng_082913 分钟前
llamafactory的包安装
python·深度学习
咸其自取24 分钟前
Flask(1): 在windows系统上部署项目1
python·flask
CopyLower28 分钟前
**Microsoft Certified Professional(MCP)** 认证考试
python·microsoft·flask
赵谨言34 分钟前
基于Python的推荐算法的电影推荐系统的设计
经验分享·python·毕业设计
我感觉。1 小时前
【李宏毅深度学习——分类模型的PyTorch架构】Homework 2:Phoneme Classification
pytorch·深度学习·李宏毅·分类模型
XU磊2601 小时前
使用 PyTorch 构建 UNet 图像去噪模型:从数据加载到模型训练的完整流程
人工智能·pytorch·python
renne1 小时前
Open WebUI+MCP搭建个人AI智能体
python·openai
JavaEdge在掘金1 小时前
LangChain4j + MCP:让你的 AI 轻松调用外部工具(内附GitHub-MCP实战)
python
SsummerC1 小时前
【leetcode100】一和零
开发语言·python·leetcode·动态规划