This repository has been archived by the owner on May 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotation_parser.py
84 lines (69 loc) · 2.79 KB
/
annotation_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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import os
import sys
if sys.version_info >= (3, 8):
from typing import TypedDict
else:
from typing_extensions import TypedDict
from typing import List
from xml.dom import minidom
class BndBox(TypedDict):
xmin: int
ymin: int
xmax: int
ymax: int
class Object(TypedDict):
name: str
pose: str
truncated: bool
difficult: bool
bndbox: BndBox
class Annotation(TypedDict):
folder: str
filename: str
path: str
database: str
width: int
height: int
depth: int
segmented: bool
objects: List[Object]
def parse_xml(file_name: str) -> Annotation:
if not os.path.exists(file_name):
raise FileNotFoundError(f"File not found: {file_name}")
doc = minidom.parse(file_name)
folder = doc.getElementsByTagName("folder")[0]
filename = doc.getElementsByTagName("filename")[0]
path = doc.getElementsByTagName("path")[0]
database = doc.getElementsByTagName("database")[0]
width = doc.getElementsByTagName("width")[0]
height = doc.getElementsByTagName("height")[0]
depth = doc.getElementsByTagName("depth")[0]
segmented = doc.getElementsByTagName("segmented")[0]
objects = doc.getElementsByTagName("object")
object_list: list[Object] = []
for obj in objects:
name = obj.getElementsByTagName("name")[0]
pose = obj.getElementsByTagName("pose")[0]
truncated = obj.getElementsByTagName("truncated")[0]
difficult = obj.getElementsByTagName("difficult")[0]
xmin = obj.getElementsByTagName("xmin")[0]
ymin = obj.getElementsByTagName("ymin")[0]
xmax = obj.getElementsByTagName("xmax")[0]
ymax = obj.getElementsByTagName("ymax")[0]
object_list.append(Object(name=str(name.firstChild.data),
pose=str(pose.firstChild.data),
truncated=str(truncated.firstChild.data) == "1",
difficult=str(difficult.firstChild.data) == "1",
bndbox=BndBox(xmin=int(xmin.firstChild.data),
ymin=int(ymin.firstChild.data),
xmax=int(xmax.firstChild.data),
ymax=int(ymax.firstChild.data))))
return Annotation(folder=str(folder.firstChild.data),
filename=str(filename.firstChild.data),
path=str(path.firstChild.data),
database=str(database.firstChild.data),
width=int(width.firstChild.data),
height=int(height.firstChild.data),
depth=int(depth.firstChild.data),
segmented=str(segmented.firstChild.data) == "1",
objects=object_list)