tesseract clib

Tesseract OCR — extract text from images and scans in 100+ languages with confidence scores, bounding boxes, and structured layout output.

ppm install tesseract
Requires the Tesseract library and at least one language pack:
sudo apt install libtesseract-dev tesseract-ocr tesseract-ocr-eng
Additional languages: sudo apt install tesseract-ocr-por tesseract-ocr-spa tesseract-ocr-deu

Overview

Tesseract is the leading open-source OCR engine, originally developed by HP and now maintained by Google. It uses a neural network (LSTM) and supports 100+ languages including Latin, Arabic, Chinese, Japanese, and more. Beyond plain text, it can return structured results with per-word confidence scores and pixel-accurate bounding boxes.

Quick Start

uses tesseract;

{ One-shot — no handle needed }
var text := OCRFile('/tmp/scan.png');
Writeln(text);

{ With specific language }
var pt := OCRFileLang('/tmp/doc.jpg', 'por');

{ Only extract digits (e.g. meter readings, invoices) }
var nums := OCRDigits('/tmp/meter.png');

Full API — Confidence & Bounding Boxes

uses tesseract;

var api := NewAPI('', 'eng');  { '' = default tessdata path }
SetPageSeg(api, psmAuto);

SetImageFile(api, '/tmp/invoice.png');

var text := GetText(api);
var conf := GetMeanConfidence(api);
Writeln('Confidence: ' + FloatToStr(conf) + '%');

{ Iterate recognized words }
var words := GetWords(api);
for w in words do
begin
  if w.Confidence > 80 then
    Writeln(w.Text + ' at (' + IntToStr(w.Left) + ',' + IntToStr(w.Top) + ')');
end;

FreeAPI(api);

Multi-language & LSTM Mode

{ English + Spanish, neural net only }
var api := NewAPIEx('', 'eng+spa', oemLSTM);
SetImageFile(api, '/tmp/mixed.png');
var text := GetText(api);
FreeAPI(api);

Restrict to Region

var api := NewAPI('', 'eng');
SetImageFile(api, '/tmp/form.png');

{ Only read the "Total" field region (pixels) }
SetRectangle(api, 500, 800, 200, 40);
var total := GetText(api);

FreeAPI(api);

HOCR Output

{ HOCR = HTML with embedded bounding boxes — useful for PDF text layers }
var api := NewAPI('', 'eng');
SetImageFile(api, '/tmp/page.png');
var hocr := GetHOCR(api, 1);
{ Write hocr to a .hocr file for pdfbeads, hocr2pdf, etc. }
FreeAPI(api);

Page Segmentation Modes

ModeUse when…
psmAutoGeneral documents (default — works for most cases)
psmSingleLineSingle text line (form fields, captions)
psmSingleWordSingle word (price tags, labels)
psmSingleBlockSingle paragraph or uniform block
psmSparseTextText scattered anywhere in the image
psmSingleCharSingle character (CAPTCHA digit)
psmOSDOnly detect orientation — no recognition

API Reference

Lifecycle

FunctionDescription
NewAPI(DataPath, Lang)Create Tesseract instance. DataPath='' for system default.
NewAPIEx(DataPath, Lang, Mode)Create with explicit OCR engine mode.
FreeAPI(API)Destroy instance and free memory.
SetPageSeg(API, Mode)Set page segmentation mode before recognition.
SetVariable(API, Name, Value)Set Tesseract config variable (e.g. char whitelist).

Image Input

FunctionDescription
SetImageFile(API, Path)Load image from file (PNG, JPEG, TIFF, BMP…).
SetImageBytes(API, Data)Load image from byte string.
SetImageRaw(API, Pixels, W, H, BPP, Stride)Raw RGB/RGBA pixels.
SetRectangle(API, L, T, W, H)Restrict OCR to a sub-region in pixels.

Recognition

FunctionDescription
GetText(API)Plain text result.
GetMeanConfidence(API)Mean confidence 0–100.
GetWords(API)Array of TOCRWord (text, confidence, bounding box).
GetLines(API)Array of TOCRLine with embedded words.
GetResult(API)Full TOCRResult: words, lines, blocks, confidence.
GetHOCR(API, Page)HOCR HTML with bounding boxes.
GetALTO(API, Page)ALTO XML for archival use.
DetectOrientation(API)Page orientation without OCR.
DetectScript(API)Writing system detection ('Latin', 'Arabic', …).

Convenience

FunctionDescription
OCRFile(Path)One-shot English OCR from file.
OCRFileLang(Path, Lang)One-shot OCR with language code.
OCRBytes(Data)One-shot OCR from byte string.
OCRDigits(Path)Extract only digits (0–9).
ListLanguages(DataPath)List installed language packs.