pascalvoc xml 转 label studio json

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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值