forked from isabek/XmlToTxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectmapper.py
77 lines (60 loc) · 2.09 KB
/
objectmapper.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
import logging
import declxml as xml
class ObjectMapper(object):
def __init__(self):
self.processor = xml.user_object("annotation", Annotation, [
xml.user_object("size", Size, [
xml.integer("width"),
xml.integer("height"),
]),
xml.array(
xml.user_object("object", Object, [
xml.string("name"),
xml.user_object("bndbox", Box, [
xml.integer("xmin"),
xml.integer("ymin"),
xml.integer("xmax"),
xml.integer("ymax"),
], alias="box")
]),
alias="objects"
),
xml.string("filename")
])
def bind(self, xml_file_path):
return xml.parse_from_file(self.processor, xml_file_path=xml_file_path)
def bind_files(self, xml_file_paths):
result = []
for xml_file_path in xml_file_paths:
try:
result.append(self.bind(xml_file_path=xml_file_path))
except Exception as e:
logging.error("%s", e.args)
return result
class Annotation(object):
def __init__(self):
self.size = None
self.objects = None
self.filename = None
def __repr__(self):
return "Annotation(size={}, object={}, filename={})".format(self.size, self.objects, self.filename)
class Size(object):
def __init__(self):
self.width = None
self.height = None
def __repr__(self):
return "Size(width={}, height={})".format(self.width, self.height)
class Object(object):
def __init__(self):
self.name = None
self.box = None
def __repr__(self):
return "Object(name={}, box={})".format(self.name, self.box)
class Box(object):
def __init__(self):
self.xmin = None
self.ymin = None
self.xmax = None
self.ymax = None
def __repr__(self):
return "Box(xmin={}, ymin={}, xmax={}, ymax={})".format(self.xmin, self.ymin, self.xmax, self.ymax)