-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
80 lines (64 loc) · 2.17 KB
/
test.py
File metadata and controls
80 lines (64 loc) · 2.17 KB
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
from xml.dom import minidom, Node
def write_to_file(img_name,food_type, file_name):
doc = minidom.Document()
# generate the annotation
annotation = doc.createElement('annotation')
doc.appendChild(annotation)
# the folder
folder = doc.createElement('folder')
folder.appendChild(doc.createTextNode("PFID2009"))
annotation.appendChild(folder)
# the filename
filename = doc.createElement("filename")
filename.appendChild(doc.createTextNode(img_name))
annotation.appendChild(filename)
# size
size = doc.createElement('size')
width = doc.createElement('width')
height = doc.createElement('height')
depth = doc.createElement('depth')
width.appendChild(doc.createTextNode('1944'))
height.appendChild(doc.createTextNode('2592'))
depth.appendChild(doc.createTextNode('3'))
size.appendChild(width)
size.appendChild(height)
size.appendChild(depth)
annotation.appendChild(size)
# segmented
segmented = doc.createElement('segmented')
segmented.appendChild(doc.createTextNode("0"))
annotation.appendChild(segmented)
# object
object = doc.createElement('object')
name = doc.createElement('name')
pose = doc.createElement('pose')
truncated = doc.createElement('truncated')
difficult = doc.createElement('difficult')
bndbox = doc.createElement('bndbox')
xmin = doc.createElement('xmin')
ymin = doc.createElement('ymin')
xmax = doc.createElement('xmax')
ymax = doc.createElement('ymax')
name.appendChild(doc.createTextNode(food_type))
pose.appendChild(doc.createTextNode('Unspecified'))
truncated.appendChild(doc.createTextNode('0'))
difficult.appendChild(doc.createTextNode('0'))
xmin.appendChild(doc.createTextNode('0'))
ymin.appendChild(doc.createTextNode('0'))
xmax.appendChild(doc.createTextNode('1944'))
ymax.appendChild(doc.createTextNode('2592'))
bndbox.appendChild(xmin)
bndbox.appendChild(ymin)
bndbox.appendChild(xmax)
bndbox.appendChild(ymax)
object.appendChild(name)
object.appendChild(pose)
object.appendChild(truncated)
object.appendChild(difficult)
object.appendChild(bndbox)
annotation.appendChild(object)
print doc.toprettyxml()
# write to xml file
f = open(file_name, "w")
f.write(doc.toprettyxml())
f.close()