-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabel_parser.py
67 lines (58 loc) · 2.3 KB
/
label_parser.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
import xml.sax
from dataclasses import dataclass
@dataclass
class Label():
name: str
xmin: int
ymin: int
xmax: int
ymax: int
class LabelHandler(xml.sax.ContentHandler):
def __init__(self):
self.folder = None
self.filename = None
self.width = None
self.height = None
self.current_tag = None
self.objects = []
self.current_object = None
def startElement(self, tag, attributes):
if tag == "object":
if self.current_object is None:
self.current_object = 0
else:
self.current_object += 1
self.objects.append(Label("", 0, 0, 0, 0))
elif tag in ["xmin", "xmax", "ymin", "ymax", "name", "folder", "filename", "width", "height"]:
self.current_tag = tag
def endElement(self, tag):
if tag in ["xmin", "xmax", "ymin", "ymax", "name", "folder", "filename", "width", "height"]:
self.current_tag = None
def characters(self, content):
if self.current_tag in ["xmin", "xmax", "ymin", "ymax", "width", "height"]:
parsed_content = int(content)
if self.current_tag == "xmin":
self.objects[self.current_object].xmin = parsed_content
elif self.current_tag == "ymin":
self.objects[self.current_object].ymin = parsed_content
elif self.current_tag == "xmax":
self.objects[self.current_object].xmax = parsed_content
elif self.current_tag == "ymax":
self.objects[self.current_object].ymax = parsed_content
elif self.current_tag == "width":
self.width = parsed_content
elif self.current_tag == "height":
self.height = parsed_content
elif self.current_tag == "name":
self.objects[self.current_object].name = content
elif self.current_tag == "folder":
self.folder = content
elif self.current_tag == "filename":
self.filename = content
def parse_xml_file(fname):
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
handler = LabelHandler()
parser.setContentHandler(handler)
parser.parse(fname)
return handler.folder, handler.filename, handler.width, handler.height, handler.objects