深度学习02-数据集格式转换

背景:

通常搜集完数据图片后,我们会用labelimg进行图片标注,比较高版本的labelimg支持的标注格式有三种,PascalVOC、YOLO、CreateML,标注的时候可以根据自己的算法模型数据集需求选择相应的格式,当然,也可以三种方式同时标注,不过会耗时间一些。有时候我们仅仅标注了一种格式转,而实际算法建模的时候可能需要对相应的格式进行转换。

xml转json:

默认选用PascalVOC方式的话,标注的数据集格式为XML,实例如下(2.xml):

复制代码
<annotation>
	<folder>Desktop</folder>
	<filename>ng2.png</filename>
	<path>C:\Users\Xiao\Desktop\ng2.png</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>1892</width>
		<height>851</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>1</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>60</xmin>
			<ymin>381</ymin>
			<xmax>354</xmax>
			<ymax>583</ymax>
		</bndbox>
	</object>
</annotation>

将该文档转换为json格式并保存的代码如下:

复制代码
import xml.etree.ElementTree as ET
import json

def xml_to_json(xml_file, json_file):
    tree = ET.parse(xml_file)
    root = tree.getroot()

    data = []
    for obj in root.findall('object'):
        obj_data = {}
        obj_data['name'] = obj.find('name').text
        obj_data['bbox'] = {
            'xmin': int(obj.find('bndbox/xmin').text),
            'ymin': int(obj.find('bndbox/ymin').text),
            'xmax': int(obj.find('bndbox/xmax').text),
            'ymax': int(obj.find('bndbox/ymax').text)
        }
        data.append(obj_data)

    json_data = {
        'filename': root.find('filename').text,
        'size': {
            'width': int(root.find('size/width').text),
            'height': int(root.find('size/height').text),
            'depth': int(root.find('size/depth').text)
        },
        'objects': data
    }

    with open(json_file, 'w') as f:
        json.dump(json_data, f, indent=4)

# Example usage
xml_file = r'C:\Users\Xiao\Desktop\tools\2.xml'
json_file = r'C:\Users\Xiao\Desktop\tools\2.json'
xml_to_json(xml_file, json_file)
print('数据转换完成!')

实际使用的时候需要适当修改一下文档路径才可以。

转换完之后的json内容如下(2.json):

复制代码
{
    "filename": "ng2.png",
    "size": {
        "width": 1892,
        "height": 851,
        "depth": 3
    },
    "objects": [
        {
            "name": "1",
            "bbox": {
                "xmin": 60,
                "ymin": 381,
                "xmax": 354,
                "ymax": 583
            }
        }
    ]
}
相关推荐
云杂项7 小时前
Exploring Model Inversion Attacks in the Black-box Setting(个人笔记)
人工智能
还卿一钵无情泪8 小时前
Unsloth 微调 构建自己的大模型 没有GPU也能微调
linux·开发语言·人工智能·python·大模型·nlp·unsloth
冬奇Lab8 小时前
LLM 驱动的自动化测试系列(06):移动端自动化(二)——DroidRun/Mobilerun 的角色级模型拆分
人工智能·测试
冬奇Lab9 小时前
一天一个开源项目(第230篇):HandRaw-Style —— 把 327 种手绘风格、165 种排版、36 种配色编号化,让 AI 画图不再每次都飘
人工智能·开源·资讯
罗西的思考9 小时前
从陶哲轩的访谈看:AI 数学研究方法论 & 给其它行业的启示
人工智能
学...9 小时前
软件学报 2023 论文《面向复杂约束优化问题的进化算法综述》阅读笔记
人工智能·ga·cmop
xhy_07079 小时前
AI 在同一步上反复打转怎么办?WES Code 循环检测怎么用
人工智能·大模型·ai编程·wes code
鱼宵10 小时前
Spring AI 提示词模板:{变量} 参数化 + few-shot,一条提示词反复用
java·人工智能·spring·few-shot·提示词工程·springai
蜗牛互联网10 小时前
Python消费Responses SSE事件:增量文本、超时与取消
java·开发语言·人工智能·后端·python
架构师那点事儿10 小时前
大模型如何私有化部署到生产环境
人工智能·架构·llm