Skip to main content

Handwritten Digit Recognition using Machine Learning

The aim of this project is to implement a classification algorithm to recognize handwritten digits (0-9).

Implementation

            This project is mainly divided into three steps:

            1) Machine learning model

            2) Image processing

            3) GUI Application

We can create this project only by using in-built datasets of machine learning. But to  visualize the real world application we have added the image processing and application part in this project.

Now let’s see each step one by one in briefly.

1) Machine learning model-:

·       What is scikit-learn or sklearn?

Scikit-learn is probably the most useful library for machine learning in Python. The sklearn library contains a lot of efficient tools for machine learning and statistical modelling including classification, regression, clustering and dimensionality reduction.

Sklearn is used to build machine learning models. It should not be used for reading the data, manipulating and summarizing it. There are better libraries for that (e.g. NumPy, Pandas etc.)

·       The digit dataset-

This dataset is made up of 1797 images with 8x8 pixels. Each image, like the one shown below, is of a hand-written digit. In order to utilize an 8x8 figure like this, we’d have to first transform it into a feature vector with length 64.

·       Algorithm-

Here we can use different classification or regression model. We have created different classifiers/regressors and also check their accuracy . Let’s see below table,

Classifier/Regressor

Accuracy

Logistic Regression

0.96

Stochastic Gradient Descent

0.95

K Nearest Neighbor

0.99

Linear Regression

0.58

Random Forest

0.84

As we can see in above table, the accuracy of K nearest neighbor is more. So we have chosen K nearest neighbor classification algorithm for this particular handwritten digit recognition project.

 

K-Nearest Neighbors:

K-Nearest Neighbors is a Machine Learning model that tries to classify new value by comparing it with the values of its closest neighbors.

           In the example below, the arrow points a new data point that needs to be classified into either white or black groups. ‘K’ in K-Nearest Neighbors refers to the number of neighbors. In this case, let's assume K=3.

So, the 3 nearest neighbors of the new data points are taken into consideration. 2 White points and 1 black point, based on the majority we classify the new data point as White. This is the KNN Algorithm.



·       Split the data for training and testing

 

sklearn.model_selection.train_test_split(*arraystest_size=None,random_state=None)

 

Ø *arrays -Allowed inputs are lists, numpy arrays, scipy-sparse matrices or pandas dataframes.

 

Ø test_size float or int, default=None

If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the test split. If int, represents the absolute number of test samples. If None, the value is set to the complement of the train size. If train size is also None, it will be set to 0.25.

Ø train_sizefloat or int, default=None

If float, should be between 0.0 and 1.0 and represent the proportion of the dataset to include in the train split. If int, represents the absolute number of train samples. If None, the value is automatically set to the complement of the test size.

Ø random_state

Controls the shuffling applied to the data before applying the split.

·       Code-



2) Image Processing-:

Python module used

1.    Image

Image is a class of python library pillow. Pillow is a Python Imaging Library. The Python Imaging Library adds image processing capabilities to your Python interpreter. This library provides extensive file format support, an efficient internal representation, and fairly powerful image processing capabilities. The core image library is designed for fast access to data stored in a few basic pixel formats. It should provide a solid foundation for a general image processing tool.

Image helps us in opening the image we get from the application and cropping it accordingly.

2.    ImageOps

Same as Image, ImageOps is also a class of pillow. ImageOps is used for inverting the image and making it in greyscale.

3.    Numpy

NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.

The data, being large in size, must be converted to numpy arrays for faster computations, before sending it to the machine learning models.

·       Code


3) GUI Application-:

Python modules used

1.    Tkinter

Tkinter is a Python binding to the Tk GUI (Graphical User Interface) toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python's de facto standard GUI. Tkinter is included with standard Linux, Microsoft Windows and Mac OS X installs of Python. The name Tkinter comes from Tk interface.

With the help of Tkinter, new GUI application is created which acts as a canvas for drawing the digits. Upon drawing digits, the app predicts the digit using image processing and machine learning modules.

2.    tkcap

tkcap is a python project module which takes screenshot of the tkinter window.  

·       Code





Ø When the application starts, _init_ method is started which defines the attributes of the app such as foreground colour, background colour, pen width, etc. After this drawwidgets() method adds widgets to the application (buttons and menubar)

Ø Among other attributes, one attribute is model imports the knn model from the machine learning model.

Ø When a digit is drawn on the canvas, tkcap takes screenshot of the application and this screenshot is processed to convert it from image format to numerical data so that it can be utilized by knn model. Upon conversion, this data is saved for next step.

Ø  Now we have the model and also the data ready for the prediction. By clicking the prediction button we can get the prediction. The data is fed to the knn model and it returns the predicted value. This value is then displayed.

Experimental Result


 
             


            As we can see from above experimental result, the KNN model has predicted digits correctly. You can verify it for all digits from 0 to 9.