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

2024-09-25 08:34:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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)

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

enter image description here


Tags: 文件数据图像编辑readlinetrainopen
3条回答

实际上,您可以使用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

(仅使用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)

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

  • 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]))

你会得到这样的东西:

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

说明:

  • 每个图像列表的图像都是一个无符号字节的Pythonlist
  • 标签是无符号字节的Pythonarray

相关问题 更多 >