读取h5py文件的数据
with h5py.File(path_to_digit_struct_mat_file, 'r') as digit_struct_mat_file:
attrs = get_attrs(digit_struct_mat_file, index)
length = len(attrs['label'])
attrs_left, attrs_top, attrs_width, attrs_height = map(lambda x: [int(i) for i in x],
[attrs['left'], attrs['top'], attrs['width'], attrs['height']])
min_left, min_top, max_right, max_bottom = (min(attrs_left),
min(attrs_top),
max(map(lambda x, y: x + y, attrs_left, attrs_width)),
max(map(lambda x, y: x + y, attrs_top, attrs_height)))
def get_attrs(digit_struct_mat_file, index):
"""
Returns a dictionary which contains keys: label, left, top, width and height, each key has multiple values.
"""
attrs = {}
f = digit_struct_mat_file
item = f['digitStruct']['bbox'][index].item()
for key in ['label', 'left', 'top', 'width', 'height']:
attr = f[item][key]
values = [f[attr[()][i].item()][()][0][0] # 此处[()]是因为h5py的要求 不然有warning
for i in range(len(attr))] if len(attr) > 1 else [attr.value[0][0]]
attrs[key] = values
return attrs