#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// 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("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 api;
delete [] outText;
pixDestroy(&image);
return 0;
}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// 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("phototest.tif");
api->SetImage(image);
// Restrict recognition to a sub-rectangle of the image
// SetRectangle(left, top, width, height)
api->SetRectangle(30, 86, 590, 100);
// Get OCR result
outText = api->GetUTF8Text();
printf("OCR output:\n%s", outText);
// Destroy used object and release memory
api->End();
delete api;
delete [] outText;
pixDestroy(&image);
return 0;
}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
Pix *image = pixRead("phototest.tif");
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);
boxDestroy(&box);
}
// Destroy used object and release memory
api->End();
delete api;
delete [] outText;
pixDestroy(&image);
return 0;
}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
Pix *image = pixRead("phototest.tif");
api->SetImage(image);
api->Recognize(0);
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
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));
}
// Destroy used object and release memory
api->End();
delete api;
delete [] outText;
pixDestroy(&image);
return 0;
}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
const char* inputfile = "devatest-rotated-270.png";
PIX *image = pixRead(inputfile);
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init(NULL, "osd");
api->SetPageSegMode(tesseract::PSM_OSD_ONLY);
api->SetImage(image);
int orient_deg;
float orient_conf;
const char* script_name;
float script_conf;
api->DetectOrientationScript(&orient_deg, &orient_conf, &script_name, &script_conf);
printf("************\n Orientation in degrees: %d\n Orientation confidence: %.2f\n"
" Script: %s\n Script confidence: %.2f\n",
orient_deg, orient_conf,
script_name, script_conf);
// Destroy used object and release memory
api->End();
delete api;
pixDestroy(&image);
return 0;
}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// 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("choices.png");
api->SetImage(image);
// Set lstm_choice_mode to alternative symbol choices per character, bbox is at word level.
api->SetVariable("lstm_choice_mode", "2");
api->Recognize(0);
tesseract::PageIteratorLevel level = tesseract::RIL_WORD;
tesseract::ResultIterator* res_it = api->GetIterator();
// Get confidence level for alternative symbol choices. Code is based on
// https://github.com/tesseract-ocr/tesseract/blob/main/src/api/hocrrenderer.cpp#L325-L344
std::vector<std::vector<std::pair<const char*, float>>>* choiceMap = nullptr;
if (res_it != 0) {
do {
const char* word;
float conf;
int x1, y1, x2, y2, tcnt = 1, gcnt = 1, wcnt = 0;
res_it->BoundingBox(level, &x1, &y1, &x2, &y2);
choiceMap = res_it->GetBestLSTMSymbolChoices();
for (auto timestep : *choiceMap) {
if (timestep.size() > 0) {
for (auto & j : timestep) {
conf = int(j.second * 100);
word = j.first;
printf("%d symbol: '%s'; \tconf: %.2f; BoundingBox: %d,%d,%d,%d;\n",
wcnt, word, conf, x1, y1, x2, y2);
gcnt++;
}
tcnt++;
}
wcnt++;
printf("\n");
}
} while (res_it->Next(level));
}
// Destroy used object and release memory
api->End();
delete api;
pixDestroy(&image);
return 0;
}
/*
Windows compile example:
SET TESS_INSTALLATION=C:/win64
SET OPENCV_INSTALLATION=C:/opencv/build
cl OpenCV_example.cc -I %TESS_INSTALLATION%/include -I %OPENCV_INSTALLATION%/include /link /LIBPATH:%TESS_INSTALLATION%/lib /LIBPATH:%OPENCV_INSTALLATION%/x64/vc14/lib tesseract51.lib leptonica-1.83.0.lib opencv_world460.lib /machine:x64
*/
#include <leptonica/allheaders.h>
#include <opencv2/opencv.hpp>
#include <string>
#include <tesseract/baseapi.h>
int main(int argc, char *argv[]) {
std::string outText, imPath = argv[1];
cv::Mat im = cv::imread(imPath, cv::IMREAD_COLOR);
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
api->Init(NULL, "eng", tesseract::OEM_LSTM_ONLY);
api->SetPageSegMode(tesseract::PSM_AUTO);
api->SetImage(im.data, im.cols, im.rows, 3, im.step);
outText = std::string(api->GetUTF8Text());
std::cout << outText;
api->End();
delete api;
return 0;
}
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
int main()
{
Pix *image;
char *outText;
char *configs[]={"path/to/my.patterns.config"};
int configs_size = 1;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
if (api->Init(NULL, "eng", tesseract::OEM_LSTM_ONLY, configs, configs_size, NULL, NULL, false)) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
image = pixRead("Arial.png");
api->SetImage(image);
outText = api->GetUTF8Text();
printf(outText);
api->End();
delete api;
delete [] outText;
pixDestroy(&image);
return 0;
}