当前位置: 首页 > news >正文

深度学习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 jsondef 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').textobj_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}}]
}

http://www.lryc.cn/news/212262.html

相关文章:

  • 【c++|opencv】一、基础操作---1.图像读取
  • 【2023.10.30练习】C语言-循环右移字符
  • C++ 自引用指针this(整理)
  • pyzed.sl 突然 ImportError: DLL load failed while importing sl: 找不到指定的程序。 的解决
  • STM32 Cube配置RS485 Modbus
  • global,nonlocal
  • HTML5教程
  • vulnhub momentum 靶机复盘
  • c++和java实现策略模式的差异
  • vim
  • AcWing93. 递归实现组合型枚举:输出从1~n中随机选出的m个整数
  • Java修仙传之Flink篇
  • 网络新闻发稿为何经久不衰?
  • Java SimpleDateFormat 中英文时间格式化转换
  • 机器学习-基本知识
  • Xilinx 7 系列 1.8V LVDS 和 2.5V LVDS 信号之间的 LVDS 兼容性
  • R语言在生态环境领域中的实践技术应用
  • ChineseChess.2023.10.31.01
  • 数据库扩展语句和约束方式以及用户管理
  • JMM 简单理解
  • 微软Azure文本转音频,保存成MP3文件【代码python3】
  • 基于单片机的超声波探伤仪设计
  • idea的设置
  • 高等数学啃书汇总重难点(八)向量代数与空间解析几何
  • C#开发DLL,CAPL调用(CAPL>> .NET DLL)
  • 0-1背包问题【穷举法+二维dp数组】
  • nodejs+vue+python+php基于微信小程序的在线学习平台设计与实现-计算机毕业设计
  • Spring学习笔记2 Spring的入门程序
  • 【Linux】虚拟机安装Linux、客户端工具及Linux常用命令(详细教程)
  • Day 47 动态规划 part13