Well, I decided to workout myself on my question to solve the above problem. What I wanted is to implement a simplsimple OCR using KNearest or SVM features in OpenCV. And below is what I did and how. ( itit is just for learning how to use KNearest for simple OCR purposes).
1) My first question was about letter_recognition.dataletter_recognition.data file that comes with OpenCV samples. I wanted to know what is inside that file.
And this SOF helped me to find it. These 16 features are explained in the paper Letter Recognition Using Holland-Style Adaptive Classifiers.
( AlthoughAlthough I didn't understand some of the features at the end)
So I just decided to take all the pixel values as my features. So I just decided to take all the pixel values as my features. (I was not worried about accuracy or performance, I just wanted it to work, at least with the least accuracy)
I took the below image for my training data:
( II know the amount of training data is less. But, since all letters are of the same font and size, I decided to try on this).
To prepare the data for training, I made a small code in OpenCV. It does the following things:
- It loads the image.
- Selects the digits ( obviouslyobviously by contour finding and applying constraints on area and height of letters to avoid false detections).
- Draws the bounding rectangle around one letter and wait for
key press manually. This time we press the digit key ourselves corresponding to the letter in the box. - Once the corresponding digit key is pressed, it resizes this box to 10x10 and saves all 100 pixel values in an array (here, samples) and corresponding manually entered digit in another array(here, responses).
- Then save both the arrays in separate txt
.txtfiles.
At the end of the manual classification of digits, all the digits in the traintraining data ( train.pngtrain.png) are labeled manually by ourselves, image will look like below:
Below is the code I used for the above purpose ( ofof course, not so clean):
For the testing part, I used the below image, which has the same type of letters I used to trainfor the training phase.
- Load the txt
.txtfiles we already saved earlier - create aan instance of the classifier we are using ( here, itit is KNearest in this case)
- Then we use KNearest.train function to train the data
- We load the image used for testing
- process the image as earlier and extract each digit using contour methods
- Draw a bounding box for it, then resize it to 10x10, and store its pixel values in an array as done earlier.
- Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognisesrecognizes the correct digit.)
I included last two steps ( trainingtraining and testing) in single code below:
Here it worked with 100% accuracy. I assume this is because all the digits are of the same kind and the same size.
But any wayanyway, this is a good start to go for beginners ( II hope so).