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

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将VOC格式的XML文件换为JSON格式,您可以使用Python中的xmltodict库和json库,类似于将一般的XML换为JSON的过程。以下是一个示例代码: ```python import xmltodict import json import os # 定义VOC格式的XML文件夹路径 folder_path = "path/to/xml/folder" # 获取XML文件列表 xml_files = [file for file in os.listdir(folder_path) if file.endswith(".xml")] # 遍历XML文件列表并换为JSON for xml_file in xml_files: # 构建XML文件路径 xml_path = os.path.join(folder_path, xml_file) # 读取XML数据 with open(xml_path, "r") as file: xml_data = file.read() # 将XML数据换为字典 dict_data = xmltodict.parse(xml_data) # 将字典换为JSON字符串 json_data = json.dumps(dict_data, indent=4) # 构建JSON文件路径 json_file = xml_file.replace(".xml", ".json") json_path = os.path.join(folder_path, json_file) # 将JSON字符串写入文件 with open(json_path, "w") as file: file.write(json_data) ``` 在上述示例代码中,我们首先定义VOC格式的XML文件夹路径为`"path/to/xml/folder"`。然后,我们使用`os.listdir()`函数获取文件夹中的XML文件列表,并使用列表推导式筛选出以`.xml`结尾的文件。 接下来,我们使用一个循环遍历XML文件列表,并对每个XML文件进行换。在循环中,首先构建XML文件的路径,并使用`open()`函数读取XML数据。 然后,我们使用`xmltodict.parse()`函数将XML数据换为字典。接着,使用`json.dumps()`函数将字典数据换为格式化的JSON字符串。 然后,我们构建JSON文件路径,将`.xml`替换为`.json`,并使用`open()`函数将JSON字符串写入该文件路径。 请确保将示例代码中的`"path/to/xml/folder"`替换为实际的VOC格式的XML文件夹路径。 希望能对您有所帮助!如果您还有其他问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值