Recursive AI

Export OCR word coordinates to TSV in C#

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

Write one tab-separated row per word in list.png (in the zip). This recipe uses its own column layout, not another OCR engine’s TSV format.

Terminal
dotnet add package RecursiveAI.OneOcr

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

C#
using OneOcr;
using System.Globalization;
 
CultureInfo.CurrentCulture =
    CultureInfo.InvariantCulture;
var ocr = OcrEngine.LoadBundled();
var page = ocr.OcrPages("list.png").Single();
using var tsv = File.CreateText("words.tsv");
for (int b = 0; b < page.Blocks.Count; b++)
{
    var lines = page.Blocks[b].Lines;
    for (int l = 0; l < lines.Count; l++)
    foreach (var word in lines[l].Words)
    {
        var box = word.Box;
        tsv.WriteLine(string.Join('\t', b, l,
            box.Left, box.Top, box.Right - box.Left,
            box.Bottom - box.Top, word.Confidence,
            word.Text));
    }
}
Console.WriteLine("Wrote words.tsv.");

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

Writes words.tsv and prints:

Text
Wrote words.tsv.

The first three rows of words.tsv are:

Text
0	0	20	14	47	23	0.99836606	Red
0	1	23	57	82	23	0.9911584	Green
1	0	91	13	98	31	0.99785054	apples

The sample image has two printed rows: “Red apples” and “Green pears.”

The full program

C#
using OneOcr;
 
var ocr = OcrEngine.LoadBundled();
var page = ocr.OcrPages("list.png").Single();
using var tsv = File.CreateText("words.tsv");
tsv.WriteLine("block\tline\tleft\ttop\twidth\theight\tconfidence\ttext");
for (int b = 0; b < page.Blocks.Count; b++)
{
    var lines = page.Blocks[b].Lines;
    for (int l = 0; l < lines.Count; l++)
    foreach (var word in lines[l].Words)
    {
        var box = word.Box;
        var text = word.Text.Replace('\t', ' ').Replace('\r', ' ').Replace('\n', ' ');
        tsv.WriteLine(FormattableString.Invariant(
            $"{b}\t{l}\t{box.Left:0.##}\t{box.Top:0.##}\t{box.Right - box.Left:0.##}\t{box.Bottom - box.Top:0.##}\t{word.Confidence:0.000}\t{text}"));
    }
}
Console.WriteLine($"Wrote words.tsv ({page.Words.Count} words).");

dotnet run prints:

Text
Wrote words.tsv (4 words).

The snippet writes only data rows. The full program adds a header, rounds values for display, and replaces any tab or line break in a word with a space so it stays in one TSV cell. Its output file contains:

Text
block	line	left	top	width	height	confidence	text
0	0	20	14	47	23	0.998	Red
0	1	23	57	82	23	0.991	Green
1	0	91	13	98	31	0.998	apples
1	1	127	62	78	24	0.995	pears

If no words are recognized, the file contains only the header.

Variations

Export only words to review. Add this before var box in the full program, and omit the final word-count message (it counts all recognized words):

C#
if (word.Confidence >= 0.995f)
    continue;

The short answer

Read the image with OcrEngine.OcrPages, then walk Blocks → Lines → Words. Write each word’s text, its two indexes, its box, and Confidence as tab-separated columns. This is a word table for your own code or for importing into a spreadsheet, not a drop-in replacement for another OCR engine’s TSV format.

How do I get the pixel coordinates of each OCR word?

Use OcrWord.Box. For an image, Left and Top locate the upper-left corner in pixels from the top-left of the image. X increases to the right and Y increases down. Subtract Left from Right for the width and Top from Bottom for the height. The values can be fractional; keep that precision if you’ll scale the box to fit an image viewer. A PDF page can use points instead of pixels, so don’t assume the same units when switching inputs.

The block index is zero-based within the page; line is zero-based within that block. In the sample, OneOcr groups the left and right words into separate blocks. Red and apples share a visual row but aren’t on the same line within the same block. Use the box coordinates if you need to group words by their position on the image rather than by the OCR layout. If you combine output from multiple images, add an image or page column so that (block, line) stays unambiguous.

Which format is best for OCR results: TSV or JSON?

Use TSV if the consumer wants one row per word and a fixed list of columns. The recipe writes confidence on its original 0–1 scale, not as a percentage. It formats decimal values using the invariant culture so a program reading the same file sees a decimal point regardless of the machine’s locale. Import text as a text column if values such as part numbers with leading zeros must stay unchanged.

Choose the OneOcr page JSON format if you also need the nested page structure, character boxes, tables, or per-element source information. The TSV file deliberately leaves those out. It also leaves out non-word rows such as page and paragraph records. If your reader expects a particular TSV schema, agree on its columns, confidence scale, and indexing before you plug this file into it.

All oneocr recipes