# OnnxOcrEngine class

> 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...

Namespace: [PdfNetStack.Ocr](https://www.jadev-corp.com/api/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.

```csharp
public sealed class OnnxOcrEngine : IOcrEngine, IDisposable
```

**Inheritance**

[object](https://learn.microsoft.com/dotnet/api/system.object) ← 
[OnnxOcrEngine](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengine/)

**Implements**

[IOcrEngine](https://www.jadev-corp.com/api/pdfnetstack-ocr/iocrengine/), 
[IDisposable](https://learn.microsoft.com/dotnet/api/system.idisposable)

## Examples

<pre><code class="lang-csharp">// 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);</code></pre>

## Fields

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_DetectionModel"></a> DetectionModel

The detection model's file name.

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

#### Field Value

 [string](https://learn.microsoft.com/dotnet/api/system.string)

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_OrientationModel"></a> OrientationModel

The line orientation model's file name.

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

#### Field Value

 [string](https://learn.microsoft.com/dotnet/api/system.string)

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_PackagedModelFolder"></a> PackagedModelFolder

The folder, under the application's base directory, that the <code>PdfNetStack.Ocr.Models</code>
package copies the models to on build and publish.

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

#### Field Value

 [string](https://learn.microsoft.com/dotnet/api/system.string)

## Properties

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_ModelHashes"></a> ModelHashes

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

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

#### Property Value

 [IReadOnlyDictionary](https://learn.microsoft.com/dotnet/api/system.collections.generic.ireadonlydictionary\-2)<[string](https://learn.microsoft.com/dotnet/api/system.string), [string](https://learn.microsoft.com/dotnet/api/system.string)\>

## Methods

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_Dispose"></a> Dispose\(\)

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

```csharp
public void Dispose()
```

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_Load_PdfNetStack_Ocr_OnnxOcrEngineOptions_"></a> Load\(OnnxOcrEngineOptions?\)

Loads the models the <code>PdfNetStack.Ocr.Models</code> package puts next to the application, in
[PackagedModelFolder](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengine/#PdfNetStack_Ocr_OnnxOcrEngine_PackagedModelFolder) under `BaseDirectory`.

```csharp
public static OnnxOcrEngine Load(OnnxOcrEngineOptions? options = null)
```

#### Parameters

`options` [OnnxOcrEngineOptions](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengineoptions/)?

Languages and tuning; defaults when null.

#### Returns

 [OnnxOcrEngine](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengine/)

#### Exceptions

 [FileNotFoundException](https://learn.microsoft.com/dotnet/api/system.io.filenotfoundexception)

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

 [InvalidDataException](https://learn.microsoft.com/dotnet/api/system.io.invaliddataexception)

A model file does not match its pinned hash.

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_Load_System_String_PdfNetStack_Ocr_OnnxOcrEngineOptions_"></a> Load\(string, OnnxOcrEngineOptions?\)

Loads the models from a folder.

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

#### Parameters

`modelDirectory` [string](https://learn.microsoft.com/dotnet/api/system.string)

The folder holding the .onnx files (see [ModelHashes](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengine/#PdfNetStack_Ocr_OnnxOcrEngine_ModelHashes)).

`options` [OnnxOcrEngineOptions](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengineoptions/)?

Languages and tuning; defaults when null.

#### Returns

 [OnnxOcrEngine](https://www.jadev-corp.com/api/pdfnetstack-ocr/onnxocrengine/)

#### Exceptions

 [FileNotFoundException](https://learn.microsoft.com/dotnet/api/system.io.filenotfoundexception)

A model file is missing.

 [InvalidDataException](https://learn.microsoft.com/dotnet/api/system.io.invaliddataexception)

A model file does not match its pinned hash.

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_RecognitionModel_System_String_"></a> RecognitionModel\(string\)

The recognizer file for a script.

```csharp
public static string RecognitionModel(string language)
```

#### Parameters

`language` [string](https://learn.microsoft.com/dotnet/api/system.string)

<code>latin</code> or <code>arabic</code>.

#### Returns

 [string](https://learn.microsoft.com/dotnet/api/system.string)

### <a id="PdfNetStack_Ocr_OnnxOcrEngine_Recognize_PdfNetStack_Ocr_OcrBitmap_PdfNetStack_Ocr_OcrRequest_System_Threading_CancellationToken_"></a> Recognize\(OcrBitmap, OcrRequest, CancellationToken\)

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

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

#### Parameters

`image` [OcrBitmap](https://www.jadev-corp.com/api/pdfnetstack-ocr/ocrbitmap/)

The image, text upright.

`request` [OcrRequest](https://www.jadev-corp.com/api/pdfnetstack-ocr/ocrrequest/)

The script to read.

`cancellationToken` [CancellationToken](https://learn.microsoft.com/dotnet/api/system.threading.cancellationtoken)

Stops the work between lines.

#### Returns

 [OcrImageResult](https://www.jadev-corp.com/api/pdfnetstack-ocr/ocrimageresult/)

## Used in

- [Make scanned PDFs searchable with OCR in C#](https://www.jadev-corp.com/guides/ocr/)
- [Make a scanned PDF searchable in C#](https://www.jadev-corp.com/recipes/make-scanned-pdf-searchable/)

Source: https://www.jadev-corp.com/pdfnetstack/api/pdfnetstack-ocr/onnxocrengine/
