An example of opencv python handwriting Recognition based on KNN

  • 2020-12-09 00:54:55
  • OfStack

OCR of Hand-written Data using kNN

OCR of Hand-written Digits

Our goal is to build an application that can read handwritten numbers. To do this, we need 1 of train_data and ES10en_data.OpenCV comes with 1 of images digits.png (in the folder opencv\sources\samples\data\), which has 5,000 handwritten numbers (500 for each number and 20x20 images for each number). So first you cut the image into 5,000 different images, each number into a single line of 400 pixels. The first 250 numbers serve as training data and the second 250 as test data.


import numpy as np
import cv2
import matplotlib.pyplot as plt

img = cv2.imread('digits.png')
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=1
knn = cv2.ml.KNearest_create()
knn.train(train, cv2.ml.ROW_SAMPLE, train_labels)
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 )

Output: 91.76

The next step to improve accuracy is to add training data, especially incorrect data. It is best to save the training data for the next time during each training.


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

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

OCR of English Alphabets

One data file, ES36en-ES37en.data, is attached to the opencv/samples/data/folder. In each row, the first column is an alphabet, which is our label. The next 16 numbers are its different characteristics.


import numpy as np
import cv2
import matplotlib.pyplot as plt


# Load the data, converters convert the letter to a number
data= np.loadtxt('letter-recognition.data', dtype= 'float32', delimiter = ',',
          converters= {0: lambda ch: ord(ch)-ord('A')})

# split the data to two, 10000 each for train and test
train, test = np.vsplit(data,2)

# split trainData and testData to features and responses
responses, trainData = np.hsplit(train,[1])
labels, testData = np.hsplit(test,[1])

# Initiate the kNN, classify, measure accuracy.
knn = cv2.ml.KNearest_create()
knn.train(trainData, cv2.ml.ROW_SAMPLE, responses)
ret, result, neighbours, dist = knn.findNearest(testData, k=5)

correct = np.count_nonzero(result == labels)
accuracy = correct*100.0/10000
print( accuracy )

Output: 93.06


Related articles: