-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpascalVOC_writer.py
More file actions
39 lines (33 loc) · 1.33 KB
/
pascalVOC_writer.py
File metadata and controls
39 lines (33 loc) · 1.33 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
import os
from jinja2 import Environment, PackageLoader
class Writer:
def __init__(self, path, width, height, depth=3, database='Unknown', segmented=0):
environment = Environment(loader=PackageLoader('pascalVOC_writer', 'templates'), keep_trailing_newline=True)
self.annotation_template = environment.get_template('annotation.xml')
abspath = os.path.abspath(path)
self.template_parameters = {
'path': abspath,
'filename': os.path.basename(abspath),
'folder': os.path.basename(os.path.dirname(abspath)),
'width': width,
'height': height,
'depth': depth,
'database': database,
'segmented': segmented,
'objects': []
}
def addObject(self, name, xmin, ymin, xmax, ymax, pose='Unspecified', truncated=0, difficult=0):
self.template_parameters['objects'].append({
'name': name,
'xmin': xmin,
'ymin': ymin,
'xmax': xmax,
'ymax': ymax,
'pose': pose,
'truncated': truncated,
'difficult': difficult,
})
def save(self, annotation_path):
with open(annotation_path, 'w') as file:
content = self.annotation_template.render(**self.template_parameters)
file.write(content)