from keras.models import load_model
from tkinter import *
import tkinter as tk #Tkinter包编写一些图形用户界面程序
import win32gui #主要的作用是方便python开发者快速调用windows API
from PIL import ImageGrab, Image #ImageGrab模块用于将当前屏幕的内容或者剪贴板上的内容拷贝到PIL图像内存。
import numpy as np
model = load_model('mnist.h5') #导入数据
def predict_digit(img):
#resize image to 28x28 pixels
img = img.resize((28,28)) #缩放图片
#convert rgb to grayscale
img = img.convert('L') #image = image.convert() 是图像实例对象的一个方法,用以指定一种色彩模式,L: 8位像素,黑白
img = np.array(img) #将图片转化成像素矩阵
#reshaping to support our model input and normalizing
img = img.reshape(1,28,28,1) #将原二维向量变成1*28*28*1的4维向量
img = img/255.0 #像素数值归一化
#predicting the class
res = model.predict([img])[0] #返回值:每个测试集的所预测的各个类别的概率
return np.argmax(res), max(res) #np.argmax,返回一个numpy数组中最大值的索引值
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements 设置按钮并绑定功能
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white", cursor="cross") #画布
self.label = tk.Label(self, text="Draw..", font=("Helvetica", 48)) #标签
self.classify_btn = tk.Button(self, text = "Recognise", command = self.classify_handwriting) #识别按钮
self.button_clear = tk.Button(self, text = "Clear", command = self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, ) #规定位置
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines) #鼠标左键绑定画笔
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # 用pywin32获得tkinter窗口句柄,实现在上面绘图
rect = win32gui.GetWindowRect(HWND) # 该函数返回指定窗口的边框矩形的大小,
a,b,c,d = rect
rect=(a+4,b+4,c-4,d-4)
im = ImageGrab.grab(rect) #PIL.ImageGrab.grab()方法拍摄屏幕快照。边框内的像素在Windows上以“RGB”图像的形式返回
digit, acc = predict_digit(im) #得到数字和正确率
self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%') #显示标签
def draw_lines(self, event):
self.x = event.x
self.y = event.y
r=8
self.canvas.create_oval(self.x-r, self.y-r, self.x + r, self.y + r, fill='black') #创建画笔
app = App() #运行
mainloop() #显示
手写数字识别注释(2)
于 2022-02-21 10:54:20 首次发布