Wrapped OpenALPR library in "alpr" namespace. Resolves issue #60.

This commit is contained in:
Matt Hill
2014-10-27 20:12:57 -04:00
parent 83ed86c6b4
commit 85f52a6b8c
79 changed files with 7234 additions and 6968 deletions

View File

@@ -23,112 +23,117 @@ using namespace std;
using namespace cv;
using namespace tesseract;
OCR::OCR(Config* config)
: postProcessor(config)
namespace alpr
{
const string EXPECTED_TESSERACT_VERSION = "3.03";
this->config = config;
if (startsWith(tesseract.Version(), EXPECTED_TESSERACT_VERSION) == false)
OCR::OCR(Config* config)
: postProcessor(config)
{
std::cerr << "Warning: You are running an unsupported version of Tesseract." << endl;
std::cerr << "Expecting version " << EXPECTED_TESSERACT_VERSION << ", your version is: " << tesseract.Version() << endl;
}
// Tesseract requires the prefix directory to be set as an env variable
tesseract.Init(config->getTessdataPrefix().c_str(), config->ocrLanguage.c_str() );
tesseract.SetVariable("save_blob_choices", "T");
tesseract.SetPageSegMode(PSM_SINGLE_CHAR);
}
const string EXPECTED_TESSERACT_VERSION = "3.03";
OCR::~OCR()
{
tesseract.Clear();
}
this->config = config;
void OCR::performOCR(PipelineData* pipeline_data)
{
timespec startTime;
getTime(&startTime);
postProcessor.clear();
// Don't waste time on OCR processing if it is impossible to get sufficient characters
if (pipeline_data->charRegions.size() < config->postProcessMinCharacters)
return;
for (uint i = 0; i < pipeline_data->thresholds.size(); i++)
{
// Make it black text on white background
bitwise_not(pipeline_data->thresholds[i], pipeline_data->thresholds[i]);
tesseract.SetImage((uchar*) pipeline_data->thresholds[i].data,
pipeline_data->thresholds[i].size().width, pipeline_data->thresholds[i].size().height,
pipeline_data->thresholds[i].channels(), pipeline_data->thresholds[i].step1());
for (uint j = 0; j < pipeline_data->charRegions.size(); j++)
if (startsWith(tesseract.Version(), EXPECTED_TESSERACT_VERSION) == false)
{
Rect expandedRegion = expandRect( pipeline_data->charRegions[j], 2, 2, pipeline_data->thresholds[i].cols, pipeline_data->thresholds[i].rows) ;
std::cerr << "Warning: You are running an unsupported version of Tesseract." << endl;
std::cerr << "Expecting version " << EXPECTED_TESSERACT_VERSION << ", your version is: " << tesseract.Version() << endl;
}
tesseract.SetRectangle(expandedRegion.x, expandedRegion.y, expandedRegion.width, expandedRegion.height);
tesseract.Recognize(NULL);
// Tesseract requires the prefix directory to be set as an env variable
tesseract.Init(config->getTessdataPrefix().c_str(), config->ocrLanguage.c_str() );
tesseract.SetVariable("save_blob_choices", "T");
tesseract.SetPageSegMode(PSM_SINGLE_CHAR);
}
tesseract::ResultIterator* ri = tesseract.GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
do
OCR::~OCR()
{
tesseract.Clear();
}
void OCR::performOCR(PipelineData* pipeline_data)
{
timespec startTime;
getTime(&startTime);
postProcessor.clear();
// Don't waste time on OCR processing if it is impossible to get sufficient characters
if (pipeline_data->charRegions.size() < config->postProcessMinCharacters)
return;
for (uint i = 0; i < pipeline_data->thresholds.size(); i++)
{
// Make it black text on white background
bitwise_not(pipeline_data->thresholds[i], pipeline_data->thresholds[i]);
tesseract.SetImage((uchar*) pipeline_data->thresholds[i].data,
pipeline_data->thresholds[i].size().width, pipeline_data->thresholds[i].size().height,
pipeline_data->thresholds[i].channels(), pipeline_data->thresholds[i].step1());
for (uint j = 0; j < pipeline_data->charRegions.size(); j++)
{
const char* symbol = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
Rect expandedRegion = expandRect( pipeline_data->charRegions[j], 2, 2, pipeline_data->thresholds[i].cols, pipeline_data->thresholds[i].rows) ;
bool dontcare;
int fontindex = 0;
int pointsize = 0;
const char* fontName = ri->WordFontAttributes(&dontcare, &dontcare, &dontcare, &dontcare, &dontcare, &dontcare, &pointsize, &fontindex);
tesseract.SetRectangle(expandedRegion.x, expandedRegion.y, expandedRegion.width, expandedRegion.height);
tesseract.Recognize(NULL);
if(symbol != 0 && pointsize >= config->ocrMinFontSize)
tesseract::ResultIterator* ri = tesseract.GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_SYMBOL;
do
{
postProcessor.addLetter(string(symbol), j, conf);
const char* symbol = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
bool dontcare;
int fontindex = 0;
int pointsize = 0;
const char* fontName = ri->WordFontAttributes(&dontcare, &dontcare, &dontcare, &dontcare, &dontcare, &dontcare, &pointsize, &fontindex);
if(symbol != 0 && pointsize >= config->ocrMinFontSize)
{
postProcessor.addLetter(string(symbol), j, conf);
if (this->config->debugOcr)
printf("charpos%d: threshold %d: symbol %s, conf: %f font: %s (index %d) size %dpx", j, i, symbol, conf, fontName, fontindex, pointsize);
bool indent = false;
tesseract::ChoiceIterator ci(*ri);
do
{
const char* choice = ci.GetUTF8Text();
postProcessor.addLetter(string(choice), j, ci.Confidence());
//letterScores.addScore(*choice, j, ci.Confidence() - MIN_CONFIDENCE);
if (this->config->debugOcr)
{
if (indent) printf("\t\t ");
printf("\t- ");
printf("%s conf: %f\n", choice, ci.Confidence());
}
indent = true;
}
while(ci.Next());
}
if (this->config->debugOcr)
printf("charpos%d: threshold %d: symbol %s, conf: %f font: %s (index %d) size %dpx", j, i, symbol, conf, fontName, fontindex, pointsize);
printf("---------------------------------------------\n");
bool indent = false;
tesseract::ChoiceIterator ci(*ri);
do
{
const char* choice = ci.GetUTF8Text();
postProcessor.addLetter(string(choice), j, ci.Confidence());
//letterScores.addScore(*choice, j, ci.Confidence() - MIN_CONFIDENCE);
if (this->config->debugOcr)
{
if (indent) printf("\t\t ");
printf("\t- ");
printf("%s conf: %f\n", choice, ci.Confidence());
}
indent = true;
}
while(ci.Next());
delete[] symbol;
}
while((ri->Next(level)));
if (this->config->debugOcr)
printf("---------------------------------------------\n");
delete[] symbol;
delete ri;
}
while((ri->Next(level)));
}
delete ri;
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
cout << "OCR Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
}
if (config->debugTiming)
{
timespec endTime;
getTime(&endTime);
cout << "OCR Time: " << diffclock(startTime, endTime) << "ms." << endl;
}
}
}