.json内容
.xml内容
代码:
`import xmltodict
import json
from xml.dom.minidom import Document
print(”starting————————————————————“)
if __name__ == "__main__":
import os
def xx_yy_max(data):
x1 = float(data[0][0])
y1 = float(data[0][1])
x2 = float(data[1][0])
y2 = float(data[1][1])
x3 = float(data[2][0])
y3 = float(data[2][1])
x4 = float(data[3][0])
y4 = float(data[3][1])
xx1 = min(x1, x2, x3, x4)
xx2 = max(x1, x2, x3, x4)
yy1 = min(y1, y2, y3, y4)
yy2 = max(y1, y2, y3, y4)
return xx1,yy1,xx2,yy2
def makexml(xml_root,file_name,bbox_len,name,bbox):
doc = Document()
orderpack = doc.createElement("annotation")
doc.appendChild(orderpack)
objectfile = doc.createElement("filename")
objectcontenttext = doc.createTextNode(file_name)
objectfile.appendChild(objectcontenttext)
orderpack.appendChild(objectfile)
obj = "object"
for ii in range(bbox_len):
objectE = doc.createElement(obj)
objectcontent = doc.createElement("name")
objectcontenttext = doc.createTextNode(name[ii])
objectcontent.appendChild(objectcontenttext)
objectE.appendChild(objectcontent)
objectresult = doc.createElement("difficult")
objectresulttext = doc.createTextNode('0')
objectresult.appendChild(objectresulttext)
objectE.appendChild(objectresult)
bn_box="bndbox"
objectbbbox = doc.createElement(bn_box)
objectxmin = doc.createElement("xmin")
objectxmintext = doc.createTextNode(str(bbox[ii][0]))
objectxmin.appendChild(objectxmintext)
objectbbbox.appendChild(objectxmin)
objectymin = doc.createElement("ymin")
objectymintext = doc.createTextNode(str(bbox[ii][1]))
objectymin.appendChild(objectymintext)
objectbbbox.appendChild(objectymin)
objectxmax = doc.createElement("xmax")
objectxmaxtext = doc.createTextNode(str(bbox[ii][2]))
objectxmax.appendChild(objectxmaxtext)
objectbbbox.appendChild(objectxmax)
objectymax = doc.createElement("ymax")
objectymaxtext = doc.createTextNode(str(bbox[ii][3]))
objectymax.appendChild(objectymaxtext)
objectbbbox.appendChild(objectymax)
objectE.appendChild(objectbbbox)
orderpack.appendChild(objectE)
f = open(xml_root, 'w')
doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='gbk')
f.close()
name=[]
point=[]
with open("不导电,角位漏底20180928115506对照样本.json",'r',encoding='utf-8') as load_f:
load_data=json.load(load_f)
print(load_data)
filename=load_data['imagePath']
print("-------%s",filename)
bbox=load_data['shapes']
point_len=len(bbox)
for ii in range(len(bbox)):
name.append(bbox[ii]['label'])
point .append(xx_yy_max(bbox[ii]['points']))
load_f.close()
print(point_len)
print(name)
print(point)
makexml(os.path.join(filename)+".xml",filename,point_len,name,point)
`
补:.
json文件的创建与读取参考:
https://blog.csdn.net/qq_28311921/article/details/86569772
https://blog.csdn.net/weixin_41084236/article/details/83107237
版权声明:本文为qq_31355669原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。