-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (48 loc) · 1.56 KB
/
app.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
from tkinter import *
from PIL import ImageGrab
import cv2
from keras.engine.saving import load_model
from numpy import argmax
canvas_width =280
canvas_height = 280
interface_scaling = 1.25 #win10 interface scaling
def paint(event):
color = "#000000"
x1, y1 = (event.x - 5), (event.y - 5)
x2, y2 = (event.x + 5), (event.y + 5)
w.create_oval(x1, y1, x2, y2, fill=color)
def predict(event):
master.update_idletasks()
x2 = master.winfo_rootx() + w.winfo_x()+1
y2 = master.winfo_rooty() + w.winfo_y()
x1 = x2 + w.winfo_width()
y1 = y2 + w.winfo_height()
ImageGrab.grab().crop((x2*interface_scaling, y2*interface_scaling, x1*interface_scaling, y1*interface_scaling)).save("temp.jpg")
img = cv2.imread("temp.jpg",cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (28,28), interpolation=cv2.INTER_CUBIC)
for i in range(len(img)):
for j in range(len(img[0])):
img[i][j] = 255 - img[i][j]
img = img.reshape(-1,28,28,1)
prediction = model.predict(img)
index = argmax(prediction)
print(f"{index} - {prediction[0][index]*100}%")
def clean_screen(event):
w.delete("all")
model = load_model("models/convolutional_neural_network_30_86.h5")
master = Tk()
master.geometry("280x280")
master.title("App")
w = Canvas(master,
width=canvas_width,
height=canvas_height,
bd=0,
highlightthickness=0,
relief='ridge')
w.pack()
w.configure(bg="#FFFFFF")
w.bind("<B1-Motion>", paint)
w.bind("<space>", clean_screen)
w.bind("<Return>", predict)
w.focus_set()
mainloop()