⽤python实现将json⽂件转化为xml格式
1. ⾸先此json格式的标注来⾃于
2. 其次json转xml的脚本不能万能的,因为每个json⽂件的内部会略有不同
3. 此例的json内部格式如下
转化后的xml格式如下(就拿第⼀个标注举例)
4.脚本⽂件如下
from xml.dom.minidom import parseString import xml.dom.minidom
import os
import cv2
import json
_IMAGE_PATH ='E:/coco/COCO/train'
_INDENT =''*4
_NEW_LINE ='\n'
_FOLDER_NODE ='yaoganDataSet'
_ROOT_NODE ='annotation'
_DATABASE_NAME =''
_ANNOTATION ='COCO2014'
_AUTHOR ='li'
逻辑运算符短路
_AUTHOR ='li'
_SEGMENTED ='0'
_DIFFICULT ='0'
_TRUNCATED ='0'
_POSE ='Unspecified'
_ANNOTATION_SAVE_PATH ='Q:/tianchi_competition/Street_Recognization/Annotations' # 封装创建节点的过程
def createElementNode(doc, tag, attr):# 创建⼀个元素节点
element_node = ateElement(tag)
# 创建⼀个⽂本节点
text_node = ateTextNode(attr)
# 将⽂本节点作为元素节点的⼦节点
element_node.appendChild(text_node)
return element_node
def createChildNode(doc, tag, attr, parent_node):
child_node = createElementNode(doc, tag, attr)
parent_node.appendChild(child_node)
if __name__ =="__main__":
json_path ="Q:/tianchi_competition/Street_Recognization/1/"
img_path ="Q:/tianchi_competition/Street_Recognization/JPEGImages/"
json_path1=os.path.join(json_path+'mchar_train.json')
with open(json_path1,"r")as f:
ann = json.load(f)
for per_img_info in ann.items():
per_img_name=per_img_info[0]
saveName = per_img_name.strip(".png")
saveName2=saveName+'.jpg'
xml_file_name = os.path.join(_ANNOTATION_SAVE_PATH,(saveName +'.xml'))            img = cv2.imread(os.path.join(img_path, saveName2))
print(os.path.join(img_path, saveName2))
# cv2.imshow(img)
height, width, channel = img.shape
print(height, width, channel)
"""创建xml框架"""
my_dom = DOMImplementation()
doc = ateDocument(None, _ROOT_NODE,None)
# 获得根节点
root_node = doc.documentElement
# folder节点
createChildNode(doc,'folder', _FOLDER_NODE, root_node)
# filename节点
createChildNode(doc,'filename', saveName +'.png', root_node)
最大源码网print("正在创建各个结点中")
# source节点
后盾网php视频教程下载教程source_node = ateElement('source')
# source的⼦节点
createChildNode(doc,'database', _DATABASE_NAME, source_node)
createChildNode(doc,'annotation', _ANNOTATION, source_node)
createChildNode(doc,'image','flickr', source_node)
root_node.appendChild(source_node)
size_node = ateElement('size')
createChildNode(doc,'width',str(width), size_node)
createChildNode(doc,'height',str(height), size_node)
createChildNode(doc,'depth',str(channel), size_node)
root_node.appendChild(size_node)
# 创建segmented节点
createChildNode(doc,'segmented', _SEGMENTED, root_node)
per_img_loc=per_img_info[1]
num_label=per_img_loc['label']
l=len(num_label)
for i in range(l):
xmin= per_img_loc['left']
ymin= per_img_loc ['top']
xmax=[]
ymax=[]
for m,n in zip(xmin,per_img_loc ['width']):
per_xmax=m+n
xmax.append(per_xmax)
for p,q in zip(ymin,per_img_loc ['height']):
per_ymax=p+q
ymax.append(per_ymax )
label=per_img_loc['label']
# object节点
object_node = ateElement('object')
# object的⼦节点
createChildNode(doc,'name',str(label[i]), object_node)
createChildNode(doc,'pose','undefine', object_node)
createChildNode(doc,'truncated','undefine', object_node)
createChildNode(doc,'difficute',str(0), object_node)
#创建object⼦节点bndbox的⼦节点
bndbox_node = ateElement('bndbox')
createChildNode(doc,'xmin',str(xmin[i]), bndbox_node)
createChildNode(doc,'ymin',str(ymin[i]), bndbox_node)
createChildNode(doc,'xmax',str(xmax[i]), bndbox_node)
createChildNode(doc,'ymax',str(ymax[i]), bndbox_node)
object_node.appendChild(bndbox_node)
root_node.appendChild(object_node)
i = i +1
with open('l','w', encoding='utf-8')as f:
doc .writexml(f, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
f.close()
fin =open('l')
fout =open(xml_file_name,'w')
lines = adlines()
for line in lines[1:]:
python解析json文件if line.split():
fakepathfout.writelines(line)
fin.close()
fout.close()
布局管理器可以管理
踩过的坑:
cv2.imred() 只能读取.jpg格式的图⽚,其次括号中的路径不能有中⽂

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。