人体关键点数据集json标注格式转xml格式

最近在做yolo检测,解决了一个困扰了我很久的难题,因为我的数据集大部分是使用labelme人体关键点json标注格式,就是如何将人体关键点数据集json标注格式转换成xml格式。

Json格式:

只需将json的 "shapes" 字段解析出来即可,具体代码实现如下

import argparse
import json
import os
import cv2
import glob
import xml.etree.ElementTree as ET
from xml.dom import minidom

def bbox2voc(imgPath, xmlDir, bboxes):
    annotation = ET.Element("annotation")
    imSrc = cv2.imread(imgPath)
    imgDir, imgName = os.path.split(imgPath)

    try:
        h, w, c = imSrc.shape
    except:
        # print(image_path, "is not exit!")
        return

    ET.SubElement(annotation, "filename", ).text = imgName

    image_size = ET.SubElement(annotation, 'size')
    ET.SubElement(image_size, 'width').text = str(w)
    ET.SubElement(image_size, 'height').text = str(h)
    ET.SubElement(image_size, 'depth').text = str(3)
    for box in bboxes:
        object = ET.SubElement(annotation, "object")

        ET.SubElement(object, 'name').text = str(box[4])
        ET.SubElement(object, 'difficult').text = str(0)

        bndbox = ET.SubElement(object, "bndbox")
        ET.SubElement(bndbox, 'xmin').text = str(box[0])
        ET.SubElement(bndbox, 'ymin').text = str(box[1])
        ET.SubElement(bndbox, 'xmax').text = str(box[2])
        ET.SubElement(bndbox, 'ymax').text = str(box[3])

    xmlstr = minidom.parseString(ET.tostring(annotation)).toprettyxml(indent="   ")

    xmlPath = imgPath.replace('.jpg', '.xml').replace(imgDir, xmlDir)
    with open(xmlPath, "w", encoding='utf-8') as f:
        f.write(xmlstr)

def pose2bboxes(imgJsonPath, xmlDir):
    print('dir {}'.format(imgJsonPath))
    labelMeJsons = glob.glob(os.path.join(imgJsonPath, '*.json'))

    for labelMeJson in labelMeJsons:
        imgPath = labelMeJson.replace('.json', '.jpg')
        bboxes = []
        with open(labelMeJson, 'r') as labelMeJsonF:
            jsonDataLabelMe = json.load(labelMeJsonF)

            for labelMeshape in jsonDataLabelMe['shapes']:
                if 'rectangle' == labelMeshape['shape_type']:
                    x0, y0, x1, y1 = labelMeshape['points'][0][0], labelMeshape['points'][0][1], labelMeshape['points'][1][0], labelMeshape['points'][1][1],
                    xmin = max(min(x0, x1), 0)
                    ymin = max(min(y0, y1), 0)
                    xmax = max(x0, x1)
                    ymax = max(y0, y1)

                    bbox = [xmin, ymin, xmax, ymax]

                    bboxes.append([int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3]), labelMeshape['label']])
        bbox2voc(imgPath, xmlDir, bboxes)

if __name__ == '__main__':
    imgJsonPath = r'G:\XrwProject\selfjson01'
    xmlDir = (imgJsonPath + 'Xml').replace('/04', '/06')
    os.makedirs(xmlDir, exist_ok=True)

    pose2bboxes(imgJsonPath, xmlDir)


imgJsonPath:为json存放的路径

运行代码,会在json路径同级创建一个文件夹,将xml存放进去

生成的xml格式:

打开labelimg、验证是否转换成功

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Mamba24

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值