tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
Getting Started

Example code taken from the Tesseract's github wiki page.

See also
https://github.com/tesseract-ocr/tesseract/wiki/APIExample

Basic example

#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
char *outText;
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng"))
{
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
// Open input image with leptonica library
Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->SetImage(image);
// Get OCR result
outText = api->GetUTF8Text();
printf("OCR output:\n%s", outText);
// Destroy used object and release memory
api->End();
delete [] outText;
pixDestroy(&image);
return 0;
}

GetComponentImages example

Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->Init(NULL, "eng");
api->SetImage(image);
Boxa* boxes = api->GetComponentImages(tesseract::RIL_TEXTLINE, true, NULL, NULL);
printf("Found %d textline image components.\n", boxes->n);
for (int i = 0; i < boxes->n; i++)
{
BOX* box = boxaGetBox(boxes, i, L_CLONE);
api->SetRectangle(box->x, box->y, box->w, box->h);
char* ocrResult = api->GetUTF8Text();
int conf = api->MeanTextConf();
fprintf(stdout, "Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s", i, box->x, box->y, box->w, box->h, conf, ocrResult);
}

Result iterator example

It is possible to get confidence value and BoundingBox per word from a ResultIterator:

Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->Init(NULL, "eng");
api->SetImage(image);
api->Recognize(0);
if (ri != 0)
{
do
{
const char* word = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
int x1, y1, x2, y2;
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
printf("word: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n", word, conf, x1, y1, x2, y2);
delete [] word;
} while (ri->Next(level));
}

Orientation and script detection (OSD) example

const char* inputfile = "/usr/src/tesseract/testing/eurotext.tif";
float deskew_angle;
PIX *image = pixRead(inputfile);
api->Init("/usr/src/tesseract/", "eng");
api->SetImage(image);
api->Recognize(0);
it->Orientation(&orientation, &direction, &order, &deskew_angle);
printf(
"Orientation: %d;\n"
"WritingDirection: %d\n"
"TextlineOrder: %d\n"
"Deskew angle: %.4f\n",
orientation, direction, order, deskew_angle);

Example of iterator over the classifier choices for a single symbol

Pix *image = pixRead("/usr/src/tesseract/testing/phototest.tif");
api->Init(NULL, "eng");
api->SetImage(image);
api->SetVariable("save_blob_choices", "T");
api->SetRectangle(37, 228, 548, 31);
api->Recognize(NULL);
if (ri != 0)
{
do
{
const char* symbol = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
if (symbol != 0)
{
printf("symbol %s, conf: %f", symbol, conf);
bool indent = false;
do
{
if (indent) printf("\t\t ");
printf("\t- ");
const char* choice = ci.GetUTF8Text();
printf("%s conf: %f\n", choice, ci.Confidence());
indent = true;
} while(ci.Next());
}
printf("---------------------------------------------\n");
delete [] symbol;
} while((ri->Next(level)));
}