OCR of English alphabets in Python OpenCV

OCR which stands for Optical character recognition is a computer vision technique used to recognize characters such as digits, alphabets, signs, etc. These characters are common in day to day life and we can perform character recognition based on our requirement. We will implement optical character recognition of the English alphabets using OpenCV. here we will use the KNN algorithm which is used for classification.

Note: You can find the data here data for which we will perform the OCR.

There are 20000 rows of data containing 17 columns where the first column represents the alphabet and the remaining 16 will represent its different features. We have to process the data by converting the alphabets into ASCII characters. To perform classification we will use 10000 rows as training_data and 10000 row as testing_data.

Below is the implementation.





#Import the libraries
import cv2 as cv
import numpy as np
# Read data and use converters
# to convert the alphabets to 
# Numeric value.
data= np.loadtxt('letter-recognition',
                 dtype= 'float32',
                 delimiter = ',',
                 converters= {0: lambda ch: ord(ch)-ord('A')})
# split the data into train_data 
# and test_data
train_data, test_data = np.vsplit(data,2)
# split train_data and test_data 
# to features and responses.
responses, training = np.hsplit(train_data,[1])
classes, testing = np.hsplit(test_data,[1])
# Create the knn classifier
knn = cv.ml.KNearest_create()
knn.train(training, cv.ml.ROW_SAMPLE, responses)
# Obtain the results of the classfier
# determine the number of neighbors.
ret, Output, neighbours,
distance = knn.findNearest(testing, k=7)
# Match the Output to find the
# number of wrong predictions.
correct_OP = np.count_nonzero(Output == classes)
#calculate accuracy and display it.
accuracy = (correct_OP*100.0)/(10000)
print( accuracy )



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.