diff --git a/modules/text/include/opencv2/text/ocr.hpp b/modules/text/include/opencv2/text/ocr.hpp index f1c593116..47a307c90 100644 --- a/modules/text/include/opencv2/text/ocr.hpp +++ b/modules/text/include/opencv2/text/ocr.hpp @@ -44,24 +44,15 @@ #ifndef __OPENCV_TEXT_OCR_HPP__ #define __OPENCV_TEXT_OCR_HPP__ -#include "text_config.hpp" - -#ifdef HAVE_TESSERACT -#include -#include -#endif - -#include "opencv2/core.hpp" #include #include - +using namespace std; namespace cv { namespace text { -using namespace std; enum { @@ -69,40 +60,35 @@ enum OCR_LEVEL_TEXTLINE }; -#ifdef HAVE_TESSERACT -class CV_EXPORTS OCRTesseract +//base class BaseOCR declares a common API that would be used in a typical text recognition scenario +class CV_EXPORTS BaseOCR { -private: - tesseract::TessBaseAPI tess; - public: - //Default constructor - OCRTesseract(const char* datapath=NULL, const char* language=NULL, const char* char_whitelist=NULL, - tesseract::OcrEngineMode oem=tesseract::OEM_DEFAULT, tesseract::PageSegMode psmode=tesseract::PSM_AUTO); - - ~OCRTesseract(); - - void run(Mat& image, string& output_text, vector* component_rects=NULL, - vector* component_texts=NULL, vector* component_confidences=NULL, - int component_level=0); + virtual ~BaseOCR() {}; + virtual void run(Mat& image, string& output_text, vector* component_rects=NULL, + vector* component_texts=NULL, vector* component_confidences=NULL, + int component_level=0) = 0; }; -#else -//stub -class CV_EXPORTS OCRTesseract + +class CV_EXPORTS OCRTesseract : public BaseOCR { public: - //Default constructor - OCRTesseract(const char* datapath=NULL, const char* language=NULL, const char* char_whitelist=NULL, - int oem=0, int psmode=0); - - ~OCRTesseract(); - - void run(Mat& image, string& output_text, vector* component_rects=NULL, - vector* component_texts=NULL, vector* component_confidences=NULL, - int component_level=0); + virtual void run(Mat& image, string& output_text, vector* component_rects=NULL, + vector* component_texts=NULL, vector* component_confidences=NULL, + int component_level=0) + { + CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); + CV_Assert( (component_level == OCR_LEVEL_TEXTLINE) || (component_level == OCR_LEVEL_WORD) ); + output_text.clear(); + if (component_rects != NULL) + component_rects->clear(); + if (component_texts != NULL) + component_texts->clear(); + if (component_confidences != NULL) + component_confidences->clear(); + } + static Ptr create(const char* datapath=NULL, const char* language=NULL, const char* char_whitelist=NULL, int oem=3, int psmode=3); }; -#endif - } diff --git a/modules/text/samples/end_to_end_recognition.cpp b/modules/text/samples/end_to_end_recognition.cpp index 153e5391a..c69d78bed 100644 --- a/modules/text/samples/end_to_end_recognition.cpp +++ b/modules/text/samples/end_to_end_recognition.cpp @@ -102,7 +102,7 @@ int main(int argc, char* argv[]) /*Text Recognition (OCR)*/ double t_r = (double)getTickCount(); - OCRTesseract* ocr = new OCRTesseract(); + Ptr ocr = OCRTesseract::create(); cout << "TIME_OCR_INITIALIZATION = " << ((double)getTickCount() - t_r)*1000/getTickFrequency() << endl; string output; diff --git a/modules/text/samples/webcam_demo.cpp b/modules/text/samples/webcam_demo.cpp index d7e84bbc8..7dd9c0e7f 100644 --- a/modules/text/samples/webcam_demo.cpp +++ b/modules/text/samples/webcam_demo.cpp @@ -54,12 +54,12 @@ private: vector< vector > &boxes; vector< vector > &words; vector< vector > &confidences; - vector< OCRTesseract* > &ocrs; + vector< Ptr > &ocrs; public: Parallel_OCR(vector &_detections, vector &_outputs, vector< vector > &_boxes, vector< vector > &_words, vector< vector > &_confidences, - vector< OCRTesseract* > &_ocrs) + vector< Ptr > &_ocrs) : detections(_detections), outputs(_outputs), boxes(_boxes), words(_words), confidences(_confidences), ocrs(_ocrs) {} @@ -120,11 +120,10 @@ int main(int argc, char* argv[]) //Initialize OCR engine (we initialize 10 instances in order to work several recognitions in parallel) int num_ocrs = 10; - vector ocrs; + vector< Ptr > ocrs; for (int o=0; o* component_rects=NULL, + vector* component_texts=NULL, vector* component_confidences=NULL, + int component_level=0) + { + + CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); + +#ifdef HAVE_TESSERACT -void OCRTesseract::run(Mat& image, string& output, vector* component_rects, - vector* component_texts, vector* component_confidences, int component_level) -{ - CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); if (component_texts != 0) component_texts->clear(); if (component_rects != 0) @@ -135,30 +157,8 @@ void OCRTesseract::run(Mat& image, string& output, vector* component_rects } tess.Clear(); -} -#else -//Stub constructor -OCRTesseract::OCRTesseract(const char* datapath, const char* language, const char* char_whitelist, int oemode, int psmode) -{ - cout << "OCRTesseract("<* component_rects, - vector* component_texts, vector* component_confidences, int component_level) -{ - CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); +#else cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl; output.clear(); @@ -168,9 +168,16 @@ void OCRTesseract::run(Mat& image, string& output, vector* component_rects component_texts->clear(); if(component_confidences) component_confidences->clear(); -} #endif + } + +}; + +Ptr OCRTesseract::create(const char* datapath, const char* language, const char* char_whitelist, int oem, int psmode) +{ + return makePtr(datapath,language,char_whitelist,oem,psmode); +} } diff --git a/modules/text/src/precomp.hpp b/modules/text/src/precomp.hpp index 69038cffa..94f05d8cc 100644 --- a/modules/text/src/precomp.hpp +++ b/modules/text/src/precomp.hpp @@ -45,4 +45,11 @@ #include "opencv2/text.hpp" +#include "text_config.hpp" + +#ifdef HAVE_TESSERACT +#include +#include +#endif + #endif