OnnxOcrEngine class
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, IDisposableInheritance
Implements
Examples
Section titled “Examples”// 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);
Fields
Section titled “Fields”DetectionModel
Section titled “ DetectionModel”The detection model’s file name.
public const string DetectionModel = "ch_PP-OCRv5_det_mobile.onnx"Field Value
Section titled “Field Value”OrientationModel
Section titled “ OrientationModel”The line orientation model’s file name.
public const string OrientationModel = "ch_PP-LCNet_x0_25_textline_ori_cls_mobile.onnx"Field Value
Section titled “Field Value”PackagedModelFolder
Section titled “ PackagedModelFolder”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"Field Value
Section titled “Field Value”Properties
Section titled “Properties”ModelHashes
Section titled “ ModelHashes”The model files and their SHA-256 hashes, for download scripts and audits.
public static IReadOnlyDictionary<string, string> ModelHashes { get; }Property Value
Section titled “Property Value”IReadOnlyDictionary<string, string>
Methods
Section titled “Methods”Dispose()
Section titled “ Dispose()”Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()Load(OnnxOcrEngineOptions?)
Section titled “ Load(OnnxOcrEngineOptions?)”Loads the models the PdfNetStack.Ocr.Models package puts next to the application, in
PackagedModelFolder under BaseDirectory.
public static OnnxOcrEngine Load(OnnxOcrEngineOptions? options = null)Parameters
Section titled “Parameters”options OnnxOcrEngineOptions?
Languages and tuning; defaults when null.
Returns
Section titled “Returns”Exceptions
Section titled “Exceptions”The models are not there: the application does not reference the package.
A model file does not match its pinned hash.
Load(string, OnnxOcrEngineOptions?)
Section titled “ Load(string, OnnxOcrEngineOptions?)”Loads the models from a folder.
public static OnnxOcrEngine Load(string modelDirectory, OnnxOcrEngineOptions? options = null)Parameters
Section titled “Parameters”modelDirectory string
The folder holding the .onnx files (see ModelHashes).
options OnnxOcrEngineOptions?
Languages and tuning; defaults when null.
Returns
Section titled “Returns”Exceptions
Section titled “Exceptions”A model file is missing.
A model file does not match its pinned hash.
RecognitionModel(string)
Section titled “ RecognitionModel(string)”The recognizer file for a script.
public static string RecognitionModel(string language)Parameters
Section titled “Parameters”language string
latin or arabic.
Returns
Section titled “Returns”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)Parameters
Section titled “Parameters”image OcrBitmap
The image, text upright.
request OcrRequest
The script to read.
cancellationToken CancellationToken
Stops the work between lines.