-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDigitStructFile.py
More file actions
70 lines (63 loc) · 2.85 KB
/
Copy pathDigitStructFile.py
File metadata and controls
70 lines (63 loc) · 2.85 KB
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
import h5py
# The DigitStructFile is just a wrapper around the h5py data. It basically references
# inf: The input h5 matlab file
# digitStructName The h5 ref to all the file names
# digitStructBbox The h5 ref to all struc data
class DigitStructFile:
def __init__(self, inf):
self.inf = h5py.File(inf, 'r')
self.digitStructName = self.inf['digitStruct']['name']
self.digitStructBbox = self.inf['digitStruct']['bbox']
# getName returns the 'name' string for for the n(th) digitStruct.
def getName(self,n):
return ''.join([chr(c[0]) for c in self.inf[self.digitStructName[n][0]].value])
def bboxHelper(self,attr):
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
# getBbox returns a dict of data for the n(th) bbox.
def getBbox(self,n):
bbox = {}
bb = self.digitStructBbox[n].item()
bbox['height'] = self.bboxHelper(self.inf[bb]["height"])
bbox['label'] = self.bboxHelper(self.inf[bb]["label"])
bbox['left'] = self.bboxHelper(self.inf[bb]["left"])
bbox['top'] = self.bboxHelper(self.inf[bb]["top"])
bbox['width'] = self.bboxHelper(self.inf[bb]["width"])
return bbox
def getDigitStructure(self,n):
s = self.getBbox(n)
s['name']=self.getName(n)
return s
# getAllDigitStructure returns all the digitStruct from the input file.
def getAllDigitStructure(self):
return [self.getDigitStructure(i) for i in range(len(self.digitStructName))]
#return [self.getDigitStructure(i) for i in range(100)]
# Return a restructured version of the dataset (one structure by boxed digit).
# Return a list of such dicts :
# 'filename' : filename of the samples
# 'boxes' : list of such dicts (one by digit) :
# 'label' : 1 to 9 corresponding digits. 10 for digit '0' in image.
# 'left', 'top' : position of bounding box
# 'width', 'height' : dimension of bounding box
def getAllDigitStructure_ByDigit(self):
pictDat = self.getAllDigitStructure()
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