在 python 中使用 NYUv2 数据集时,我尝试获取数据集中所有 894 个类的列表。从 documentation 我知道它列在
names
文件的 nyu_depth_v2_labeled.mat
变量中。
我想要一个包含 894 个类名称字符串的数组,但是当尝试我在网上找到的方法时,我无法使其工作。
这是我的代码:
import numpy as np
import h5py
# Load the .mat file
mat_path = '/path/to/nyu_depth_v2_labeled.mat'
mat_file = h5py.File(mat_path, 'r')
# get variable 'names'
names = mat_file.get('names')
print(names)
print('-------------------------------')
print(names[:])
现在的问题是结果是这样的:
<HDF5 dataset "names": shape (1, 894), type "|O">
-------------------------------
[[<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
...
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>
<HDF5 object reference> <HDF5 object reference> <HDF5 object reference>]]
如何使数据“可读”为Python字符串?
pymatreader
可在 PyPI 上使用,即
Matlab mat 文件的便捷阅读器
安装后,你应该可以做
from pymatreader import read_mat
data = read_mat('/path/to/nyu_depth_v2_labeled.mat')
然后应该导致
是一个Pythondata
,包含mat文件的所有变量。dict
我没有能力测试它,所以请测试它并写下它是否符合你需要做的。