-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
113,364 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Mar 2 12:06:18 2022 | ||
@author: AKSHAY KUMAR | ||
""" | ||
|
||
from tkinter import * | ||
import numpy as np | ||
from PIL import ImageGrab | ||
from Prediction import predict | ||
|
||
window = Tk() | ||
window.title("Handwritten digit recognition") | ||
l1 = Label() | ||
|
||
|
||
def MyProject(): | ||
global l1 | ||
|
||
widget = cv | ||
# Setting co-ordinates of canvas | ||
x = window.winfo_rootx() + widget.winfo_x() | ||
y = window.winfo_rooty() + widget.winfo_y() | ||
x1 = x + widget.winfo_width() | ||
y1 = y + widget.winfo_height() | ||
|
||
# Image is captured from canvas and is resized to (28 X 28) px | ||
img = ImageGrab.grab().crop((x, y, x1, y1)).resize((28, 28)) | ||
|
||
# Converting rgb to grayscale image | ||
img = img.convert('L') | ||
|
||
# Extracting pixel matrix of image and converting it to a vector of (1, 784) | ||
x = np.asarray(img) | ||
vec = np.zeros((1, 784)) | ||
k = 0 | ||
for i in range(28): | ||
for j in range(28): | ||
vec[0][k] = x[i][j] | ||
k += 1 | ||
|
||
# Loading Thetas | ||
Theta1 = np.loadtxt('Theta1.txt') | ||
Theta2 = np.loadtxt('Theta2.txt') | ||
|
||
# Calling function for prediction | ||
pred = predict(Theta1, Theta2, vec / 255) | ||
|
||
# Displaying the result | ||
l1 = Label(window, text="Digit = " + str(pred[0]), font=('Algerian', 20)) | ||
l1.place(x=230, y=420) | ||
|
||
|
||
lastx, lasty = None, None | ||
|
||
|
||
# Clears the canvas | ||
def clear_widget(): | ||
global cv, l1 | ||
cv.delete("all") | ||
l1.destroy() | ||
|
||
|
||
# Activate canvas | ||
def event_activation(event): | ||
global lastx, lasty | ||
cv.bind('<B1-Motion>', draw_lines) | ||
lastx, lasty = event.x, event.y | ||
|
||
|
||
# To draw on canvas | ||
def draw_lines(event): | ||
global lastx, lasty | ||
x, y = event.x, event.y | ||
cv.create_line((lastx, lasty, x, y), width=30, fill='white', capstyle=ROUND, smooth=TRUE, splinesteps=12) | ||
lastx, lasty = x, y | ||
|
||
|
||
# Label | ||
L1 = Label(window, text="Handwritten Digit Recoginition", font=('Algerian', 25), fg="blue") | ||
L1.place(x=35, y=10) | ||
|
||
# Button to clear canvas | ||
b1 = Button(window, text="1. Clear Canvas", font=('Algerian', 15), bg="orange", fg="black", command=clear_widget) | ||
b1.place(x=120, y=370) | ||
|
||
# Button to predict digit drawn on canvas | ||
b2 = Button(window, text="2. Prediction", font=('Algerian', 15), bg="white", fg="red", command=MyProject) | ||
b2.place(x=320, y=370) | ||
|
||
# Setting properties of canvas | ||
cv = Canvas(window, width=350, height=290, bg='black') | ||
cv.place(x=120, y=70) | ||
|
||
cv.bind('<Button-1>', event_activation) | ||
window.geometry("600x500") | ||
window.mainloop() |
Oops, something went wrong.