Recursive AI

Share one OCR engine across C# worker threads

OneOcr is a free, open-source OCR library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

The engine is immutable and thread-safe, so both workers can share it.

Terminal
dotnet add package RecursiveAI.OneOcr

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneImage;
using OneOcr;
var engine = OcrEngine.LoadBundled();
string[] files = ["north.png", "south.png"];
Parallel.ForEach(files, file =>
    Console.WriteLine($"{file}:\n" +
        Image.Load(file).ToText(engine)));

Unzip the example, then dotnet run.

The two PNGs in the zip say “North entrance” and “South entrance”. One run printed this; either job can print first:

Text
south.png:
South entrance
north.png:
North entrance

Each worker loads its own image and prints the filename with its text in one call. If a worker fails to load its image, the loop reports the failure through AggregateException; another job may already have printed its result.

Variations

Read a batch in input order

Replace the Parallel.ForEach call in the snippet at the top with:

C#
var images = files.Select(file => Image.Load(file));
var pages = engine.Ocr(images);
foreach (var page in pages)
    Console.WriteLine($"{page.Index}: {page.ToText()}");
Text
0: North entrance
1: South entrance

Convert an image to text · Batch OCR images in input order


The short answer

Create one OcrEngine with LoadBundled() and pass that same instance to each worker. The engine is immutable and thread-safe. Keep the input and result for each job separate: in the example, each worker owns its own Image and labels the text with its filename.

How do I run OCR on multiple threads in C#?

For independent jobs, keep the engine in a long-lived field or capture it outside the worker loop as shown above. Load each image inside its job. If the work comes from a queue, label the result with the job ID so you can match it to the input regardless of which worker finishes first. Sharing the engine does not make your own mutable result collection thread-safe.

Worker count and OCR’s internal thread count are separate. OcrOptions.MaxDegreeOfParallelism controls the threads used inside recognition; the documented default is the smaller of four and the core count. If several large images are in flight, bound the number of jobs you start instead of loading the whole queue into memory.

How do I keep OCR results in input order?

Use the batch overload when you have a fixed set of images and want a list in input order. The variation supplies north.png first, so the north sign is at index 0 even if an independent worker loop happens to finish the south sign first. OcrAsync(images) preserves that same input order. When awaiting an async call, keep its input images alive until it completes.

Batching is an ordering choice, not an engine lifetime requirement. You can reuse one engine across separate calls, across batches, or both.

Source: OneOcr quick start and threading notes at v1.0.0 and batch behavior.

All oneocr recipes