Table of Contents

Class Hocr

Namespace
Egami.Ocr
Assembly
Egami.dll

Reads and writes hOCR, the HTML format that Tesseract, OCRopus, kraken and others write: pages (ocr_page), lines (ocr_line and its kin) and words (ocrx_word) with their bbox, x_wconf, textangle and lang.

public static class Hocr
Inheritance
Hocr
Inherited Members

Examples

// Read hOCR from Tesseract or any other engine, under limits for files you did not make.
using FileStream file = File.OpenRead("page.hocr");
IReadOnlyList<OcrResult> pages = Hocr.Read(file, new OcrReadOptions { MaxWords = 100_000 });
Console.WriteLine($"{pages.Count} page, {pages[0].Words.Count()} words: {pages[0].Lines[0].Text}");

// Write results back as hOCR (or Alto.Write for ALTO).
using FileStream copy = File.Create("copy.hocr");
Hocr.Write(copy, pages);

Remarks

The reader takes HTML as browsers do, not only well-formed XHTML: unknown tags, missing end tags and entities are fine, and no DTD or other file is ever fetched. Lines turned by textangle 90, 180 or 270 give turned word quads. Words outside a line each get a line of their own.

Methods

Parse(string, OcrReadOptions?)

Reads every page of hOCR text.

public static IReadOnlyList<OcrResult> Parse(string hocr, OcrReadOptions? options = null)

Parameters

hocr string

The hOCR document.

options OcrReadOptions

The limits, or null for Default.

Returns

IReadOnlyList<OcrResult>

One result per ocr_page, in document order.

Examples

// Read hOCR from Tesseract or any other engine, under limits for files you did not make.
using FileStream file = File.OpenRead("page.hocr");
IReadOnlyList<OcrResult> pages = Hocr.Read(file, new OcrReadOptions { MaxWords = 100_000 });
Console.WriteLine($"{pages.Count} page, {pages[0].Words.Count()} words: {pages[0].Lines[0].Text}");

// Write results back as hOCR (or Alto.Write for ALTO).
using FileStream copy = File.Create("copy.hocr");
Hocr.Write(copy, pages);

Exceptions

ArgumentNullException

hocr is null.

CorruptDataException

The text has no ocr_page and no ocrx_word.

LimitExceededException

The text breaks a limit of options.

Read(Stream, OcrReadOptions?)

Reads every page of an hOCR file.

public static IReadOnlyList<OcrResult> Read(Stream stream, OcrReadOptions? options = null)

Parameters

stream Stream

The file, from its current position. It is left open.

options OcrReadOptions

The limits, or null for Default.

Returns

IReadOnlyList<OcrResult>

One result per ocr_page, in file order.

Examples

// Read hOCR from Tesseract or any other engine, under limits for files you did not make.
using FileStream file = File.OpenRead("page.hocr");
IReadOnlyList<OcrResult> pages = Hocr.Read(file, new OcrReadOptions { MaxWords = 100_000 });
Console.WriteLine($"{pages.Count} page, {pages[0].Words.Count()} words: {pages[0].Lines[0].Text}");

// Write results back as hOCR (or Alto.Write for ALTO).
using FileStream copy = File.Create("copy.hocr");
Hocr.Write(copy, pages);

Exceptions

ArgumentNullException

stream is null.

CorruptDataException

The file has no ocr_page and no ocrx_word.

LimitExceededException

The file breaks a limit of options.

Write(Stream, IEnumerable<OcrResult>)

Writes pages as an hOCR (XHTML) document in UTF-8, one ocr_page per result.

public static void Write(Stream stream, IEnumerable<OcrResult> pages)

Parameters

stream Stream

Where to write, from its current position. It is left open.

pages IEnumerable<OcrResult>

The pages.

Examples

// Read hOCR from Tesseract or any other engine, under limits for files you did not make.
using FileStream file = File.OpenRead("page.hocr");
IReadOnlyList<OcrResult> pages = Hocr.Read(file, new OcrReadOptions { MaxWords = 100_000 });
Console.WriteLine($"{pages.Count} page, {pages[0].Words.Count()} words: {pages[0].Lines[0].Text}");

// Write results back as hOCR (or Alto.Write for ALTO).
using FileStream copy = File.Create("copy.hocr");
Hocr.Write(copy, pages);

Exceptions

ArgumentNullException

stream or pages is null.