diff --git a/modules/text/include/opencv2/text/ocr.hpp b/modules/text/include/opencv2/text/ocr.hpp index 47a307c90..6b6ab6e92 100644 --- a/modules/text/include/opencv2/text/ocr.hpp +++ b/modules/text/include/opencv2/text/ocr.hpp @@ -46,7 +46,6 @@ #include #include -using namespace std; namespace cv { @@ -65,29 +64,20 @@ class CV_EXPORTS BaseOCR { public: 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; + virtual void run(Mat& image, std::string& output_text, std::vector* component_rects=NULL, + std::vector* component_texts=NULL, std::vector* component_confidences=NULL, + int component_level=0) = 0; }; class CV_EXPORTS OCRTesseract : public BaseOCR { public: - 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); + virtual void run(Mat& image, std::string& output_text, std::vector* component_rects=NULL, + std::vector* component_texts=NULL, std::vector* component_confidences=NULL, + int component_level=0); + + static Ptr create(const char* datapath=NULL, const char* language=NULL, + const char* char_whitelist=NULL, int oem=3, int psmode=3); }; diff --git a/modules/text/src/ocr_tesseract.cpp b/modules/text/src/ocr_tesseract.cpp index 3cd89407e..ada901653 100644 --- a/modules/text/src/ocr_tesseract.cpp +++ b/modules/text/src/ocr_tesseract.cpp @@ -47,13 +47,28 @@ #include #include #include -using namespace std; namespace cv { namespace text { +using namespace std; + +void OCRTesseract::run(Mat& image, string& output_text, vector* component_rects, + vector* component_texts, vector* component_confidences, + int component_level) +{ + 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(); +} class OCRTesseractImpl : public OCRTesseract { @@ -68,42 +83,42 @@ public: { #ifdef HAVE_TESSERACT - const char *lang = "eng"; - if (language != NULL) - lang = language; - - if (tess.Init(datapath, lang, (tesseract::OcrEngineMode)oemode)) - { - cout << "OCRTesseract: Could not initialize tesseract." << endl; - throw 1; - } + const char *lang = "eng"; + if (language != NULL) + lang = language; + + if (tess.Init(datapath, lang, (tesseract::OcrEngineMode)oemode)) + { + cout << "OCRTesseract: Could not initialize tesseract." << endl; + throw 1; + } - //cout << "OCRTesseract: tesseract version " << tess.Version() << endl; + //cout << "OCRTesseract: tesseract version " << tess.Version() << endl; - tesseract::PageSegMode pagesegmode = (tesseract::PageSegMode)psmode; - tess.SetPageSegMode(pagesegmode); + tesseract::PageSegMode pagesegmode = (tesseract::PageSegMode)psmode; + tess.SetPageSegMode(pagesegmode); - if(char_whitelist != NULL) - tess.SetVariable("tessedit_char_whitelist", char_whitelist); - else - tess.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); + if(char_whitelist != NULL) + tess.SetVariable("tessedit_char_whitelist", char_whitelist); + else + tess.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); - tess.SetVariable("save_best_choices", "T"); + tess.SetVariable("save_best_choices", "T"); #else - cout << "OCRTesseract("<* component_texts=NULL, vector* component_confidences=NULL, int component_level=0) { - - CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); -#ifdef HAVE_TESSERACT - - if (component_texts != 0) - component_texts->clear(); - if (component_rects != 0) - component_rects->clear(); - if (component_confidences != 0) - component_confidences->clear(); + CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); - tess.SetImage((uchar*)image.data, image.size().width, image.size().height, image.channels(), image.step1()); - tess.Recognize(0); - output = string(tess.GetUTF8Text()); +#ifdef HAVE_TESSERACT - if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) ) - { - tesseract::ResultIterator* ri = tess.GetIterator(); - tesseract::PageIteratorLevel level = tesseract::RIL_WORD; - if (component_level == OCR_LEVEL_TEXTLINE) - level = tesseract::RIL_TEXTLINE; - - if (ri != 0) { - do { - const char* word = ri->GetUTF8Text(level); - if (word == NULL) - continue; - float conf = ri->Confidence(level); - int x1, y1, x2, y2; - ri->BoundingBox(level, &x1, &y1, &x2, &y2); - - if (component_texts != 0) - component_texts->push_back(string(word)); - if (component_rects != 0) - component_rects->push_back(Rect(x1,y1,x2-x1,y2-y1)); - if (component_confidences != 0) - component_confidences->push_back(conf); - - delete[] word; - } while (ri->Next(level)); + if (component_texts != 0) + component_texts->clear(); + if (component_rects != 0) + component_rects->clear(); + if (component_confidences != 0) + component_confidences->clear(); + + tess.SetImage((uchar*)image.data, image.size().width, image.size().height, image.channels(), image.step1()); + tess.Recognize(0); + output = string(tess.GetUTF8Text()); + + if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) ) + { + tesseract::ResultIterator* ri = tess.GetIterator(); + tesseract::PageIteratorLevel level = tesseract::RIL_WORD; + if (component_level == OCR_LEVEL_TEXTLINE) + level = tesseract::RIL_TEXTLINE; + + if (ri != 0) { + do { + const char* word = ri->GetUTF8Text(level); + if (word == NULL) + continue; + float conf = ri->Confidence(level); + int x1, y1, x2, y2; + ri->BoundingBox(level, &x1, &y1, &x2, &y2); + + if (component_texts != 0) + component_texts->push_back(string(word)); + if (component_rects != 0) + component_rects->push_back(Rect(x1,y1,x2-x1,y2-y1)); + if (component_confidences != 0) + component_confidences->push_back(conf); + + delete[] word; + } while (ri->Next(level)); + } + delete ri; } - delete ri; - } - tess.Clear(); + tess.Clear(); #else - cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl; - output.clear(); - if(component_rects) - component_rects->clear(); - if(component_texts) - component_texts->clear(); - if(component_confidences) - component_confidences->clear(); + cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl; + output.clear(); + if(component_rects) + component_rects->clear(); + if(component_texts) + 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) +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); + return makePtr(datapath,language,char_whitelist,oem,psmode); }