Ask Your Question
0

How to train OCR to recognize characters?

asked 2013-09-28 22:37:05 -0500

mgautam gravatar image

updated 2013-09-29 07:27:05 -0500

berak gravatar image

I want too know how can I modify the following code to train OCR to recognize 26 characters instead of 20 characters and storing that information in OCR.xml file.

Extra characters that are now included are: A,E,I,O,U,Q.

Also what are TrainingDataF5, TrainingDataF10, TrainingDataF15 and TrainingDataF20? What is their use? Do they work on same character image size of 20x20?

#include <cv.h>
#include <highgui.h>
#include <cvaux.h>

#include "OCR.h"

#include <iostream>
#include <vector>

using namespace std;
using namespace cv;

const int numFilesChars[]={30, 30, 30, 30,30,30, 35, 40, 42, 41, 42, 33, 30, 31, 49, 44, 30, 24, 21, 20, 34, 9, 10, 3, 11, 3, 15, 4, 9, 12, 10, 21, 18, 8, 15, 7};

int main ( int argc, char** argv )
{
    cout << "OpenCV Training OCR Automatic Number Plate Recognition\n";
    cout << "\n";

    char* path;

    //Check if user specify image to process
    if(argc >= 1 )
    {
        path= argv[1];

    }else{
        cout << "Usage:\n" << argv[0] << " <path to chars folders files> \n";
        return 0;
    }        

    Mat classes;
    Mat trainingDataf5;
    Mat trainingDataf10;
    Mat trainingDataf15;
    Mat trainingDataf20;

    vector<int> trainingLabels;
    OCR ocr;

    for(int i=0; i< OCR::numCharacters; i++)
    {
        int numFiles=numFilesChars[i];
        for(int j=0; j< numFiles; j++){
            cout << "Character "<< OCR::strCharacters[i] << " file: " << j << "\n";
            stringstream ss(stringstream::in | stringstream::out);
            ss << path << OCR::strCharacters[i] << "/" << j << ".jpg";
            Mat img=imread(ss.str(), 0);
            Mat f5=ocr.features(img, 5);
            Mat f10=ocr.features(img, 10);
            Mat f15=ocr.features(img, 15);
            Mat f20=ocr.features(img, 20);

            trainingDataf5.push_back(f5);
            trainingDataf10.push_back(f10);
            trainingDataf15.push_back(f15);
            trainingDataf20.push_back(f20);
            trainingLabels.push_back(i);
        }
    }


    trainingDataf5.convertTo(trainingDataf5, CV_32FC1);
    trainingDataf10.convertTo(trainingDataf10, CV_32FC1);
    trainingDataf15.convertTo(trainingDataf15, CV_32FC1);
    trainingDataf20.convertTo(trainingDataf20, CV_32FC1);
    Mat(trainingLabels).copyTo(classes);

    FileStorage fs("OCR.xml", FileStorage::WRITE);
    fs << "TrainingDataF5" << trainingDataf5;
    fs << "TrainingDataF10" << trainingDataf10;
    fs << "TrainingDataF15" << trainingDataf15;
    fs << "TrainingDataF20" << trainingDataf20;
    fs << "classes" << classes;
    fs.release();

    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-29 05:05:36 -0500

FLY gravatar image

updated 2013-09-29 05:06:19 -0500

Read the book Packtpub.Mastering.OpenCV.with.Practical.Computer.Vision.Projects. its well explained in that book , add the number of the alphabet in numFilesChars[] and alphabet in the strings and save it ocr.xml

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-28 22:37:05 -0500

Seen: 2,037 times

Last updated: Sep 29 '13