-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI_for_MNIST.py
More file actions
89 lines (74 loc) · 2.79 KB
/
GUI_for_MNIST.py
File metadata and controls
89 lines (74 loc) · 2.79 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
79
80
81
82
83
84
85
86
87
88
89
import cv2
import tkinter as tk
from PIL import ImageGrab
from Model import *
"""erase digit"""
def clear_widget():
global cv
cv.delete("all")
def activate_event(event):
global lastx, lasty
cv.bind("<B1-Motion>", draw_lines)
lastx, lasty = event.x, event.y
"""draw digit"""
def draw_lines(event):
global lastx, lasty
x, y = event.x, event.y
cv.create_line((lastx, lasty, x, y), width=8, fill='black', capstyle=tk.ROUND, smooth=tk.TRUE, splinesteps=12)
lastx, lasty = x, y
"""recognise digit"""
def recognize_digit():
global image_number
filename = f'image_{image_number}.png'
widget = cv
x = root.winfo_rootx() + widget.winfo_x()
y = root.winfo_rooty() + widget.winfo_y()
x1 = x + widget.winfo_width()
y1 = y + widget.winfo_height()
ImageGrab.grab().crop((x, y, x1, y1)).save(filename)
image = cv2.imread(filename, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
"""capture contours"""
cotours = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
for ctr in cotours:
"""pick only sufficiently large contours"""
if cv2.contourArea(ctr) > 1500 and cv2.contourArea(ctr) < 5000:
rect = cv2.boundingRect(ctr)
x, y, w, h = rect
img = th[y:y + h, x:x + w]
"""create a rectangle around region of interest"""
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 1)
"""resize region of interest"""
img = cv2.resize(img, (28, 28))
"""scale region of interest"""
img = img/255
"""reshape region of interest"""
img = img.reshape(1, 1, 28*28)
"""predict digit"""
prediction = Model.predict(img)
prediction = prediction[0]
data = str(prediction)
font = cv2.FONT_HERSHEY_SIMPLEX
fontscale = 0.5
color = (255, 0, 0)
thickness = 1
"""output prediction on the screen"""
cv2.putText(image, data, (x, y-5), font, fontscale, color, thickness)
cv2.imshow('image', image)
cv2.waitKey(0)
root = tk.Tk()
root.resizable(0, 0)
root.title("Hand written digit recognizer")
lastx, lasty = None, None
image_number = 0
"""create Canvas"""
cv = tk.Canvas(root, width=640, height=480, bg="white")
cv.grid(row=0, column=0, pady=2, sticky=tk.W, columnspan=2)
cv.bind("<Button-1>", activate_event)
"""button to recognize digit"""
button_save = tk.Button(text="recognize digit", command=recognize_digit)
button_save.grid(row=2, column=0, pady=1, padx=1)
button_clear = tk.Button(text="clear widget", command=clear_widget)
button_clear.grid(row=2, column=1, pady=1, padx=1)
root.mainloop()