Skip to content

TesseractOcrEngine class

View as Markdown

Namespace: PdfNetStack.Ocr.Tesseract
Assembly: PdfNetStack.Ocr.Tesseract.dll
Package: PdfNetStack

OCR with an installed Tesseract: each image is one tesseract process that reads the pixels from its standard input and writes its results to its standard output, with no shell and no temporary files. A process that crashes, hangs or floods its output only costs that image. Safe to call from several threads, up to MaxConcurrentProcesses at once. Tesseract and its language data are installed separately.

public sealed class TesseractOcrEngine : IOcrEngine, IDisposable

Inheritance

object ← TesseractOcrEngine

Implements

IOcrEngine, IDisposable

using var engine = TesseractOcrEngine.Load(new TesseractOcrEngineOptions { Languages = ["fra+ara"] });
var report = new PdfOcr(engine, new PdfOcrOptions { Languages = ["fra+ara"] }).MakeSearchable(document);

The languages Tesseract has data for, as it names them.

public IReadOnlyCollection<string> InstalledLanguages { get; }

IReadOnlyCollection<string>

The version the executable reports, such as 5.5.0.

public string Version { get; }

string

Refuses new calls, and ends those waiting for their turn with ObjectDisposedException. A process already running is not stopped: its call finishes with its real result or failure, within Timeout. To stop one sooner, cancel its token.

public void Dispose()

Finds Tesseract, checks its version and that the data for Languages is there.

public static TesseractOcrEngine Load(TesseractOcrEngineOptions? options = null)

options TesseractOcrEngineOptions?

Where things are, and tuning; defaults when null.

TesseractOcrEngine

FileNotFoundException

The executable or the data for a language is missing. The message says where to get it.

DirectoryNotFoundException

TessdataPath does not exist.

ArgumentException

A language name or a setting is not valid.

TesseractOcrException

The executable fails, or is not Tesseract 4 or later.

Recognize(OcrBitmap, OcrRequest, CancellationToken)

Section titled “ Recognize(OcrBitmap, OcrRequest, CancellationToken)”

Recognizes one upright 8 bit gray image; boxes come back in its pixel coordinates.

public OcrImageResult Recognize(OcrBitmap image, OcrRequest request, CancellationToken cancellationToken)

image OcrBitmap

The image, text upright.

request OcrRequest

The script to read.

cancellationToken CancellationToken

Stops the work between lines.

OcrImageResult

ArgumentException

The language is not installed or not valid, or the image is larger than Tesseract reads.

TimeoutException

The process took longer than Timeout and was killed.

TesseractOcrException

The process failed, or its output cannot be used.

OperationCanceledException

Cancelled; the process was killed.

ObjectDisposedException

The engine was disposed before this call’s process started.