当前位置:网站首页>将 .json 格式 转换成 .xml格式
将 .json 格式 转换成 .xml格式
2022-08-09 13:10:00 【花椒酱不吃花椒喵】
原文链接:https://blog.csdn.net/weixin_42419002/article/details/100161294
1. 转换前的 .json 格式文件的内容:
json 是一个字典,具体内容看下面这个链接:
https://blog.csdn.net/weixin_42419002/article/details/100159408
2. 转换后的 .xml 格式文件的内容:
<annotation>
<folder>VOC2012</folder> # 这些值训练的时候不会被用到
<filename>2007_000661.jpg</filename>
<source>
<database>The VOC2007 Database</database>
<annotation>PASCAL VOC2007</annotation>
<image>flickr</image>
</source>
<size>
<width>500</width> # 这个值需要改变
<height>375</height> # 这个值需要改变
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>sofa</name> # 这个值需要改变
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>119</xmin> # 这个值需要改变
<ymin>177</ymin> # 这个值需要改变
<xmax>331</xmax> # 这个值需要改变
<ymax>278</ymax> # 这个值需要改变
</bndbox>
</object>
<object>
<name>chair</name> # 这个值需要改变
<pose>Unspecified</pose>
<truncated>1</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>133</xmin> # 这个值需要改变
<ymin>159</ymin> # 这个值需要改变
<xmax>179</xmax> # 这个值需要改变
<ymax>197</ymax> # 这个值需要改变
</bndbox>
</object>
</annotation>
3. 将 .json 转换成 .xml 格式代码:
import os
import json
from lxml import etree as ET
from xml.dom import minidom
def edit_xml(objects, id, dir):
save_xml_path = os.path.join(dir, "%s.xml" % id) # xml
root = ET.Element("annotation")
# root.set("version", "1.0")
folder = ET.SubElement(root, "folder")
folder.text = "none"
filename = ET.SubElement(root, "filename")
filename.text = "none"
source = ET.SubElement(root, "source")
source.text = "201908"
owner = ET.SubElement(root, "owner")
owner.text = "YZN"
size = ET.SubElement(root, "size")
width = ET.SubElement(size, "width")
width.text = str(2048)
height = ET.SubElement(size, "height")
height.text = str(2048)
depth = ET.SubElement(size, "depth")
depth.text = "3"
segmented = ET.SubElement(root, "segmented")
segmented.text = "0"
for obj in objects: #
object = ET.SubElement(root, "object")
name = ET.SubElement(object, "name") # number
name.text = obj["category"]
# meaning = ET.SubElement(object, "meaning") # name
# meaning.text = inf_value[0]
pose = ET.SubElement(object, "pose")
pose.text = "Unspecified"
truncated = ET.SubElement(object, "truncated")
truncated.text = "0"
difficult = ET.SubElement(object, "difficult")
difficult.text = "0"
bndbox = ET.SubElement(object, "bndbox")
xmin = ET.SubElement(bndbox, "xmin")
xmin.text = str(int(obj["bbox"]["xmin"]))
ymin = ET.SubElement(bndbox, "ymin")
ymin.text = str(int(obj["bbox"]["ymin"]))
xmax = ET.SubElement(bndbox, "xmax")
xmax.text = str(int(obj["bbox"]["xmax"]))
ymax = ET.SubElement(bndbox, "ymax")
ymax.text = str(int(obj["bbox"]["ymax"]))
tree = ET.ElementTree(root)
tree.write(save_xml_path, encoding="UTF-8", xml_declaration=True)
root = ET.parse(save_xml_path)
file_lines = minidom.parseString(ET.tostring(root, encoding="Utf-8")).toprettyxml(
indent="\t")
file_line = open(save_xml_path, "w", encoding="utf-8")
file_line.write(file_lines)
file_line.close()
def getDirId(dir): # get the id list of id.png
names = os.listdir(dir)
ids = []
for name in names:
# path = os.path.join(dir, name)
# img = cv2.imread(path)
# w, h, c = img.shape
# if name.endswith(".jpg") or name.endswith(".png"):
# ids["%s" % name.split(".")[0]] = [w, h, c]
ids.append(name.split(".")[0])
return ids
filedir = "annotations.json"
annos = json.loads(open(filedir).read())
trainIds = getDirId("Tinghua100K/images/train/")
testIds = getDirId("Tinghua100K/images/test/")
ids = annos["imgs"].keys() # all img ids in .json
for id in ids:
# json 中的ID图片有待检测目标,且该id图片在 train文件夹中
if len(annos["imgs"][id]["objects"]) > 0 and (id in trainIds) and :
objects = annos["imgs"][id]["objects"]
edit_xml(objects, id, dir = "xmlLabel/train")
elif len(annos["imgs"][id]["objects"]) > 0 and (id in testIds):
objects = annos["imgs"][id]["objects"]
edit_xml(objects, id, dir = "xmlLabel/test")
边栏推荐
- 搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
- NC84 完全二叉树结点数
- FFmpeg长时间无响应的解决方法
- 43. The sword refers to Offer 1 ~ 1 the number of occurrences of n integers (recursive, mathematics)
- 什么是布隆过滤器?如何使用?
- JS轮播图实现
- 机器学习web服务化实战:一次吐血的服务化之路 (转载非原创)
- 蓝桥历届真题-跑步锻炼
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 14 Assignment
- RobotFramework 之 数据驱动
猜你喜欢

海康设备获取YV12图像-不用rtsp

FFmpeg multimedia file processing (implementation of ffmpeg operation directory and list)

Unity3d_API_GPS_LocationService

pytest 之 fixture参数化

01_iTween_第一天--小球抛物线

GIN a preliminary study, the environment is installed

群组行动控制--自动队列化实现策略

pytest 之 重运行机制与测试报告

Come and throw eggs.

Ledong Fire Rescue Brigade was invited to carry out fire safety training for cadres
随机推荐
javscript基础易错点集合
面试攻略系列(四)-- 你不知道的大厂面试
pytest 基础认知
FFmpeg av_interleaved_write_frame错误
GIN a preliminary study, the environment is installed
Q_04_06 把它放在一起:传送
RobotFramework简介
微服务+微信小程序实现社区服务
RobotFramework 之 用户关键字
FFMPEG多媒体文件处理(ffmpeg文件的删除与重命名)
KMP方法
ArcEngine(十)创建矢量图层
正则表达式-re模块
搭建大型分布式服务(四)Docker搭建开发环境安装Mysql
32位机器和64位机器基本数据类型长度
利用信号灯和共享内存实现进程间同步通信
面试攻略系列(三)-- 高级开发工程师面试问些啥?
FFmpeg multimedia file processing (ffmpeg prints audio and video Meta information)
JS轮播图实现
Q_06_03 表达式