-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_digit_recognizer.py
More file actions
78 lines (64 loc) · 2.63 KB
/
gui_digit_recognizer.py
File metadata and controls
78 lines (64 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from keras.models import load_model
from tkinter import *
import tkinter as tk
import win32gui
from PIL import ImageGrab, Image
import numpy as np
import cv2
from skimage import img_as_ubyte
from skimage.color import rgb2gray
model = load_model('mnist.h5')
def predict_digit(img):
img = rgb2gray(img)
img_gray_u8 = img_as_ubyte(img)
(thresh, im_binary) = cv2.threshold(img_gray_u8, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
img_resized = cv2.resize(im_binary,(28,28))
im_gray_invert = 255 - img_resized
#cv2.imshow("invert image", im_gray_invert)
im_final = im_gray_invert.reshape(1,28,28,1)
ans = model.predict(im_final)
ans = np.argmax(ans,axis=1)[0]
#resize image to 28x28 pixels
#img = img.resize((28,28))
#convert rgb to grayscale
#img = img.convert('L')
#img = np.array(img)
#reshaping to support our model input and normalizing
#img = img.reshape(1,28,28,1)
#img = img/255.0
#predicting the class
#res = model.predict([img])[0]
return np.argmax(ans), 2#max(ans)
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() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas
a,b,c,d = rect
rect=(a+4,b+4,c-4,d-4)
im = ImageGrab.grab(rect)
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()