TesseractOCR

repository·master·Indexed 42 days ago

https://github.com/sicos1977/tesseractocr

A .NET wrapper for the Tesseract 5.5.2 OCR engine that enables optical character recognition on images using C#. It provides a hierarchical layout API to access blocks, paragraphs, text lines, words, and symbols, and supports various image formats via the Leptonica library.

Tokens
1.4K
Snippets
4
Records
7
Agent score
3%

What's inside TesseractOCR

  1. Iterate through the page layout hierarchy

    master

    TesseractOCR 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:

    1. Blocks: The top-level containers (page.Layout).
    2. Paragraphs: Contained within blocks (block.Paragraphs).
    3. TextLines: Contained within paragraphs (paragraph.TextLines).
    4. Words: Contained within text lines (textLine.Words).
    5. 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 ...
                    }
                }
            }
        }
    }
  2. OCR a single page

    master

    To perform basic OCR on an image, initialize an Engine with the path to your tessdata, the target Language, and an EngineMode. Use TesseractOCR.Pix.Image.LoadFromFile to load the image, then call engine.Process(img) to get a page object containing the text and confidence scores.

    using var engine = new Engine(@"./tessdata", Language.English, EngineMode.Default);
    using var img = TesseractOCR.Pix.Image.LoadFromFile(testImagePath);
    using var page = engine.Process(img);
    Console.WriteLine("Mean confidence: {0}", page.MeanConfidence);
    Console.WriteLine("Text: \r\n{0}", page.Text);
  3. Configure logging with Microsoft ILogger

    master

    TesseractOCR uses the Microsoft.Extensions.Logging.ILogger interface. You can use any compatible logging library. The TesseractOCR.Logger namespace provides built-in loggers, such as Stream (for files) and Console.

    var logger = !string.IsNullOrWhiteSpace(<some logfile>)
                    ? new TesseractOCR.Loggers.Stream(File.OpenWrite(<some logfile>))
                    : new TesseractOCR.Loggers.Console();
  4. Supported input image formats

    master

    TesseractOCR uses the Leptonica library to read images. Supported formats include:

    • PNG (requires libpng, libz)
    • JPEG (requires libjpeg / libjpeg-turbo)
    • TIFF (requires libtiff, libz)
    • JPEG 2000 (requires libopenjp2)
    • GIF (requires libgif)
    • WebP (including animated WebP, requires libwebp)
    • BMP (no library required*)
    • PNM (no library required*)

    *Except Leptonica requirements. Note: Support for Windows.Drawing.Image has been removed to ensure cross-platform compatibility; use Leptonica-supported formats instead.