Skip to content

OnnxOcrEngine class

View as Markdown

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

OCR with the PP-OCRv5 mobile models (PaddleOCR, Apache-2.0) on ONNX Runtime: a DB text detector, a line orientation classifier and CTC recognizers for Latin and Arabic script. Pre and post processing are managed code; the native runtime only sees float tensors.

public sealed class OnnxOcrEngine : IOcrEngine, IDisposable

Inheritance

object ← OnnxOcrEngine

Implements

IOcrEngine, IDisposable

// The models from the PdfNetStack.Ocr.Models package, or OnnxOcrEngine.Load("models", ...) for a folder of your own.
using var engine = OnnxOcrEngine.Load(new OnnxOcrEngineOptions { Languages = ["latin", "arabic"] });
var report = new PdfOcr(engine).MakeSearchable(document);

The detection model’s file name.

public const string DetectionModel = "ch_PP-OCRv5_det_mobile.onnx"

string

The line orientation model’s file name.

public const string OrientationModel = "ch_PP-LCNet_x0_25_textline_ori_cls_mobile.onnx"

string

The folder, under the application’s base directory, that the PdfNetStack.Ocr.Models package copies the models to on build and publish.

public const string PackagedModelFolder = "pdfnetstack-ocr-models"

string

The model files and their SHA-256 hashes, for download scripts and audits.

public static IReadOnlyDictionary<string, string> ModelHashes { get; }

IReadOnlyDictionary<string, string>

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Loads the models the PdfNetStack.Ocr.Models package puts next to the application, in PackagedModelFolder under BaseDirectory.

public static OnnxOcrEngine Load(OnnxOcrEngineOptions? options = null)

options OnnxOcrEngineOptions?

Languages and tuning; defaults when null.

OnnxOcrEngine

FileNotFoundException

The models are not there: the application does not reference the package.

InvalidDataException

A model file does not match its pinned hash.

Loads the models from a folder.

public static OnnxOcrEngine Load(string modelDirectory, OnnxOcrEngineOptions? options = null)

modelDirectory string

The folder holding the .onnx files (see ModelHashes).

options OnnxOcrEngineOptions?

Languages and tuning; defaults when null.

OnnxOcrEngine

FileNotFoundException

A model file is missing.

InvalidDataException

A model file does not match its pinned hash.

The recognizer file for a script.

public static string RecognitionModel(string language)

language string

latin or arabic.

string

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