-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_for_digit_recognizer.py
75 lines (60 loc) · 2.25 KB
/
GUI_for_digit_recognizer.py
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
from keras.models import load_model
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')
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(res), max(res)
import subprocess
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import tkinter.messagebox
import os
#This creates the main window of an application
root = Tk()
root.title("Digit Recognizer")
toolBar =Menu(root)
root.configure(background='Grey',menu=toolBar)
def clear_canvas():
canvas.delete('all')
def browse_file():
global file_path
file_path = filedialog.askopenfilename()
def open_img():
browse_file()
imgname=os.path.basename(file_path)
loaded_img=ImageTk.PhotoImage(Image.open(file_path).resize((500,500),resample=0))
canvas.create_image(10,10, anchor=NW, image=loaded_img)
canvas.loaded_img = loaded_img
def predict_image():
i = Image.open(file_path)
digit,accuracy = predict_digit(i)
accuracy = str((int(accuracy*100)))
digit = str(digit)
pred_details.configure(text = digit + " with " + accuracy + " % surety")
def open_mspaint():
subprocess.Popen(['C:\\Windows\\System32\\mspaint.exe'])
canvas = Canvas(root, width = 500, height = 500)
pred_details = Label(text="Please load an Image", font=("Comic Sans MS", 30))
predict_btn = Button(text = "Predict Digit", command = predict_image)
clear_btn = Button(text = "Clear Image", command = clear_canvas)
# Grid structure
canvas.grid(row=0, column=0, pady=2, sticky=W, )
pred_details.grid(row=0, column=1,pady=2, padx=2)
predict_btn.grid(row=1, column=1, pady=2, padx=2)
clear_btn.grid(row=1, column=0, pady=2)
subMenu = Menu(toolBar,tearoff=0)
toolBar.add_cascade(label='File',menu=subMenu)
subMenu.add_command(label='Open Digit Image',command= open_img)
subMenu.add_command(label="Draw a Digit (MS Paint)", command=open_mspaint)
subMenu.add_command(label="Exit",command=root.destroy)
root.mainloop()