I am working in a project named "Handwritten Math Evaluation". So what basically happens in this is that there are 11 classes of (0 - 9) and (+, -) each containing 50 clean handwritten digits in them. Then I trained a CNN model for it with 80 % of data used in training and 20 % using in testing of model which results in an accuracy of 98.83 %. Here is the code for the architecture of CNN model:

import pandas as pd 
import numpy as np 
import pickle 
import keras 
from keras.models import Model 
from keras.layers import *
from keras import optimizers 
from keras.layers import Input, Dense 
from keras.models import Sequential 
from keras.layers import Dense 
from keras.layers import Dropout 
from keras.layers import Flatten 
from keras.layers.convolutional import Conv2D 
from keras.layers.convolutional import MaxPooling2D 
from keras.utils import np_utils 
from keras import backend as K  
from keras.utils.np_utils import to_categorical 
from keras.models import model_from_json
import matplotlib.pyplot as plt
model = Sequential() 
model.add(Conv2D(30, (5, 5), input_shape =(28,28,1), activation ='relu')) 
model.add(MaxPooling2D(pool_size =(2, 2))) 
model.add(Conv2D(15, (3, 3), activation ='relu')) 
model.add(MaxPooling2D(pool_size =(2, 2))) 
model.add(Dense(128, activation ='relu')) 
model.add(Dense(50, activation ='relu')) 
model.add(Dense(12, activation ='softmax')) 
# Compile model 
model.compile(loss ='categorical_crossentropy', 
            optimizer ='adam', metrics =['accuracy']) 
model.fit(X_train, y_train, epochs=1000)

Now each image in dataset is preprocessed as follows:

import cv2
im = cv2.imread(path)
im_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rect = rects[0]
im_crop =im_th[rect[1]:rect[1]+rect[3],rect[0]:rect[0]+rect[2]]
im_resize = cv2.resize(im_crop,(28,28))
im_resize = np.array(im_resize)

I have made an evaluation function which solves simple expression like 7+8 :-

def evaluate(im):
    s = ''
    data = []
    im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
    ret, im_th = cv2.threshold(im_gray, 90, 255, cv2.THRESH_BINARY_INV)
    ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])
    boundingBoxes = [cv2.boundingRect(c) for c in ctrs]
    look_up = ['0','1','2','3','4','5','6','7','8','9','+','-'] 
    for c in ctrs:
        rect = boundingBoxes[i]
        im_crop = im_th[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]]
        im_resize = cv2.resize(im_crop,(28,28))
        im_resize = np.array(im_resize)
        im_resize = im_resize.reshape(28,28,1)
    data = np.array(data)
    predictions = model.predict(data)
    while i<len(boundingBoxes):
        rect = boundingBoxes[i]
        s += look_up[predictions[i].argmax()]
    return s

I need help extending this, thought for compound fractions but the problem is that they are identical to subtraction sign when resized to (28, 28). So I need help in distinguishing between them.

This is my first question, so please let me know if any details are left.

  • $\begingroup$ Welcome to SE:AI! We focus on the conceptual aspects, as opposed to programming questions. Can I ask is there a way to ask the core conceptual question without so much of the code? $\endgroup$ – DukeZhou Nov 17 '20 at 3:31
  • 1
    $\begingroup$ I though to give all the details $\endgroup$ – Dhaval Bothra Nov 17 '20 at 5:10
  • $\begingroup$ We appreciate earnestness—I was concerned that the question has received two close votes and no answers as yet. (My thought was emphasizing the conceptual could facilitate getting the information that could help you solve your specific problem. Right now, I have to wonder if including all of the imports and ancillary code is keeping people away.) $\endgroup$ – DukeZhou Nov 17 '20 at 5:27
  • $\begingroup$ Ok I will edit the question $\endgroup$ – Dhaval Bothra Nov 17 '20 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.