首页 > 解决方案 > 带有 GUI 的手写数字识别

问题描述

我在 jupyter 中写了这个,当我在没有 GUI 的情况下完成它时,它工作得很好。当我点击 gui 上的预测时,它只说数字是 0 或 2...我不知道为什么,代码有什么问题?

# https://gyazo.com/aaaf51a4f13b300d219c8bb3d85e1154 - image
# https://gyazo.com/cade86fd7474397d6b98430c371d5680 - gif

import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt

data = keras.datasets.mnist
(X_train_images, y_train_labels), (X_test_images, y_test_labels) = data.load_data()

X_train = X_train_images.reshape(X_train_images.shape[0],28,28,1)
X_test = X_test_images.reshape(X_test_images.shape[0],28,28,1)
X_train = X_train / 255
X_test = X_test / 255

y_train = keras.utils.to_categorical(y_train_labels)
y_test = keras.utils.to_categorical(y_test_labels)

model = keras.Sequential()
model.add(keras.layers.Conv2D(32, kernel_size = (3,3), activation = 'relu', input_shape=(28, 28, 1)))
model.add(keras.layers.Conv2D(64, kernel_size = (3,3), activation = 'relu'))
model.add(keras.layers.MaxPooling2D(pool_size = (2,2)))
model.add(keras.layers.Dropout(0.25))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(256, activation = 'relu'))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(10, activation = 'softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])

model.fit(X_train, y_train, batch_size = 128, epochs = 10, validation_data=(X_test, y_test))

model.save('mnist.h5')

model.evaluate(X_test, y_test)

313/313 [===============================] - 3s 8ms/步 - 损失:0.6929 - 准确度:0.8408

[0.6929071545600891, 0.8407999873161316]

from tkinter import *
from PIL import Image, ImageDraw

model = load_model('mnist.h5')

class_names = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

def draw_lines(event):
    x = event.x
    y = event.y
    
    x1 = x - 8
    y1 = y - 8
    
    x2 = x + 8
    y2 = y + 8
    
    canvas1.create_oval((x1 ,y1 ,x2 ,y2), fill = 'black')
    img_draw.ellipse((x1,y1,x2,y2), fill = 'black')


def save():
    img.resize(size = (28,28))
    img_array = np.array(img)
#     img_array.save('1.jpg') - not finished the save function yet


def exit():
    window.destroy()
    
def predict():
    img_size = img.resize(size = (28,28))
    img_array = np.array(img_size)
    img_array_shape = img_array.reshape(1,28,28,1)
    img_scaled = img_array_shape / 255
    img_prediction = model.predict(img_scaled)
    binary_label1 = np.argmax(img_prediction[0])
    binary_label2 = np.amax(img_prediction)
    label1.config(text = str(binary_label1))
    label2.config(text = str(binary_label2))
    
def clear():
    canvas1.delete('all')
    img=Image.new(mode = 'L', size = (300,300), color = 255)
    img_draw=ImageDraw.Draw(img)
    label1.config(text ='#')
    label2.config(text = '%')
    
    
window = Tk()
window.geometry('550x350')
window.title('Hello')
window.configure(background = 'white')
window.resizable(0,0)
# label1 = Label(window, bg = 'white', fg = 'black', text = 'label1')
# label1.grid(row = 4, column = 10, sticky = W)
# button1 = Button(window, bg = 'white', fg = 'black', command = 'click', width = 10)
# button1.place(x = 10, y = 10)
# entrytext1 = Entry(window, bg = 'white', width = 20)
# entrytext1.grid(row = 3, column = 10, sticky = E)
# entrytext2 = Entry(window, bg = 'white', width = 20)
# entrytext2.grid(row = 5, column = 10, sticky = E)
canvas1 = Canvas(window, bg = 'white', height = 300, width = 300)
canvas1.grid(row=0,column=0)
button1 = Button(window, bg = 'white', fg = 'black', command = clear, width = 10, text = 'Clear')
button1.grid(row = 1, column = 0, sticky = W)
button2 = Button(window, bg = 'white', fg = 'black', command = predict, width = 10, text = 'Predict')
button2.grid(row = 1, column = 1, sticky = E)
button3 = Button(window, bg = 'white', fg = 'black', command = 'click', width = 10, text = 'Save')
button3.grid(row = 1, column = 2, sticky = E)
button4 = Button(window, bg = 'white', fg = 'black', command = exit, width = 10, text = 'Exit')
button4.grid(row = 1, column = 3, sticky = E)
canvas1.bind('<B1-Motion>', draw_lines)
label1 = Label(window, bg = 'white', fg = 'black', text = '#', font = 'Arial 12 bold')
label1.grid(row = 0, column = 1)
label2 = Label(window, bg = 'white', fg = 'black', text = '%', font = 'Arial 12 bold')
label2.grid(row = 0, column = 2)
# img=Image.new(mode = 'RGB', size = (300,300), color = (0,0,0))
img=Image.new(mode = 'L', size = (300,300), color = 255)
img_draw=ImageDraw.Draw(img)
window.mainloop()

标签: pythonjupyter-notebookhandwriting-recognition

解决方案


推荐阅读