-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_xml.py
55 lines (46 loc) · 1.63 KB
/
get_xml.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
import numpy as np
from xml.etree.ElementTree import parse
class PascalVocXmlParser(object):
def __init__(self):
pass
def get_fname(self, annotation_file):
root = self._root_tag(annotation_file)
return root.find("filename").text
def get_width(self, annotation_file):
tree = self._tree(annotation_file)
for elem in tree.iter():
if 'width' in elem.tag:
return float(elem.text)
def get_height(self, annotation_file):
tree = self._tree(annotation_file)
for elem in tree.iter():
if 'height' in elem.tag:
return float(elem.text)
def get_labels(self, annotation_file):
root = self._root_tag(annotation_file)
labels = []
obj_tags = root.findall("object")
for t in obj_tags:
labels.append(t.find("name").text)
return labels
def get_boxes(self, annotation_file):
root = self._root_tag(annotation_file)
bbs = []
obj_tags = root.findall("object")
for t in obj_tags:
box_tag = t.find("bndbox")
x1 = box_tag.find("xmin").text
y1 = box_tag.find("ymin").text
x2 = box_tag.find("xmax").text
y2 = box_tag.find("ymax").text
box = np.array([float(x1), float(x2), float(y1), float(y2)])
bbs.append(box)
bbs = np.array(bbs)
return bbs
def _root_tag(self, fname):
tree = parse(fname) # 解析xml文本,返回根元素。
root = tree.getroot()
return root
def _tree(self, fname):
tree = parse(fname)
return tree