【问题标题】:Create an array like MNIST dataset from tinker draw Python从 tinker draw Python 创建一个类似 MNIST 数据集的数组
【发布时间】:2020-05-22 23:22:08
【问题描述】:

我创建了一个基本的修补画布,用户可以在其中使用画笔进行绘画。 我将绘制线的所有 x 和 y 坐标存储在一个数组中,然后将它们调整为 28x28 像素(数据集图像的大小);我可以以某种方式将该数组转换为像 MNIST 数据集(张量流数据集)这样的 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))

【问题讨论】:

    标签: python tensorflow cv2 tinker


    【解决方案1】:

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

    【讨论】:

    • 问题是我保存了绘制线的 x 和 y 坐标(例如 [[1,2] [2,3]]),而 mnist 数据集基于颜色不在线条的位置(示例是问题中的那个)。
    • 为什么要对像 MNIST 这样的数据集使用 3 通道 (RGB)?为什么不只有灰度和单通道?这样您可以将数据空间从 (28 x 28 x 3) 节省到 (28 x 28 x 1)
    【解决方案2】:

    这是使用 PIL(Python Imaging Library 或 Pillow)读取图像并将其转换为 numpy 数组的方法-

    from PIL import Image
    import numpy as np
    import sys
    
    try:
        # Absolute path to image-
        # 'Image.open()' method reads the image file. Pillow can read
        # over 30 different file formats
        pic = Image.open("/aboslute_path/pic.jpeg")
    
    except IOError:
        print("\nCannot open/load file.\n")
        sys.exit(1)
    
    # Pillow allows us to get some basic information about the image-
    print("\nImage basic information:")
    print("Format = {0}, Size = {1}, Mode = {2}".format(
        pic.format, pic.size, pic.mode))
    
    
    # Convert PIL image to numpy array-
    pic_np = np.array(pic.getdata()).reshape(pic.size[0], pic.size[1], 3)
    

    这里,3 表示 RGB 或 3 个通道。如果您的图像是单通道,则将 3 替换为 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2023-01-08
      • 2017-01-10
      • 2019-05-26
      • 1970-01-01
      • 2011-02-16
      • 2019-10-30
      相关资源
      最近更新 更多