Recursive AI

OCR a single cropped line of text in C#

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

Read one upright line from line.png (in the zip) without detecting page layout.

Terminal
dotnet add package RecursiveAI.OneOcr

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

C#
using OneImage;
using OneOcr.Model;
using OneOcr.Models;
using OneOcr.Recognition;
 
var weights = BundledModels.ReadRecognizer();
var model = ModelContainer.Parse(weights);
var reader = LineRecognizer.FromContainer(model);
var crop = Image.Load("line.png")
    .Resize(height: reader.InputHeight)
    .ConvertTo(PixelFormat.Gray8);
var gray = crop.ToArray<Gray8>();
var bytes = gray.Select(pixel => pixel.V).ToArray();
var result = reader.Recognize(bytes, crop.Width);
Console.WriteLine(result.Text);
Console.WriteLine(FormattableString.Invariant(
    $"Confidence: {result.Confidence:0.000}"));

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Reads the image of “Pickup at noon” and prints:

Text
Pickup at noon
Confidence: 1.000

OneImage comes with OneOcr as a dependency and handles the resize and grayscale conversion. A buffer whose length isn’t InputHeight × width causes ArgumentException.

Variations

Save just the recognized text. Add this after the call to Recognize:

C#
File.WriteAllText("line.txt", result.Text);

The short answer

Load the bundled recognition weights with BundledModels.ReadRecognizer() and create a LineRecognizer from the parsed model. Resize the crop to reader.InputHeight, keeping its aspect ratio. Pass one grayscale byte per pixel to Recognize along with the resized width. You get text and a confidence score, not a page or word boxes.

How do I read only a cropped area of an image with C# OCR?

Use the line recognizer when the crop contains exactly one upright text line, for example a label cut from a fixed area of a form. It expects pixels from the line, not the original PNG bytes. The example loads a PNG, specifies only the target height so the width scales with it, and converts the result to Gray8. ToArray<Gray8>() gives the pixels in row order: left to right across the first row, then left to right across the next. The program takes the byte value of each pixel before calling Recognize.

Don’t force every crop to the same width. The model requires a fixed height, not a fixed width, and stretching the image horizontally changes the shape of the letters. The recognizer’s input specification describes how the model normalizes those grayscale values. You don’t need to do that normalization yourself.

If you have a whole page or a crop with multiple lines, use the page OCR API instead. The line recognizer does not split a crop into lines or return positions for the text it reads. A crop narrower than the model can process returns empty text; crops that exceed the recognizer’s size limits throw OcrLimitException.

Is OCR 100% accurate if the confidence shows 1.000?

No. The default line score is an uncalibrated summary of the probabilities for the characters the model emitted. It is zero when no text was read. The example rounds the score to three decimal places, so 1.000 is neither a guarantee that the line is right nor a claim that the internal score was exactly one.

Keep the unrounded value if you use it to prioritize manual review, then check candidate thresholds against labeled crops from your own documents. Don’t reuse a threshold from the page model’s word-confidence scores: those are scores for different units and the page pipeline can calibrate them.

All oneocr recipes