Optical Character Recognizer using kNN and OpenCV ! Part2.

sudhir jain
3 min readSep 11, 2017

--

Update: This article is part of a series. Check out the full series: Part 1 and Part 2.

The very basic method to do OCR is using kNN . Prerequisite of this method is a basic knowledge of Python ,OpenCV and Machine Learning.

The whole process can classified in two group.

  1. Training our ML model and knowing it’s efficiency .
  2. Loading the model created to recognize the character.

Lets Begin with the Training Model.

I am going to use this image to train our model with Hand written character.

This Image contains 5000 handwritten digits 500 each. You can save this file as “digits.png” in your program directory.

We basically needs to extract the 5000 images from this file out of which we will use 2500 to train our model and 2500 to test the efficiency of our model.

import cv2
import numpy as np
#Load the training image
img = cv2.imread("digits.png")
#Convert this Image in gray scale
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
# Now we split the image to 5000 cells, each 20x20 size
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]
# Make it into a Numpy array. It size will be (50,100,20,20)
x = np.array(cells)

# Now we prepare train data and test data.
train = x[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400)

test = x[:,50:100].reshape(-1,400).astype(np.float32) # Size = (2500,400)
# Create labels for train and test data
k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]

test_labels = train_labels.copy()
# Initiate kNN, train the data, then test it with test data for k=5
knn = cv2.ml.KNearest_create()
knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)

By this small piece of code we have trained our kNN model. Now can test its accuracy. If you have used the same process, the accuracy of the Model will be around 92%. So its a good habit to at least save 20% of your data set for testing purposes to know accuracy of your ML Model.

ret,result,neighbours,dist = knn.findNearest(test,k=5)
# Now we check the accuracy of classification
# For that, compare the result with test_labels and check which are wrong
matches = result==test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100.0/result.size
print accuracy

We can also save the model for future use. Because every time training the model to test our images will be time taking process.

# save the kNN Model
np.savez('knn_data.npz',train=train, train_labels=train_labels)

Now the Part 2 of this programs starts where we can load the trained model and test our own image.

I will using this image for predicting you can save it as “test.jpg” in your working directory.

#Load the kNN Model
with
np.load('knn_data.npz') as data:
print data.files
train = data['train']
train_labels = data['train_labels']

knn = cv2.ml.KNearest_create()
knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)

test_img=cv2.imread("test.jpg")
test_img =cv2.cvtColor(test_img,cv2.COLOR_BGR2GRAY)
test_img =cv2.resize(test_img, (20, 20))
x = np.array(test_img)
test_img = x.reshape(-1,400).astype(np.float32)
ret,result,neighbours,dist = knn.findNearest(test_img,k=1)
#Print the predicted number
print int(result)

While predicting keep the value of k Nearest Neighbors(k) be odd as having a even value can cause a draw and will be conflicting.

I hope you find this blog helpful. Will cover the kNN in more detail with bigger data set in the next part.

Do follow me for more technical blogs.

--

--