forked from guchengxi1994/mask2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor_singleObj.py
82 lines (62 loc) · 1.82 KB
/
processor_singleObj.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
'''
@lanhuage: python
@Descripttion: https://blog.csdn.net/qq_33196814/article/details/99992771
@version: beta
@Author: xiaoshuyui
@Date: 2020-04-22 17:07:28
LastEditors: xiaoshuyui
LastEditTime: 2020-10-10 15:42:02
'''
# import json
import xmltodict
def json_to_xml(json_str):
# xmltodict库的unparse()json转xml
# 参数pretty 是格式化xml
xml_str = xmltodict.unparse(json_str, pretty=1)
return xml_str
def root2annotion(xml_str):
pass
def img2xml(folder:str,filename:str,path:str,width:int,height:int,name:str, \
xmin:int,ymin:int,xmax:int,ymax:int):
annotation = {}
# annotation['folder'] = "HBXZ"
annotation['folder'] = folder
annotation['filename'] = filename
# annotation['filename'] = "xxx.jpg"
annotation['path'] = path
# annotation['path'] = "xxxx\\xxxxx\\xxx.jpg"
source = {}
source['database'] = "Unknown"
annotation['source'] = source
size = {}
size['width'] = width
# size['width'] = 903
# size['height'] = 1722
size['height'] = height
size['depth'] = 1
annotation['size'] = size
annotation['segmented'] = 0
# object = {}
ob = {}
ob['name'] = name
ob['difficult'] = 0
# ob['name'] = 'weld'
bndbox = {}
# bndbox['xmin'] = 387
# bndbox['ymin'] = 34
# bndbox['xmax'] = 578
# bndbox['ymax'] = 1622
bndbox['xmin'] = xmin
bndbox['ymin'] = ymin
bndbox['xmax'] = xmax
bndbox['ymax'] = ymax
ob['bndbox'] = bndbox
annotation['object'] = ob
# dic = {}
dicts = {'annotation': annotation}
return json_to_xml(dicts)
# if __name__ == "__main__":
# # with open("D:\\getWeld\\insertXML\\test2.xml",'rb') as f:
# f = open("D:\\getWeld\\insertXML\\test2.xml",'w')
# f.writelines(img2xml("test","aa","asas\\aa.xx",12,23,"aaa",123,444,4523,664))
# f.close()