-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpacker.py
69 lines (58 loc) · 2.49 KB
/
unpacker.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
import h5py
class DigitStructWrapper:
"""
Wrapper for the H5PY digitStruct files from the SVHN dataset
Creates an array of dictionaries containing the filename and bounding boxes for every digit in the image.
Adapted from https://github.com/hangyao
"""
def __init__(self, inf):
self.inf = h5py.File(inf, 'r')
self.digitStructName = self.inf['digitStruct']['name']
self.digitStructBbox = self.inf['digitStruct']['bbox']
def get_name(self, n):
"""Return the name of the n(th) digit struct"""
return ''.join([chr(c[0]) for c in self.inf[self.digitStructName[n][0]].value])
def get_attribute(self, attr):
"""Helper function for dealing with one vs. multiple bounding boxes"""
if (len(attr) > 1):
attr = [self.inf[attr.value[j].item()].value[0][0] for j in range(len(attr))]
else:
attr = [attr.value[0][0]]
return attr
def get_bbox(self, n):
"""Return a dict containing the data from the n(th) bbox"""
bbox = {}
bb = self.digitStructBbox[n].item()
bbox['height'] = self.get_attribute(self.inf[bb]["height"])
bbox['label'] = self.get_attribute(self.inf[bb]["label"])
bbox['left'] = self.get_attribute(self.inf[bb]["left"])
bbox['top'] = self.get_attribute(self.inf[bb]["top"])
bbox['width'] = self.get_attribute(self.inf[bb]["width"])
return bbox
def get_item(self, n):
"""Return the name and bounding boxes of a single image"""
s = self.get_bbox(n)
s['name'] = self.get_name(n)
return s
def unpack(self):
"""Returns a list of dicts containing all the bounding boxes"""
return [self.get_item(i) for i in range(len(self.digitStructName))]
def unpack_all(self):
pictDat = self.unpack()
result = []
structCnt = 1
for i in range(len(pictDat)):
item = {'filename': pictDat[i]["name"]}
figures = []
for j in range(len(pictDat[i]['height'])):
figure = {}
figure['height'] = pictDat[i]['height'][j]
figure['label'] = pictDat[i]['label'][j]
figure['left'] = pictDat[i]['left'][j]
figure['top'] = pictDat[i]['top'][j]
figure['width'] = pictDat[i]['width'][j]
figures.append(figure)
structCnt = structCnt + 1
item['boxes'] = figures
result.append(item)
return result