Iterate through the page layout hierarchy
masterTesseractOCR provides a hierarchical way to access the layout of a processed page. You can traverse from the page.Layout down through several levels of granularity:
- Blocks: The top-level containers (
page.Layout). - Paragraphs: Contained within blocks (
block.Paragraphs). - TextLines: Contained within paragraphs (
paragraph.TextLines). - Words: Contained within text lines (
textLine.Words). - Symbols: The individual characters within a word (
word.Symbols).
Each level provides access to Confidence, Text, and a BoundingBox (containing X1, Y1, X2, Y2, Width, and Height). Paragraphs also include metadata like Justification, IsListItem, IsCrown, and FirstLineIdent via the Info property.
using var engine = new Engine(@"./tessdata", Language.English, EngineMode.Default);
using var img = Pix.Image.LoadFromFile(testImagePath);
using var page = engine.Process(img);
var result = new StringBuilder();
foreach (var block in page.Layout)
{
// ... access block.Confidence, block.BoundingBox, block.Text ...
foreach (var paragraph in block.Paragraphs)
{
// ... access paragraph.Confidence, paragraph.Info, paragraph.Text ...
foreach (var textLine in paragraph.TextLines)
{
// ... access textLine.Confidence, textLine.Text, textLine.BoundingBox ...
foreach (var word in textLine.Words)
{
// ... access word.Confidence, word.Text, word.IsNumeric, word.Language ...
foreach (var symbol in word.Symbols)
{
// ... access symbol.Confidence, symbol.Text, symbol.IsSuperscript ...
}
}
}
}
}