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 
42 #include <iostream>
43 #include <opencv2/opencv_modules.hpp>
44 
45 #ifdef HAVE_OPENCV_TEXT
46 
47 #include "opencv2/datasets/tr_chars.hpp"
48 #include <opencv2/core.hpp>
49 #include "opencv2/text.hpp"
50 #include "opencv2/imgproc.hpp"
51 #include "opencv2/imgcodecs.hpp"
52 
53 #include <cstdio>
54 #include <cstdlib> // atoi
55 
56 #include <string>
57 #include <vector>
58 
59 using namespace std;
60 using namespace cv;
61 using namespace cv::datasets;
62 using namespace cv::text;
63 
main(int argc,char * argv[])64 int main(int argc, char *argv[])
65 {
66     const char *keys =
67             "{ help h usage ? |    | show this message }"
68             "{ path p         |true| path to dataset description file ( list_English_Img.m ) and Img folder.}";
69     CommandLineParser parser(argc, argv, keys);
70     string path(parser.get<string>("path"));
71     if (parser.has("help") || path=="true")
72     {
73         parser.printMessage();
74         return -1;
75     }
76 
77     Ptr<TR_chars> dataset = TR_chars::create();
78     dataset->load(path);
79 
80     // ***************
81     // dataset. train, test contain information about each element of appropriate sets and splits.
82     // For example, let output first elements of these vectors and their sizes for last split.
83     // And number of splits.
84     int numSplits = dataset->getNumSplits();
85     printf("splits number: %u\n", numSplits);
86 
87     vector< Ptr<Object> > &currTrain = dataset->getTrain(numSplits-1);
88     vector< Ptr<Object> > &currTest = dataset->getTest(numSplits-1);
89     vector< Ptr<Object> > &currValidation = dataset->getValidation(numSplits-1);
90     printf("train size: %u\n", (unsigned int)currTrain.size());
91     printf("test size: %u\n", (unsigned int)currTest.size());
92     printf("validation size: %u\n", (unsigned int)currValidation.size());
93 
94 
95     // WARNING: The order of classes' labels is different in Chars74k and in the output of our classifier
96     string src_classes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // labels order as in the clasifier output
97     string tar_classes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; // labels order as in the Chars74k dataset
98 
99     Ptr<OCRHMMDecoder::ClassifierCallback> ocr = loadOCRHMMClassifierCNN("OCRBeamSearch_CNN_model_data.xml.gz");
100 
101     int numOK = 0;
102     int upperNumOK = 0;
103 
104     for (unsigned int i=0; i<(unsigned int)currTest.size(); i++)
105     {
106         TR_charsObj *exampleTest = static_cast<TR_charsObj *>(currTest[i].get());
107         printf("processed image: %u, name: %s\n", i, exampleTest->imgName.c_str());
108         printf("  label: %u,", exampleTest->label);
109 
110         string imfilename = path+string("/Img/")+exampleTest->imgName.c_str()+string(".png");
111         Mat image  = imread(imfilename);
112         vector<int> out_classes;
113         vector<double> out_confidences;
114         ocr->eval(image, out_classes, out_confidences);
115         int prediction = 1 + tar_classes.find_first_of(src_classes[out_classes[0]]);
116         printf(" prediction: %u\n", prediction);
117 
118         if (exampleTest->label == prediction)
119             numOK++;
120 
121         char l = tar_classes[exampleTest->label];
122         char p = tar_classes[prediction];
123         if (toupper(l) == toupper(p))
124             upperNumOK++;
125     }
126 
127     printf("\n---------------------------------------------\n");
128     printf("Chars74k Classification Accuracy (case-sensitive): %f\n",(float)numOK/currTest.size());
129     printf("Chars74k Classification Accuracy (case-insensitive): %f\n",(float)upperNumOK/currTest.size());
130 
131     return 0;
132 }
133 
134 #else
135 
main()136 int main()
137 {
138     std::cerr << "OpenCV was built without text module" << std::endl;
139     return 0;
140 }
141 
142 #endif // HAVE_OPENCV_TEXT
143