我使用OpenCV中的facerecognizer创建了一个简单的facerecognizer函数。它能很好地处理人们的图像。
现在我想用手写字符代替人来做一个测试。我遇到了MNIST数据集,但它们将图像存储在一个奇怪的文件中,这是我以前从未见过的。
我只需要从中提取一些图像:
train-images.idx3-ubyte
并将它们保存在一个文件夹中作为.gif
或者我是不是误解了这件事。如果是,我在哪里可以得到这样的数据集?
编辑
我还有gzip文件:
train-images-idx3-ubyte.gz
我试图阅读内容,但是show()
不起作用,如果我read()
看到随机符号。
images = gzip.open("train-images-idx3-ubyte.gz", 'rb')
print images.read()
编辑
通过使用以下命令获得一些有用的输出:
with gzip.open('train-images-idx3-ubyte.gz','r') as fin:
for line in fin:
print('got line', line)
不知何故,我现在必须将其转换为图像,输出:
实际上,您可以使用PyPI提供的idx2numpy包。它的非常简单易用,直接将数据转换为numpy数组。 你要做的是:
下载数据
从official website下载MNIST数据集。
如果您使用的是Linux,那么可以使用wget从命令行本身获取它。快跑:
解压缩数据
解压缩数据。在Linux上,您可以使用gzip
最终,您应该拥有以下文件:
前缀
data/
只是因为我已经将它们提取到名为data
的文件夹中。你的问题在这里之前看起来还不错,所以继续读下去。使用idx2numpy
这里有一个简单的python代码,可以将解压文件中的所有内容作为numpy数组读取。
现在,您可以将它与OpenCV juts一起使用,其方式与显示任何其他图像的方式相同,例如
要安装idx2numpy,可以使用PyPI(
pip
包管理器)。只需运行命令:(仅使用matplotlib、gzip和numpy)
提取图像数据:
打印图像:
打印前50个标签:
下载培训/测试图像和标签:
在一个工作场所解压,比如
samples/
。从PyPi获取python-mnist包:
导入
mnist
包并读取训练/测试图像:要在控制台上显示图像,请执行以下操作:
你会得到这样的东西:
说明:
list
。array
。相关问题 更多 >
编程相关推荐