-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
GUI.py
104 lines (74 loc) · 3.07 KB
/
GUI.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from tkinter import *
import cv2
import numpy as np
from PIL import ImageGrab
from keras.models import load_model
import webbrowser
model = load_model('mnist.h5')
image_folder = "img/"
root = Tk()
root.resizable(0, 0)
root.title("Digit Recognition System")
lastx, lasty = None, None
image_number = 0
cv = Canvas(root, width=800, height=600, bg='white')
cv.grid(row=0, column=0, pady=2, sticky=NSEW, columnspan=2)
def clear_widget():
global cv
cv.delete('all')
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=ROUND, smooth=TRUE, splinesteps=12)
lastx, lasty = x, y
def activate_event(event):
global lastx, lasty
cv.bind('<B1-Motion>', draw_lines)
lastx, lasty = event.x, event.y
cv.bind('<Button-1>', activate_event)
def Recognize_Digit():
global image_number
filename = f'img_{image_number}.png'
widget = cv
x = root.winfo_rootx() + widget.winfo_rootx()
y = root.winfo_rooty() + widget.winfo_rooty()
x1 = x + widget.winfo_width()
y1 = y + widget.winfo_height()
print(x, y, x1, y1)
# get image and save
ImageGrab.grab().crop((x, y, x1, y1)).save(image_folder + filename)
image = cv2.imread(image_folder + filename, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2GRAY)
ret, th = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
contours = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
for cnt in contours:
x, y, w, h = cv2.boundingRect(cnt)
# make a rectangle box around each curve
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 1)
# Cropping out the digit from the image corresponding to the current contours in the for loop
digit = th[y:y + h, x:x + w]
# Resizing that digit to (18, 18)
resized_digit = cv2.resize(digit, (18, 18))
# Padding the digit with 5 pixels of black color (zeros) in each side to finally produce the image of (28, 28)
padded_digit = np.pad(resized_digit, ((5, 5), (5, 5)), "constant", constant_values=0)
digit = padded_digit.reshape(1, 28, 28, 1)
digit = digit / 255.0
pred = model.predict([digit])[0]
final_pred = np.argmax(pred)
data = str(final_pred) + ' ' + str(int(max(pred) * 100)) + '%'
font = cv2.FONT_HERSHEY_SIMPLEX
fontScale = 0.5
color = (255, 0, 0)
thickness = 1
cv2.putText(image, data, (x, y - 5), font, fontScale, color, thickness)
cv2.imshow('Predictions', image)
cv2.waitKey(0)
def callback():
webbrowser.open_new(r"www.google.com")
btn_save = Button(text='Recognize Digits',width=15, height=3, command=Recognize_Digit)
btn_save.grid(row=2, column=0, pady=1, padx=1)
button_clear = Button(text='Clear Output',width=15, height=3, command=clear_widget)
button_clear.grid(row=2, column=1, pady=1, padx=1)
button_info = Button(text='Feedback', width=15, height=2, command=callback)
button_info.grid(row=3, column=0, pady=1, padx=1)
root.mainloop()