通过Python从.idx3-ubyte文件或GZIP中提取图像

问题描述 投票:13回答:4

我使用OpenCV中的facerecognizer创建了一个简单的面部识别功能。它可以很好地处理来自人的图像。

现在我想通过使用手写字符而不是人来进行测试。我遇到了MNIST数据集,但是他们将图像存储在一个我从未见过的奇怪文件中。

我只需从中提取一些图像:

train-images.idx3-ubyte

并将它们保存在.gif文件夹中

或者我想念这个MNIST的事情。如果是,我在哪里可以获得这样的数据集?

编辑

我也有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)

不知何故,我现在必须将其转换为图像,输出:

enter image description here

python mnist
4个回答
37
投票

下载培训/测试图像和标签:

  • train-images-idx3-ubyte.gz:训练集图像
  • train-labels-idx1-ubyte.gz:训练集标签
  • t10k-images-idx3-ubyte.gz:测试集图像
  • t10k-labels-idx1-ubyte.gz:测试集标签

samples/说,并在工作区解压缩它们。

从PyPi获取python-mnist包:

pip install python-mnist

导入mnist包并阅读培训/测试图像:

from mnist import MNIST

mndata = MNIST('samples')

images, labels = mndata.load_training()
# or
images, labels = mndata.load_testing()

要将图像显示到控制台:

index = random.randrange(0, len(images))  # choose an index ;-)
print(mndata.display(images[index]))

你会得到这样的东西:

............................
............................
............................
............................
............................
.................@@.........
..............@@@@@.........
............@@@@............
..........@@................
..........@.................
...........@................
...........@................
...........@...@............
...........@@@@@.@..........
...........@@@...@@.........
...........@@.....@.........
..................@.........
..................@@........
..................@@........
..................@.........
.................@@.........
...........@.....@..........
...........@....@@..........
............@@@@............
.............@..............
............................
............................
............................

说明:

  • 图像列表的每个图像都是无符号字节的Python list
  • 标签是一个无符号字节的Python array

8
投票

(仅使用matplotlib,gzip和numpy) 提取图像数据:

import gzip
f = gzip.open('train-images-idx3-ubyte.gz','r')

image_size = 28
num_images = 5

import numpy as np
f.read(16)
buf = f.read(image_size * image_size * num_images)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, image_size, image_size, 1)

打印图像:

import matplotlib.pyplot as plt
image = np.asarray(data[2]).squeeze()
plt.imshow(image)
plt.show()

enter image description here

打印前50个标签:

f = gzip.open('train-labels-idx1-ubyte.gz','r')
f.read(8)
for i in range(0,50):   
    buf = f.read(1)
    labels = np.frombuffer(buf, dtype=np.uint8).astype(np.int64)
    print(labels)

2
投票

您实际上可以使用PyPI提供的idx2numpy包。它非常简单,可以直接将数据转换为numpy数组。这是你要做的:

下载数据

official website下载MNIST数据集。 如果您正在使用Linux,那么您可以使用wget从命令行本身获取它。赶紧跑:

wget http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
wget http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz

解压缩数据

解压缩或解压缩数据。在Linux上,您可以使用gzip

最终,您应该拥有以下文件:

data/train-images-idx3-ubyte
data/train-labels-idx1-ubyte
data/t10k-images-idx3-ubyte
data/t10k-labels-idx1-ubyte

前缀data/只是因为我将它们提取到名为data的文件夹中。你的问题看起来就像你在这里做得好,所以继续阅读。

使用idx2numpy

这是一个简单的python代码,用于将解压缩文件中的所有内容读取为numpy数组。

import idx2numpy
import numpy as np
file = 'data/train-images-idx3-ubyte'
arr = idx2numpy.convert_from_file(file)
# arr is now a np.ndarray type of object of shape 60000, 28, 28

您现在可以使用OpenCV juts,就像使用类似的方式显示任何其他图像一样

cv.imshow("Image", arr[4])

要安装idx2numpy,可以使用PyPI(pip包管理器)。只需运行命令:

pip install idx2numpy

1
投票

使用它将mnist数据库提取到python中的图像和csv标签:

https://github.com/sorki/python-mnist

© www.soinside.com 2019 - 2024. All rights reserved.