Capstone Project Report (Digit-Recognition Using CNN)
Capstone Project Report (Digit-Recognition Using CNN)
Group members:
Seenuvasan V
Yudhiksha V
Vasunthura M
Preetha A
Abstract:
Handwritten Digit Recognition is an interesting machine
learning problem in which we have to identify the handwritten
digits through various classification algorithms. There are a
number of ways and algorithms to recognize handwritten
digits, including Deep Learning/CNN, SVM, Gaussian Naive
Bayes, KNN, Decision Trees, Random Forests, etc.
Prerequisites:
import tensorflow as tf
import matplotlib.pyplot as plt
import seaborn as sn
import numpy as np
import pandas as pd
import math
import datetime
import platform
print('Python version:', platform.python_version())
print('Tensorflow version:', tf.__version__)
print('Keras version:', tf.keras.__version__)
print(x_train.shape, y_train.shape
)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
batch_size = 128
num_classes = 10
epochs = 10
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,
3),activation='relu',input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,optimiz
er=keras.optimizers.Adadelta(),metrics=['accuracy'])
The model.fit() function of Keras will start the training of the model.
It takes the training data, validation data, epochs, and batch size.
hist = model.fit(x_train,
y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation
_data=(x_test, y_test))
print("The model has successfully trained")
model.save('mnist.h5')
print("Saving the model as mnist.h5")
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)
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.x = self.y = 0
# Creating elements
self.canvas = tk.Canvas(self, width=300, height=300, bg = "white",
cursor="cross")
self.label = tk.Label(self, text="Thinking..", font=("Helvetica", 48))
self.classify_btn = tk.Button(self, text = "Recognise", command =
self.classify_handwriting)
self.button_clear = tk.Button(self, text = "Clear", command =
self.clear_all)
# Grid structure
self.canvas.grid(row=0, column=0, pady=2, sticky=W, )
self.label.grid(row=0, column=1,pady=2, padx=2)
self.classify_btn.grid(row=1, column=1, pady=2, padx=2)
self.button_clear.grid(row=1, column=0, pady=2)
#self.canvas.bind("<Motion>", self.start_pos)
self.canvas.bind("<B1-Motion>", self.draw_lines)
def clear_all(self):
self.canvas.delete("all")
def classify_handwriting(self):
HWND = self.canvas.winfo_id() # get the handle of the canvas
rect = win32gui.GetWindowRect(HWND) # get the coordinate of the
canvas
im = ImageGrab.grab(rect)
app = App()
mainloop()
Output:
Implementation of digits recognition in command prompt.
Screenshots:
Conclusion:
Thus, created have successfully built a Python deep learning
project on handwritten digit recognition app. We have built
and trained the Convolutional neural network which is very
effective for image classification purposes. Later on, we build
the GUI where we draw a digit on the canvas then we classify
the digit and show the results.
This can be used as digitalization of scanning the signature in
future technologies.