Handwritten Equation Solver using Convolutional Neural Network

Vipul Gupta
4 min readJun 22, 2019

--

Introduction

With the advancement in technology, machine learning and deep learning are playing a crucial role in present times. Now, machine learning and deep learning techniques are being employed in handwriting recognition, robotics, artificial intelligence, and many more fields. Developing such system requires training our machines with data, making it capable to learn and make required prediction. This article presents a Handwritten Equation Solver trained by handwritten digits and mathematical symbol using Convolutional Neural Network with some image processing techniques to achieve a decent accuracy of 73.46%.

Acquiring Training Data

1. Downloading Dataset

We can download the dataset from this link. Extract the zip file. There will be different folders containing images for different maths symbol. For simplicity, we are using 0–9 digits, +, — and, times images in our equation solver. On observing our dataset, we can see that it is biased for some of the digits/symbols, as it contains 12000 images for some symbol and 3000 images for others. To remove this bias, reduce the number of images in each folder to approx. 4000.

Sample images in our dataset

2. Extracting Features

We can use contour extraction to obtain features.

  1. Invert the image and then convert it to a binary image because contour extraction gives the best result when the object is white and surrounding is black.
  2. To find contours use ‘findContour’ function. For features, we obtain the bounding rectangle of contour using ‘boundingRect’ function (Bounding rectangle is the smallest horizontal rectangle enclosing the entire contour).
  3. Since each image in our dataset contains only one symbol/digit, we only need the bounding rectangle of maximum size. For this purpose, we calculate the area of the bounding rectangle of each contour and select the rectangle with maximum area.
  4. Now, resize the maximum area bounding rectangle to 28 by 28. Reshape it to 784 by 1. So there will be now 784-pixel values or features. Now, give the corresponding label to it (For e.g, for 0–9 images same label as their digit, for — assign label 10, for + assign label 11, for times assign label 12). So now our dataset contains 784 features column and one label column. After extracting features, save the data to a CSV file.

Training data using a convolutional neural network

Since convolutional neural network works on two-dimensional data and our dataset is in the form of 785 by 1. Therefore, we need to reshape it. Firstly, assign the labels column in our dataset to variable y_train. Then drop the labels column from the dataset and then reshape the dataset to 28 by 28. Now, our dataset is ready for CNN.

1. Building Convolutional Neural Network

For making CNN, import all the necessary libraries.

Convert the y_train data to categorical data using ‘to_categorical’ function. For making model, use the following line of code.

2. Fitting model to data

For fitting CNN to data use the following lines of code.

It will take around three hours to train our model. After training, we can save our model as json file for future use, So that we don’t have to train our model and wait for three hours every time. To save our model, we can use the following line of codes.

Testing Our Model or Solving Equations using it

Firstly, we import our saved model using the following line of codes.

  1. Now, input an image containing a handwritten equation. Convert the image to a binary image and then invert the image(if digits/symbols are in black).
  2. Now obtain contours of the image by default, it will obtain contours from left to right.
  3. Obtain bounding rectangle for each contour.
  4. Sometimes, we may get two or more contours for the same digit/symbol. To avoid that, we can check if the bounding rectangle of those two contours overlaps or not. If they overlap, then discard the smaller rectangle.
  5. Now, resize all the remaining bounding rectangle to 28 by 28.
  6. Using our model, predict the corresponding digit/symbol for each bounding rectangle and store it in a string.
  7. After that use ‘eval’ function on the string to solve the equation.
Sample Handwritten Equation Image
Separate contour of each digit/symbol

Thank you for reading my article. You can download the full code for Handwritten equation solver from here: https://github.com/vipul79321/Handwritten-Equation-Solver

--

--

Vipul Gupta
Vipul Gupta

Written by Vipul Gupta

Some infinities are bigger than other infinities

Responses (4)