forked from rykov8/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 85
/
data_svt.py
64 lines (55 loc) · 2.19 KB
/
data_svt.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
import numpy as np
import os
from xml.etree import ElementTree
from ssd_data import BaseGTUtility
class GTUtility(BaseGTUtility):
"""Utility for SVT (Street View Text) dataset.
# Arguments
data_path: Path to ground truth and image data.
test: Boolean for using training or test set.
polygon: Return oriented boxes defined by their four corner points.
Required by SegLink...
"""
def __init__(self, data_path, test=False, polygon=False):
self.data_path = data_path
if test:
gt_path = os.path.join(data_path, 'test.xml')
else:
gt_path = os.path.join(data_path, 'train.xml')
self.gt_path = gt_path
self.image_path = image_path = os.path.join(data_path, 'img')
self.classes = ['Background', 'Text']
self.image_names = []
self.data = []
self.text = []
tree = ElementTree.parse(gt_path)
root = tree.getroot()
for image_tree in root.findall('image'):
name = image_tree.find('imageName').text
image_name = os.path.split(name)[1]
resolution = image_tree.find('Resolution')
img_width = float(resolution.attrib['x'])
img_height = float(resolution.attrib['y'])
boxes = []
text = []
for box_tree in image_tree.find('taggedRectangles'):
x1 = float(box_tree.attrib['x'])
y1 = float(box_tree.attrib['y'])
x2 = x1 + float(box_tree.attrib['width'])
y2 = y1 + float(box_tree.attrib['height'])
if polygon:
box = [x1, y1, x1, y2, x2, y2, x2, y1, 1]
else:
box = [x1, y1, x2, y2, 1]
boxes.append(box)
text.append(box_tree.find('tag').text)
boxes = np.asarray(boxes)
boxes[:,0:-1:2] /= img_width
boxes[:,1:-1:2] /= img_height
self.image_names.append(image_name)
self.data.append(boxes)
self.text.append(text)
self.init()
if __name__ == '__main__':
gt_util = GTUtility('data/SVT', test=True)
print(gt_util.data)