我使用OpenCV中的facerecognizer创建了一个简单的面部识别功能.它可以很好地处理来自人的图像.
现在我想通过使用手写字符而不是人来进行测试.我遇到了MNIST数据集,但是他们将图像存储在一个我从未见过的奇怪文件中.
我只需从中提取一些图像:
train-images.idx3-ubyte
Run Code Online (Sandbox Code Playgroud)
并将它们保存在文件夹中 .gif
或者我想念这个MNIST的事情.如果是,我在哪里可以获得这样的数据集?
编辑
我也有gzip文件:
train-images-idx3-ubyte.gz
Run Code Online (Sandbox Code Playgroud)
我试图阅读内容,但show()不起作用,如果read()我看到随机符号.
images = gzip.open("train-images-idx3-ubyte.gz", 'rb')
print images.read()
Run Code Online (Sandbox Code Playgroud)
编辑
通过使用以下方式管理以获得一些有用的输出:
with gzip.open('train-images-idx3-ubyte.gz','r') as fin:
for line in fin:
print('got line', line)
Run Code Online (Sandbox Code Playgroud)
不知何故,我现在必须将其转换为图像,输出:
Lau*_*RTE 40
下载培训/测试图像和标签:
比如,在工作区解压缩它们samples/.
从PyPi 获取python-mnist包:
pip install python-mnist
Run Code Online (Sandbox Code Playgroud)
导入mnist包并阅读培训/测试图像:
from mnist import MNIST
mndata = MNIST('samples')
images, labels = mndata.load_training()
# or
images, labels = mndata.load_testing()
Run Code Online (Sandbox Code Playgroud)
要将图像显示到控制台:
index = random.randrange(0, len(images)) # choose an index ;-)
print(mndata.display(images[index]))
Run Code Online (Sandbox Code Playgroud)
你会得到这样的东西:
............................
............................
............................
............................
............................
.................@@.........
..............@@@@@.........
............@@@@............
..........@@................
..........@.................
...........@................
...........@................
...........@...@............
...........@@@@@.@..........
...........@@@...@@.........
...........@@.....@.........
..................@.........
..................@@........
..................@@........
..................@.........
.................@@.........
...........@.....@..........
...........@....@@..........
............@@@@............
.............@..............
............................
............................
............................
Run Code Online (Sandbox Code Playgroud)
说明:
list的无符号字节.array无符号字节.Pun*_*rud 15
(仅使用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)
Run Code Online (Sandbox Code Playgroud)
打印图像:
import matplotlib.pyplot as plt
image = np.asarray(data[2]).squeeze()
plt.imshow(image)
plt.show()
Run Code Online (Sandbox Code Playgroud)
打印前100个标签:
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)
Run Code Online (Sandbox Code Playgroud)
小智 8
安装 idx2numpy
pip install idx2numpy
Run Code Online (Sandbox Code Playgroud)
下载数据
从官网下载MNIST数据集。
解压数据
最终,您应该拥有以下文件:
train-images-idx3-ubyte
train-labels-idx1-ubyte
t10k-images-idx3-ubyte
t10k-labels-idx1-ubyte
Run Code Online (Sandbox Code Playgroud)
使用 idx2numpy
pip install idx2numpy
Run Code Online (Sandbox Code Playgroud)
import gzip
import numpy as np
def training_images():
with gzip.open('data/train-images-idx3-ubyte.gz', 'r') as f:
# first 4 bytes is a magic number
magic_number = int.from_bytes(f.read(4), 'big')
# second 4 bytes is the number of images
image_count = int.from_bytes(f.read(4), 'big')
# third 4 bytes is the row count
row_count = int.from_bytes(f.read(4), 'big')
# fourth 4 bytes is the column count
column_count = int.from_bytes(f.read(4), 'big')
# rest is the image pixel data, each pixel is stored as an unsigned byte
# pixel values are 0 to 255
image_data = f.read()
images = np.frombuffer(image_data, dtype=np.uint8)\
.reshape((image_count, row_count, column_count))
return images
def training_labels():
with gzip.open('data/train-labels-idx1-ubyte.gz', 'r') as f:
# first 4 bytes is a magic number
magic_number = int.from_bytes(f.read(4), 'big')
# second 4 bytes is the number of labels
label_count = int.from_bytes(f.read(4), 'big')
# rest is the label data, each label is stored as unsigned byte
# label values are 0 to 9
label_data = f.read()
labels = np.frombuffer(label_data, dtype=np.uint8)
return labels
Run Code Online (Sandbox Code Playgroud)
您实际上可以使用PyPI上提供的idx2numpy软件包。它非常简单易用,可以直接将数据转换为numpy数组。这是您要做的:
从官方网站下载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
Run Code Online (Sandbox Code Playgroud)
解压缩或解压缩数据。在Linux上,您可以使用gzip
最终,您应该具有以下文件:
data/train-images-idx3-ubyte
data/train-labels-idx1-ubyte
data/t10k-images-idx3-ubyte
data/t10k-labels-idx1-ubyte
Run Code Online (Sandbox Code Playgroud)
前缀data/只是因为我已经将它们提取到名为的文件夹中data。您的问题看起来很不错,到这里为止,请继续阅读。
这是一个简单的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
Run Code Online (Sandbox Code Playgroud)
现在,您可以将OpenCV juts与显示其他图像的方式相同,例如使用
cv.imshow("Image", arr[4])
Run Code Online (Sandbox Code Playgroud)
要安装idx2numpy,可以使用PyPI(pip程序包管理器)。只需运行以下命令:
pip install idx2numpy
Run Code Online (Sandbox Code Playgroud)