python之json文件转xml文件案例讲解
作者:G果 发布时间:2021-11-18 04:54:23
标签:python,json
json文件格式
这是yolov4模型跑出来的检测结果result.json
下面是截取的一张图的检测结果
{
"frame_id":1, #图片的序号
"filename":"/media/wuzhou/Gap/rgb-piglet/test/00000000.jpg", #图片的路径
"objects": [ #该图中所有的目标:目标类别、目标名称、归一化的框的坐标(xywh格式)、置信度
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.750913, "center_y":0.402691, "width":0.038380, "height":0.193304}, "confidence":0.995435},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.764775, "center_y":0.199255, "width":0.049979, "height":0.130169}, "confidence":0.994495},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.560050, "center_y":0.482614, "width":0.036331, "height":0.166377}, "confidence":0.994460},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.710756, "center_y":0.406446, "width":0.041782, "height":0.191297}, "confidence":0.993540},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.638335, "center_y":0.238725, "width":0.107689, "height":0.092282}, "confidence":0.992926},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.780232, "center_y":0.448454, "width":0.041550, "height":0.179540}, "confidence":0.990020},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.563412, "center_y":0.350035, "width":0.103184, "height":0.059460}, "confidence":0.979756},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.522591, "center_y":0.195170, "width":0.083014, "height":0.071478}, "confidence":0.970642},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.658721, "center_y":0.154640, "width":0.103852, "height":0.055686}, "confidence":0.967082},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.537660, "center_y":0.256810, "width":0.101619, "height":0.095211}, "confidence":0.918135},
{"class_id":0, "name":"pp", "relative_coordinates":{"center_x":0.528618, "center_y":0.481005, "width":0.033226, "height":0.177723}, "confidence":0.310291}
]
},
完整代码
代码需要指定图片的路径,例如 file_dir = "H:/rgb-piglet/five/test"
注意:result.json文件要跟图片放一起
代码生成的xml与图片在同一个路径下
import json
import time
import os
from PIL import Image
import cv2
import numpy as np
'''人为构造xml文件的格式'''
out0 ='''<annotation>
<folder>%(folder)s</folder>
<filename>%(name)s</filename>
<path>%(path)s</path>
<source>
<database>None</database>
</source>
<size>
<width>%(width)d</width>
<height>%(height)d</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
'''
out1 = ''' <object>
<name>%(class)s</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>%(xmin)d</xmin>
<ymin>%(ymin)d</ymin>
<xmax>%(xmax)d</xmax>
<ymax>%(ymax)d</ymax>
</bndbox>
</object>
'''
out2 = '''</annotation>
'''
def read_json(json_dir):
with open(json_dir,"r") as f:
data = json.load(f)
print(type(data),len(data),type(data[0]),data[0]['frame_id'])
return data
'''txt转xml函数'''
def translate(fdir,lists):
source = {}
label = {}
data = read_json(fdir+"/result.json")
k = 0
for jpg in lists:
print(jpg)
if jpg[-4:] == '.jpg':
image= cv2.imread(jpg)#路径不能有中文
h,w,_ = image.shape #图片大小
fxml = jpg.replace('.jpg','.xml')
fxml = open(fxml, 'w');
imgfile = jpg.split('/')[-1]
source['name'] = imgfile
source['path'] = jpg
source['folder'] = os.path.basename(fdir)
source['width'] = w
source['height'] = h
fxml.write(out0 % source)
for obj in data[k]["objects"]:
label['class'] = obj["class_id"]
box = obj["relative_coordinates"]
'''把txt上的数字(归一化)转成xml上框的坐标'''
xmin = float(box["center_x"] - 0.5*box["width"])*w
ymin = float(box["center_y"] - 0.5*box["height"])*h
xmax = float(xmin + box["width"]*w)
ymax = float(ymin + box["height"]*h)
label['xmin'] = xmin
label['ymin'] = ymin
label['xmax'] = xmax
label['ymax'] = ymax
fxml.write(out1 % label)
k = k+1
fxml.write(out2)
if __name__ == '__main__':
file_dir = "H:/rgb-piglet/five/test"
lists=[]
for i in os.listdir(file_dir):
if i[-3:]=='jpg':
lists.append(file_dir+'/'+i)
#print(lists)
translate(file_dir,lists)
print('---------------Done!!!--------------')
来源:https://blog.csdn.net/weixin_42899627/article/details/118373040
0
投稿
猜你喜欢
- 1、需求我们的代码已经变得无法阅读,到处都是硬编码的切片索引,我们想优化他们。2、解决方案代码中如果有很多硬编码的索引值,将导致可读性和维护
- 这篇文章主要介绍了python自动化unittest yaml使用过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
- 本文介绍了6个asp常用的判断函数的语法和使用方法,有IsArray 函数,IsDate 函数 ,IsEmpty 函数,IsNull 函数,
- 一.权限表mysql数据库中的3个权限表:user 、db、 host权限表的存取过程是:1)先从user表中的host、 user、 pa
- 在做数据分析或者统计的时候,经常需要进行数据正态性的检验,因为很多假设都是基于正态分布的基础之上的,例如:T检验。在Python中,主要有以
- 这周心血来潮,翻看了现在比较流行的几个JS脚本框架的底层代码,虽然是走马观花,但也受益良多,感叹先人们的伟大……感叹是为了缓解严肃的气氛并引
- 最近在无忧脚本混了一阵子,回复了一些贴子,自己却没有做出什么东东让大家看看,心里有些不安,于是写了下边的一点东西,本来应该发在类封装区的,考
- 之前一直使用QtCreator,在设计界面时非常方便,python早就集成了Qt模块,在python中以pyQt的包存在,目前常用的是pyQ
- opts, args = getopt.getopt(sys.argv[1:], "t:s:h", ["wal
- 字符画,一种由字母、标点、汉字或其他字符组成的图画。简单的字符画是利用字符的形状代替图画的线条来构成简单的人物、事物等形象,它一般由人工制作
- 使用预训练模型的代码如下:# 加载预训练模型 resNet50 = models.resnet50(pretrained=True) Res
- 最好不要在 base 环境中安装或者配置一些包之类的,它是 Python 的基础环境,为了更好的管理 Python 环境
- 最近游戏项目在多个国家上线,每个国家都对应两份儿svn目录(一份是本地策划目录,一份是线上目录)。于是乎维护变得很烦躁。需要先更新本地策划s
- Python使用pandas导入xlsx格式的excel文件内容1. 基本导入在 Python中使用pandas导入.xlsx文件的方法是r
- 1.Python虚拟环境创建首先我们为什么要创建虚拟环境呢?因为不同的项目所依赖的环境不一样,他们需要不同的第三方库等等。为了避免冲突,所以
- 有时候我们没办法得到pdf或者word文档,这个时候会使用手机或者相机进行拍照,往往会出现背景,打印出来就是灰色的或者有黑色的背景,这个时候
- 大家好,为了进行调试和错误跟踪,人们在整个代码库中广泛使用日志,今天来看看如何在代码中定义日志,并探讨日志的权限。一、日志层级在开始之前,需
- 在python中,通过如下两个模块可以实现邮件的自动化操作smtplibemailsmtplib模块是对SMTP协议的封装,用于发送邮件;e
- VBScript似乎已经成为ASP服务器端开发的首先语言,VBScript函数库丰富、而且使用起来也很容易上手,即使平时不太编程的朋友,只要
- python 的虚拟环境可以为一个 python 项目提供独立的解释环境、依赖包等资源,既能够很好的隔离不同项目使用不同 python 版本