Skip to main content

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:

  1. It loads the image.
  2. Selects the digits ( obviouslyobviously by contour finding and applying constraints on area and height of letters to avoid false detections).
  3. 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.
  4. 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).
  5. Then save both the arrays in separate txt.txt files.

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.

  1. Load the txt.txt files we already saved earlier
  2. create aan instance of the classifier we are using ( here, itit is KNearest in this case)
  3. Then we use KNearest.train function to train the data
  1. We load the image used for testing
  2. process the image as earlier and extract each digit using contour methods
  3. Draw a bounding box for it, then resize it to 10x10, and store its pixel values in an array as done earlier.
  4. 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).

Well, I decided to workout myself on my question to solve above problem. What I wanted is to implement a simpl OCR using KNearest or SVM features in OpenCV. And below is what I did and how. ( it is just for learning how to use KNearest for simple OCR purposes).

1) My first question was about letter_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. ( Although I didn't understand some of the features at end)

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 below image for my training data:

( I know the amount of training data is less. But, since all letters are of 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 following things:

  1. It loads the image.
  2. Selects the digits ( obviously by contour finding and applying constraints on area and height of letters to avoid false detections).
  3. 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 box.
  4. Once corresponding digit key is pressed, it resizes this box to 10x10 and saves 100 pixel values in an array (here, samples) and corresponding manually entered digit in another array(here, responses).
  5. Then save both the arrays in separate txt files.

At the end of manual classification of digits, all the digits in the train data( train.png) are labeled manually by ourselves, image will look like below:

Below is the code I used for above purpose ( of course, not so clean):

For testing part I used below image, which has same type of letters I used to train.

  1. Load the txt files we already saved earlier
  2. create a instance of classifier we are using ( here, it is KNearest)
  3. Then we use KNearest.train function to train the data
  1. We load the image used for testing
  2. process the image as earlier and extract each digit using contour methods
  3. Draw bounding box for it, then resize to 10x10, and store its pixel values in an array as done earlier.
  4. Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognises the correct digit.)

I included last two steps ( training and testing) in single code below:

Here it worked with 100% accuracy. I assume this is because all the digits are of same kind and same size.

But any way, this is a good start to go for beginners ( I hope so).

Well, I decided to workout myself on my question to solve the above problem. What I wanted is to implement a simple OCR using KNearest or SVM features in OpenCV. And below is what I did and how. (it is just for learning how to use KNearest for simple OCR purposes).

1) My first question was about letter_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. (Although I didn't understand some of the features at the end)

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:

(I 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:

  1. It loads the image.
  2. Selects the digits (obviously by contour finding and applying constraints on area and height of letters to avoid false detections).
  3. 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.
  4. 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).
  5. Then save both the arrays in separate .txt files.

At the end of the manual classification of digits, all the digits in the training data  (train.png) are labeled manually by ourselves, image will look like below:

Below is the code I used for the above purpose (of course, not so clean):

For the testing part, I used the below image, which has the same type of letters I used for the training phase.

  1. Load the .txt files we already saved earlier
  2. create an instance of the classifier we are using (it is KNearest in this case)
  3. Then we use KNearest.train function to train the data
  1. We load the image used for testing
  2. process the image as earlier and extract each digit using contour methods
  3. Draw a bounding box for it, then resize it to 10x10, and store its pixel values in an array as done earlier.
  4. Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognizes the correct digit.)

I included last two steps (training 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 anyway, this is a good start to go for beginners (I hope so).

List markdown
Source Link
genpfault
  • 52.3k
  • 12
  • 93
  • 153

A) It loads the image.

B) Selects the digits ( obviously by contour finding and applying constraints on area and height of letters to avoid false detections).

C) 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 box.

D) Once corresponding digit key is pressed, it resizes this box to 10x10 and saves 100 pixel values in an array (here, samples) and corresponding manually entered digit in another array(here, responses).

E) Then save both the arrays in separate txt files.

  1. It loads the image.
  2. Selects the digits ( obviously by contour finding and applying constraints on area and height of letters to avoid false detections).
  3. 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 box.
  4. Once corresponding digit key is pressed, it resizes this box to 10x10 and saves 100 pixel values in an array (here, samples) and corresponding manually entered digit in another array(here, responses).
  5. Then save both the arrays in separate txt files.

A) Load the txt files we already saved earlier

B) create a instance of classifier we are using ( here, it is KNearest)

C) Then we use KNearest.train function to train the data

  1. Load the txt files we already saved earlier
  2. create a instance of classifier we are using ( here, it is KNearest)
  3. Then we use KNearest.train function to train the data

A) We load the image used for testing

B) process the image as earlier and extract each digit using contour methods

C) Draw bounding box for it, then resize to 10x10, and store its pixel values in an array as done earlier.

D) Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognises the correct digit.)

  1. We load the image used for testing
  2. process the image as earlier and extract each digit using contour methods
  3. Draw bounding box for it, then resize to 10x10, and store its pixel values in an array as done earlier.
  4. Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognises the correct digit.)

A) It loads the image.

B) Selects the digits ( obviously by contour finding and applying constraints on area and height of letters to avoid false detections).

C) 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 box.

D) Once corresponding digit key is pressed, it resizes this box to 10x10 and saves 100 pixel values in an array (here, samples) and corresponding manually entered digit in another array(here, responses).

E) Then save both the arrays in separate txt files.

A) Load the txt files we already saved earlier

B) create a instance of classifier we are using ( here, it is KNearest)

C) Then we use KNearest.train function to train the data

A) We load the image used for testing

B) process the image as earlier and extract each digit using contour methods

C) Draw bounding box for it, then resize to 10x10, and store its pixel values in an array as done earlier.

D) Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognises the correct digit.)

  1. It loads the image.
  2. Selects the digits ( obviously by contour finding and applying constraints on area and height of letters to avoid false detections).
  3. 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 box.
  4. Once corresponding digit key is pressed, it resizes this box to 10x10 and saves 100 pixel values in an array (here, samples) and corresponding manually entered digit in another array(here, responses).
  5. Then save both the arrays in separate txt files.
  1. Load the txt files we already saved earlier
  2. create a instance of classifier we are using ( here, it is KNearest)
  3. Then we use KNearest.train function to train the data
  1. We load the image used for testing
  2. process the image as earlier and extract each digit using contour methods
  3. Draw bounding box for it, then resize to 10x10, and store its pixel values in an array as done earlier.
  4. Then we use KNearest.find_nearest() function to find the nearest item to the one we gave. ( If lucky, it recognises the correct digit.)
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

And this SOFthis SOF helped me to find it. These 16 features are explained in the paper Letter Recognition Using Holland-Style Adaptive Classifiers. ( Although I didn't understand some of the features at end)

And this SOF helped me to find it. These 16 features are explained in the paper Letter Recognition Using Holland-Style Adaptive Classifiers. ( Although I didn't understand some of the features at end)

And this SOF helped me to find it. These 16 features are explained in the paper Letter Recognition Using Holland-Style Adaptive Classifiers. ( Although I didn't understand some of the features at end)

deleted 10 characters in body
Source Link
Peter O.
  • 33.1k
  • 14
  • 86
  • 97
Loading
Corrected b0rqt speelink: the non-word "seperate" must be "separate"
Source Link
tchrist
  • 80.7k
  • 31
  • 135
  • 186
Loading
import sys
Source Link
Jesse Aldridge
  • 8.2k
  • 10
  • 50
  • 78
Loading
changed the link
Source Link
Abid Rahman K
  • 52.8k
  • 31
  • 149
  • 158
Loading
im3 changed to im in the training and testing script.
Source Link
Legend
  • 117.5k
  • 125
  • 287
  • 409
Loading
Source Link
Abid Rahman K
  • 52.8k
  • 31
  • 149
  • 158
Loading