-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotations.py
73 lines (64 loc) · 2.01 KB
/
annotations.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
from lxml import etree
import ntpath
def generateAnnotationFile(fName, h, w, c, xMin, yMin, xMax, yMax):
root = etree.Element('annotation')
folder = etree.Element('folder')
folder.text = 'Annotations'
root.append(folder)
fileName = etree.Element('fileName')
fileName.text = ntpath.basename(fName)
root.append(fileName)
path = etree.Element('path')
path.text = '../JPEGImages/{}'.format(ntpath.basename(fName))
root.append(path)
source = etree.Element('source')
database = etree.Element('database')
database.text = 'Unknown'
source.append(database)
root.append(source)
size = etree.Element('size')
width = etree.Element('width')
width.text = str(w)
size.append(width)
height = etree.Element('height')
height.text = str(h)
size.append(height)
depth = etree.Element('depth')
depth.text = str(c)
size.append(depth)
root.append(size)
segmented = etree.Element('segmented')
segmented.text = str(0)
root.append(segmented)
object = etree.Element('object')
name = etree.Element('name')
name.text = 'ref_box'
object.append(name)
pose = etree.Element('pose')
pose.text = 'Unspecified'
object.append(pose)
truncated = etree.Element('truncated')
truncated.text = str(0)
object.append(truncated)
difficult = etree.Element('difficult')
difficult.text = str(0)
object.append(difficult)
bndbox = etree.Element('bndbox')
xmin = etree.Element('xmin')
xmin.text = str(xMin)
ymin = etree.Element('ymin')
ymin.text = str(yMin)
xmax = etree.Element('xmax')
xmax.text = str(xMax)
ymax = etree.Element('ymax')
ymax.text = str(yMax)
bndbox.append(xmin)
bndbox.append(ymin)
bndbox.append(xmax)
bndbox.append(ymax)
object.append(bndbox)
root.append(object)
s = etree.tostring(root, pretty_print=True)
with open('./VOC2018/Annotations/{}xml'.format(ntpath.basename(fName)[:-3]), 'w') as f:
f.write(s.decode(encoding="utf-8"))
return