Commit 493785a7 authored by lluis's avatar lluis

moves implementation to cpp file and removes using directive from header file

parent 3ab63082
...@@ -46,7 +46,6 @@ ...@@ -46,7 +46,6 @@
#include <vector> #include <vector>
#include <string> #include <string>
using namespace std;
namespace cv namespace cv
{ {
...@@ -65,29 +64,20 @@ class CV_EXPORTS BaseOCR ...@@ -65,29 +64,20 @@ class CV_EXPORTS BaseOCR
{ {
public: public:
virtual ~BaseOCR() {}; virtual ~BaseOCR() {};
virtual void run(Mat& image, string& output_text, vector<Rect>* component_rects=NULL, virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL, std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) = 0; int component_level=0) = 0;
}; };
class CV_EXPORTS OCRTesseract : public BaseOCR class CV_EXPORTS OCRTesseract : public BaseOCR
{ {
public: public:
virtual void run(Mat& image, string& output_text, vector<Rect>* component_rects=NULL, virtual void run(Mat& image, std::string& output_text, std::vector<Rect>* component_rects=NULL,
vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL, std::vector<std::string>* component_texts=NULL, std::vector<float>* component_confidences=NULL,
int component_level=0) int component_level=0);
{
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) ); static Ptr<OCRTesseract> create(const char* datapath=NULL, const char* language=NULL,
CV_Assert( (component_level == OCR_LEVEL_TEXTLINE) || (component_level == OCR_LEVEL_WORD) ); const char* char_whitelist=NULL, int oem=3, int psmode=3);
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<OCRTesseract> create(const char* datapath=NULL, const char* language=NULL, const char* char_whitelist=NULL, int oem=3, int psmode=3);
}; };
......
...@@ -47,13 +47,28 @@ ...@@ -47,13 +47,28 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <queue> #include <queue>
using namespace std;
namespace cv namespace cv
{ {
namespace text namespace text
{ {
using namespace std;
void OCRTesseract::run(Mat& image, string& output_text, vector<Rect>* component_rects,
vector<string>* component_texts, vector<float>* 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 class OCRTesseractImpl : public OCRTesseract
{ {
...@@ -68,42 +83,42 @@ public: ...@@ -68,42 +83,42 @@ public:
{ {
#ifdef HAVE_TESSERACT #ifdef HAVE_TESSERACT
const char *lang = "eng"; const char *lang = "eng";
if (language != NULL) if (language != NULL)
lang = language; lang = language;
if (tess.Init(datapath, lang, (tesseract::OcrEngineMode)oemode)) if (tess.Init(datapath, lang, (tesseract::OcrEngineMode)oemode))
{ {
cout << "OCRTesseract: Could not initialize tesseract." << endl; cout << "OCRTesseract: Could not initialize tesseract." << endl;
throw 1; throw 1;
} }
//cout << "OCRTesseract: tesseract version " << tess.Version() << endl; //cout << "OCRTesseract: tesseract version " << tess.Version() << endl;
tesseract::PageSegMode pagesegmode = (tesseract::PageSegMode)psmode; tesseract::PageSegMode pagesegmode = (tesseract::PageSegMode)psmode;
tess.SetPageSegMode(pagesegmode); tess.SetPageSegMode(pagesegmode);
if(char_whitelist != NULL) if(char_whitelist != NULL)
tess.SetVariable("tessedit_char_whitelist", char_whitelist); tess.SetVariable("tessedit_char_whitelist", char_whitelist);
else else
tess.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); tess.SetVariable("tessedit_char_whitelist", "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
tess.SetVariable("save_best_choices", "T"); tess.SetVariable("save_best_choices", "T");
#else #else
cout << "OCRTesseract("<<oemode<<psmode<<"): Tesseract not found." << endl; cout << "OCRTesseract("<<oemode<<psmode<<"): Tesseract not found." << endl;
if (datapath != NULL) if (datapath != NULL)
cout << " " << datapath << endl; cout << " " << datapath << endl;
if (language != NULL) if (language != NULL)
cout << " " << language << endl; cout << " " << language << endl;
if (char_whitelist != NULL) if (char_whitelist != NULL)
cout << " " << char_whitelist << endl; cout << " " << char_whitelist << endl;
#endif #endif
} }
~OCRTesseractImpl() ~OCRTesseractImpl()
{ {
#ifdef HAVE_TESSERACT #ifdef HAVE_TESSERACT
tess.End(); tess.End();
#endif #endif
} }
...@@ -111,72 +126,73 @@ public: ...@@ -111,72 +126,73 @@ public:
vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL, vector<string>* component_texts=NULL, vector<float>* component_confidences=NULL,
int component_level=0) int component_level=0)
{ {
CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) );
#ifdef HAVE_TESSERACT CV_Assert( (image.type() == CV_8UC1) || (image.type() == CV_8UC1) );
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()); #ifdef HAVE_TESSERACT
tess.Recognize(0);
output = string(tess.GetUTF8Text());
if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) ) if (component_texts != 0)
{ component_texts->clear();
tesseract::ResultIterator* ri = tess.GetIterator(); if (component_rects != 0)
tesseract::PageIteratorLevel level = tesseract::RIL_WORD; component_rects->clear();
if (component_level == OCR_LEVEL_TEXTLINE) if (component_confidences != 0)
level = tesseract::RIL_TEXTLINE; component_confidences->clear();
if (ri != 0) { tess.SetImage((uchar*)image.data, image.size().width, image.size().height, image.channels(), image.step1());
do { tess.Recognize(0);
const char* word = ri->GetUTF8Text(level); output = string(tess.GetUTF8Text());
if (word == NULL)
continue; if ( (component_rects != NULL) || (component_texts != NULL) || (component_confidences != NULL) )
float conf = ri->Confidence(level); {
int x1, y1, x2, y2; tesseract::ResultIterator* ri = tess.GetIterator();
ri->BoundingBox(level, &x1, &y1, &x2, &y2); tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
if (component_level == OCR_LEVEL_TEXTLINE)
if (component_texts != 0) level = tesseract::RIL_TEXTLINE;
component_texts->push_back(string(word));
if (component_rects != 0) if (ri != 0) {
component_rects->push_back(Rect(x1,y1,x2-x1,y2-y1)); do {
if (component_confidences != 0) const char* word = ri->GetUTF8Text(level);
component_confidences->push_back(conf); if (word == NULL)
continue;
delete[] word; float conf = ri->Confidence(level);
} while (ri->Next(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 #else
cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl; cout << "OCRTesseract(" << component_level << image.type() <<"): Tesseract not found." << endl;
output.clear(); output.clear();
if(component_rects) if(component_rects)
component_rects->clear(); component_rects->clear();
if(component_texts) if(component_texts)
component_texts->clear(); component_texts->clear();
if(component_confidences) if(component_confidences)
component_confidences->clear(); component_confidences->clear();
#endif #endif
} }
}; };
Ptr<OCRTesseract> OCRTesseract::create(const char* datapath, const char* language, const char* char_whitelist, int oem, int psmode) Ptr<OCRTesseract> OCRTesseract::create(const char* datapath, const char* language,
const char* char_whitelist, int oem, int psmode)
{ {
return makePtr<OCRTesseractImpl>(datapath,language,char_whitelist,oem,psmode); return makePtr<OCRTesseractImpl>(datapath,language,char_whitelist,oem,psmode);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment