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标签即可。

相关推荐
阿豪只会阿巴9 小时前
【多喝热水系列】从零开始的ROS2之旅——Day4
c++·笔记·python·ros2
2401_841495649 小时前
【LeetCode刷题】寻找重复数
数据结构·python·算法·leetcode·链表·数组·重复数
laplace01239 小时前
LangChain 1.0 入门实战(Part 1)详细笔记
笔记·python·langchain·numpy·pandas
only-lucky9 小时前
Python版本OpenCV
开发语言·python·opencv
三万棵雪松10 小时前
【python-基础】
开发语言·python
先做个垃圾出来………10 小时前
2610.转换二维数组
开发语言·python
java1234_小锋10 小时前
[免费]基于Python的Django+Vue3在线商城系统(简易版)【论文+源码+SQL脚本】
python·django·商城系统·python毕业设计·在线商城
ray96310 小时前
Python——for循环和range()函数
python
中國龍在廣州10 小时前
谈谈2025年人工智能现状及发展趋势分析
人工智能·深度学习·算法·自然语言处理·chatgpt·机器人·机器人学习
vibag10 小时前
Model大模型接口
python·语言模型·langchain·大模型