python - 使用 Tinker Draw Python 创建类似 MNIST 数据集的数组

标签 python tensorflow cv2 tinker

我创建了一个基本的修补 Canvas ,用户可以使用画笔进行绘画。 我将绘制线的所有 x 和 y 坐标存储在一个数组中,然后将它们的大小调整为 28x28 像素(数据集图像的大小);我可以以某种方式将该数组转换为像 MNIST 数据集( tensorflow 数据集)这样的 numpy 数组吗?

这是该数据集的示例(每个数字代表 RGB 刻度上的颜色):

[[  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0  84 185 159 151  60  36   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0 222 254 254 254 254 241 198 198 198 198 198 198 198 198 170  52   0   0   0   0   0   0]
 [  0   0   0   0   0   0  67 114  72 114 163 227 254 225 254 254 254 250 229 254 254 140   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0  17  66  14  67  67  67  59  21 236 254 106   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  83 253 209  18   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  22 233 255  83   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 129 254 238  44   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0  59 249 254  62   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 133 254 187   5   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   9 205 248  58   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0 126 254 182   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0  75 251 240  57   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0  19 221 254 166   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   3 203 254 219  35   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0  38 254 254  77   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0  31 224 254 115   1   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0 133 254 254  52   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0  61 242 254 254  52   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0 121 254 254 219  40   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0 121 254 207  18   0   0   0   0   0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0]]

编辑: 我发现有一个 cv2 用于将图像转换为 tensorflow 所需的特定数组 img_to_array(image) 。 所以现在我必须知道如何“拍摄”用户绘制的线,然后将它们转换为数组。

基本上,该程序的目的是使用 tensorflow 制作的分类神经网络来识别用户绘制的数字。

解决方案:

1)保存图像:

from win32 import win32gui
HWND = self.canvas.winfo_id()
rect = win32gui.GetWindowRect(HWND) 
img = ImageGrab.grab(rect)
img = img.resize((28, 28), PIL.Image.ANTIALIAS)

2)转换图像:

img = np.invert(img.convert('L')).ravel()
img = np.split(img, 28)
img = np.array(img)
print("Image:", img, img.shape)
img = img / 255
img = np.array(img)

3)预测结果:

prediction = self.model.predict(img.reshape(1,28,28))

最佳答案

如果您的数组是 yourarray,并且如果我理解您的问题,则不会
arr = np.array(yourarray) 足够了吗?

关于python - 使用 Tinker Draw Python 创建类似 MNIST 数据集的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60103004/

相关文章:

python - 根据自定义用户模型的顺序修改字段

python - Tensorflow - 获取像素的邻域

python - tensorflow 中什么是不可馈送的?

python - 在没有opencv的python上显示视频?

python - 在 Pillow 和 OpenCV 中打开的图像并不相同

python - 值错误 : cannot reshape array of size 230 into shape (3, 600,800)

python - 在 NLTK 解析器中使用整数/日期作为终端

python - 由于缺少 TensorBoard 安装,Spyder Anaconda 从 Keras Libraries CNN : WARNING:root:Limited tf. compat.v2.summary API 中出现错误

python - 使用 docker 和 GPU 进行 Pycharm 调试

python - R : The h5py Python package is required to save and load models 错误