character_recognition.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * cropped_word_recognition.cpp
  3. *
  4. * A demo program of text recognition in a given cropped word.
  5. * Shows the use of the OCRBeamSearchDecoder class API using the provided default classifier.
  6. *
  7. * Created on: Jul 9, 2015
  8. * Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
  9. */
  10. #include "opencv2/text.hpp"
  11. #include "opencv2/core/utility.hpp"
  12. #include "opencv2/highgui.hpp"
  13. #include "opencv2/imgproc.hpp"
  14. #include <iostream>
  15. using namespace std;
  16. using namespace cv;
  17. using namespace cv::text;
  18. int main(int argc, char* argv[])
  19. {
  20. cout << endl << argv[0] << endl << endl;
  21. cout << "A demo program of Scene Text Character Recognition: " << endl;
  22. cout << "Shows the use of the OCRBeamSearchDecoder::ClassifierCallback class using the Single Layer CNN character classifier described in:" << endl;
  23. cout << "Coates, Adam, et al. \"Text detection and character recognition in scene images with unsupervised feature learning.\" ICDAR 2011." << endl << endl;
  24. Mat image;
  25. if(argc>1)
  26. image = imread(argv[1]);
  27. else
  28. {
  29. cout << " Usage: " << argv[0] << " <input_image>" << endl;
  30. cout << " the input image must contain a single character (e.g. scenetext_char01.jpg)." << endl << endl;
  31. return(0);
  32. }
  33. string vocabulary = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // must have the same order as the classifier output classes
  34. Ptr<OCRHMMDecoder::ClassifierCallback> ocr = loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz");
  35. double t_r = (double)getTickCount();
  36. vector<int> out_classes;
  37. vector<double> out_confidences;
  38. ocr->eval(image, out_classes, out_confidences);
  39. cout << "OCR output = \"" << vocabulary[out_classes[0]] << "\" with confidence "
  40. << out_confidences[0] << ". Evaluated in "
  41. << ((double)getTickCount() - t_r)*1000/getTickFrequency() << " ms." << endl << endl;
  42. return 0;
  43. }