cropped_word_recognition.cpp 3.02 KB
Newer Older
1
/*
2
 * cropped_word_recognition.cpp
3
 *
4 5
 * A demo program of text recognition in a given cropped word.
 * Shows the use of the OCRBeamSearchDecoder class API using the provided default classifier.
6
 *
7
 * Created on: Jul 9, 2015
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *     Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
 */

#include "opencv2/text.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"

#include <iostream>

using namespace std;
using namespace cv;
using namespace cv::text;

int main(int argc, char* argv[])
{
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38
    cout << endl << argv[0] << endl << endl;
    cout << "A demo program of Scene Text cropped word Recognition: " << endl;
    cout << "Shows the use of the OCRBeamSearchDecoder class using the Single Layer CNN character classifier described in:" << endl;
    cout << "Coates, Adam, et al. \"Text detection and character recognition in scene images with unsupervised feature learning.\" ICDAR 2011." << endl << endl;

    Mat image;
    if(argc>1)
        image  = imread(argv[1]);
    else
    {
        cout << "    Usage: " << argv[0] << " <input_image>" << endl << endl;
        return(0);
    }

39
    string vocabulary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // must have the same order as the clasifier output classes
40 41
    vector<string> lexicon;  // a list of words expected to be found on the input image
    lexicon.push_back(string("abb"));
42
    lexicon.push_back(string("riser"));
43 44 45 46 47
    lexicon.push_back(string("CHINA"));
    lexicon.push_back(string("HERE"));
    lexicon.push_back(string("President"));
    lexicon.push_back(string("smash"));
    lexicon.push_back(string("KUALA"));
48
    lexicon.push_back(string("Produkt"));
49 50 51
    lexicon.push_back(string("NINTENDO"));

    // Create tailored language model a small given lexicon
52
    Mat transition_p;
53 54 55 56 57
    createOCRHMMTransitionsTable(vocabulary,lexicon,transition_p);

    // An alternative would be to load the default generic language model
    //    (created from ispell 42869 english words list)
    /*Mat transition_p;
58
    string filename = "OCRHMM_transitions_table.xml";
59 60
    FileStorage fs(filename, FileStorage::READ);
    fs["transition_probabilities"] >> transition_p;
61 62
    fs.release();*/

63 64
    Mat emission_p = Mat::eye(62,62,CV_64FC1);

65 66
    // Notice we set here a beam size of 50. This is much faster than using the default value (500).
    // 50 works well with our tiny lexicon example, but may not with larger dictionaries.
67 68
    Ptr<OCRBeamSearchDecoder> ocr = OCRBeamSearchDecoder::create(
                loadOCRBeamSearchClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz"),
69
                vocabulary, transition_p, emission_p, OCR_DECODER_VITERBI, 50);
70 71 72 73 74 75 76 77 78

    double t_r = (double)getTickCount();
    string output;

    vector<Rect>   boxes;
    vector<string> words;
    vector<float>  confidences;
    ocr->run(image, output, &boxes, &words, &confidences, OCR_LEVEL_WORD);

lluis's avatar
lluis committed
79
    cout << "OCR output = \"" << output << "\". Decoded in "
80 81 82 83
         << ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl << endl;

    return 0;
}