-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson2xml.py
84 lines (83 loc) · 3.29 KB
/
json2xml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import json
from PIL import Image
from tqdm import tqdm
from xml.etree.ElementTree import ElementTree,Element
src = r'E:/jyzdata/JYZvideo/video/images/'
sampleSrc = r'E:/jyzdata/sample.xml'
fileList = os.listdir(src)
jsonFileList = [x for x in fileList if x.endswith('json') ]
#jsonFileList = jsonFileList[:3]
for jsonFile in tqdm(jsonFileList):
#获取sample文件
tree=ElementTree()
tree.parse(sampleSrc)
root=tree.getroot()
# print(jsonFile[:-5])
with open(src+jsonFile) as f:
temp = json.loads(f.read())
imagePath = temp['imagePath']
img = Image.open(src+imagePath)
w,h = img.size
boxNum = len(temp['shapes'])
size = Element('size')
width = Element('width')
height = Element('height')
depth = Element('depth')
width.text = str(w)
height.text = str(h)
depth.text = '3'
size.append(width)
size.append(height)
size.append(depth)
fileName = Element('filename')
fileName.text = imagePath
root.append(size)
root.append(fileName)
for i in range(boxNum):
element=Element('object') #指点里面是属性,结果展示:<train name="wang">
#创建二级目录
oneName=Element('name')
oneName.text = temp['shapes'][i]['label']#二级目录的值 #结果展示:<id>1</id>
onePose = Element('pose')
onePose.text = 'Unknown'
oneTruncated = Element('truncated')
oneTruncated.text = '1'
oneDifficult = Element('difficult')
oneDifficult.text = '0'
oneBndbox = Element('bndbox')
twoX0 = Element('x0')
twoY0 = Element('y0')
twoX1 = Element('x1')
twoY1 = Element('y1')
twoX2 = Element('x2')
twoY2 = Element('y2')
twoX3 = Element('x3')
twoY3 = Element('y3')
twoX0.text = str(temp['shapes'][i]['points'][0][0])
twoY0.text = str(temp['shapes'][i]['points'][0][1])
twoX1.text = str(temp['shapes'][i]['points'][1][0])
twoY1.text = str(temp['shapes'][i]['points'][1][1])
twoX2.text = str(temp['shapes'][i]['points'][2][0])
twoY2.text = str(temp['shapes'][i]['points'][2][1])
twoX3.text = str(temp['shapes'][i]['points'][3][0])
twoY3.text = str(temp['shapes'][i]['points'][3][1])
oneBndbox.append(twoX0)
oneBndbox.append(twoY0)
oneBndbox.append(twoX1)
oneBndbox.append(twoY1)
oneBndbox.append(twoX2)
oneBndbox.append(twoY2)
oneBndbox.append(twoX3)
oneBndbox.append(twoY3)
element.append(oneName)
element.append(onePose)
element.append(oneTruncated)
element.append(oneDifficult)
element.append(oneBndbox)
root.append(element)
# tree.write('D:/jyzdata/jyzbigxml/'+jsonFile[:-5] + '.xml')
tree.write(os.path.join('E:/jyzdata/JYZvideo/video/images/xml/', jsonFile[:-5]+'.xml'),encoding='utf-8',xml_declaration=True)
# with open(os.path.join('D:/jyzdata/jyzbigxml/', jsonFile[:-5]+'.xml'), 'w') as fh:
# dom.writexml(fh)
# print('写入name/pose OK!')