tr_chars_benchmark.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2014, Itseez Inc, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Itseez Inc or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include <iostream>
  42. #include <opencv2/opencv_modules.hpp>
  43. #ifdef HAVE_OPENCV_TEXT
  44. #include "opencv2/datasets/tr_chars.hpp"
  45. #include <opencv2/core.hpp>
  46. #include "opencv2/text.hpp"
  47. #include "opencv2/imgproc.hpp"
  48. #include "opencv2/imgcodecs.hpp"
  49. #include <cstdio>
  50. #include <cstdlib> // atoi
  51. #include <string>
  52. #include <vector>
  53. using namespace std;
  54. using namespace cv;
  55. using namespace cv::datasets;
  56. using namespace cv::text;
  57. int main(int argc, char *argv[])
  58. {
  59. const char *keys =
  60. "{ help h usage ? | | show this message }"
  61. "{ path p |true| path to dataset description file ( list_English_Img.m ) and Img folder.}";
  62. CommandLineParser parser(argc, argv, keys);
  63. string path(parser.get<string>("path"));
  64. if (parser.has("help") || path=="true")
  65. {
  66. parser.printMessage();
  67. return -1;
  68. }
  69. Ptr<TR_chars> dataset = TR_chars::create();
  70. dataset->load(path);
  71. // ***************
  72. // dataset. train, test contain information about each element of appropriate sets and splits.
  73. // For example, let output first elements of these vectors and their sizes for last split.
  74. // And number of splits.
  75. int numSplits = dataset->getNumSplits();
  76. printf("splits number: %u\n", numSplits);
  77. vector< Ptr<Object> > &currTrain = dataset->getTrain(numSplits-1);
  78. vector< Ptr<Object> > &currTest = dataset->getTest(numSplits-1);
  79. vector< Ptr<Object> > &currValidation = dataset->getValidation(numSplits-1);
  80. printf("train size: %u\n", (unsigned int)currTrain.size());
  81. printf("test size: %u\n", (unsigned int)currTest.size());
  82. printf("validation size: %u\n", (unsigned int)currValidation.size());
  83. // WARNING: The order of classes' labels is different in Chars74k and in the output of our classifier
  84. string src_classes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // labels order as in the clasifier output
  85. string tar_classes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // labels order as in the Chars74k dataset
  86. Ptr<OCRHMMDecoder::ClassifierCallback> ocr = loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz");
  87. int numOK = 0;
  88. int upperNumOK = 0;
  89. for (unsigned int i=0; i<(unsigned int)currTest.size(); i++)
  90. {
  91. TR_charsObj *exampleTest = static_cast<TR_charsObj *>(currTest[i].get());
  92. printf("processed image: %u, name: %s\n", i, exampleTest->imgName.c_str());
  93. printf(" label: %u,", exampleTest->label);
  94. string imfilename = path+string("/Img/")+exampleTest->imgName.c_str()+string(".png");
  95. Mat image = imread(imfilename);
  96. vector<int> out_classes;
  97. vector<double> out_confidences;
  98. ocr->eval(image, out_classes, out_confidences);
  99. int prediction = 1 + tar_classes.find_first_of(src_classes[out_classes[0]]);
  100. printf(" prediction: %u\n", prediction);
  101. if (exampleTest->label == prediction)
  102. numOK++;
  103. char l = tar_classes[exampleTest->label];
  104. char p = tar_classes[prediction];
  105. if (toupper(l) == toupper(p))
  106. upperNumOK++;
  107. }
  108. printf("\n---------------------------------------------\n");
  109. printf("Chars74k Classification Accuracy (case-sensitive): %f\n",(float)numOK/currTest.size());
  110. printf("Chars74k Classification Accuracy (case-insensitive): %f\n",(float)upperNumOK/currTest.size());
  111. return 0;
  112. }
  113. #else
  114. int main()
  115. {
  116. std::cerr << "OpenCV was built without text module" << std::endl;
  117. return 0;
  118. }
  119. #endif // HAVE_OPENCV_TEXT