Recursive AI

OCR Hindi (Devanagari) text in C#

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

Terminal
dotnet add package RecursiveAI.OneOcr
dotnet add package RecursiveAI.OneOcr.Models.Indic

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

C#
using OneImage;
using OneOcr;
 
var engine = OcrEngine.LoadBundled();
var image = Image.Load("notice.png");
Console.WriteLine(image.ToText(engine));

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

The printed text in notice.png (included in the zip):

Text
आज पुस्तकालय खुला है
कृपया दरवाजा बंद करें

What the example prints:

Text
आज पुस्तकालय खुला है	है
कृपया दरवाजा बंद करें

Both source lines were read, but OneOcr added a second है to the first line. That word is part of the OCR output, not part of the image; check the result before reusing it.

Variations

Inspect the script and text of each word. Replace the last line with:

C#
var page = image.Ocr(engine);
foreach (var word in page.Words)
    Console.WriteLine($"{word.Script}: {word.Text}");

Deva is the script code for Devanagari. The extra word is also labeled Deva, so the label alone won’t filter it out.

Filter the extra word on this sample. Replace the print call with:

C#
var page = image.Ocr(engine);
foreach (var word in page.Words)
    if (word.Confidence >= 0.95f)
        Console.WriteLine(word.Text);

On this notice, 0.95 drops the extra word and keeps all eight source words. Don’t assume the same cutoff will work on other images.


The short answer

Install RecursiveAI.OneOcr.Models.Indic with RecursiveAI.OneOcr. OcrEngine.LoadBundled() finds the installed model, and image.ToText(engine) returns the recognized Hindi as editable text. Use the Indic package even if your program only reads Hindi: the recognizer is packaged by script family, not as one model per language.

How can I extract Hindi text from an image?

With the Indic model installed, OneOcr decides which script to read from the image by default. For pages with separate Hindi and English lines, leave automatic script selection on so OneOcr can choose a recognizer for each line. OneOcr 1.2.1 does not support handwriting. There’s no manual step to split the Devanagari letters or separate vowel signs before passing in the image. Use the same PNG-loading call on a scan of your own printed text. Keep the entire line in frame, including vowel signs that extend above and below the letters. A crop that cuts those pixels off gives OCR less information than the original image.

The image-to-text call is useful when you want to copy the whole notice into another document. For a form where each field has a specific location, inspect image.Ocr(engine) instead. The result associates words with boxes in the image. Compare a suspicious word’s box with the source rather than deleting every short word: even on this notice, है occurs both in the original and in the extra output.

Is the extracted Hindi text Unicode?

Yes. The result is a .NET string containing Devanagari characters, not a picture of the line or a transliteration into Latin letters. Save it as UTF-8 if you write it to a text file, and open it with a font that supports Devanagari. Display and recognition are separate: a font can render a recognized character correctly, but it can’t restore a vowel sign the recognizer missed. When proofreading the result, compare whole words against the image, including marks attached to a letter.

All oneocr recipes