dictnet_demo.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "opencv2/text.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include "opencv2/imgproc.hpp"
  4. #include <sstream>
  5. #include <iostream>
  6. using namespace std;
  7. using namespace cv;
  8. using namespace cv::text;
  9. inline void printHelp()
  10. {
  11. cout << " Demo of wordspotting CNN for text recognition." << endl;
  12. cout << " Max Jaderberg et al.: Reading Text in the Wild with Convolutional Neural Networks, IJCV 2015"<<std::endl<<std::endl;
  13. cout << " Usage: program <input_image>" << endl;
  14. cout << " Caffe Model files (dictnet_vgg.caffemodel, dictnet_vgg_deploy.prototxt, dictnet_vgg_labels.txt)"<<endl;
  15. cout << " must be in the current directory." << endl << endl;
  16. cout << " Obtaining Caffe Model files in linux shell:"<<endl;
  17. cout << " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg.caffemodel"<<endl;
  18. cout << " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_deploy.prototxt"<<endl;
  19. cout << " wget http://nicolaou.homouniversalis.org/assets/vgg_text/dictnet_vgg_labels.txt"<<endl<<endl;
  20. }
  21. int main(int argc, const char * argv[])
  22. {
  23. if (argc != 2)
  24. {
  25. printHelp();
  26. exit(1);
  27. }
  28. Mat image = imread(argv[1], IMREAD_GRAYSCALE);
  29. cout << "Read image (" << argv[1] << "): " << image.size << ", channels: " << image.channels() << ", depth: " << image.depth() << endl;
  30. if (image.empty())
  31. {
  32. printHelp();
  33. exit(1);
  34. }
  35. Ptr<OCRHolisticWordRecognizer> wordSpotter = OCRHolisticWordRecognizer::create("dictnet_vgg_deploy.prototxt", "dictnet_vgg.caffemodel", "dictnet_vgg_labels.txt");
  36. std::string word;
  37. vector<float> confs;
  38. wordSpotter->run(image, word, 0, 0, &confs);
  39. cout << "Detected word: '" << word << "', confidence: " << confs[0] << endl;
  40. }