1

I have an image. I read the text content using ironocr. The following code used to read text.

var Ocr = new AutoOcr();
var Result = Ocr.Read(bmpCrop);
string text = Result.Text; 
return text;

But the text trims the space and I couldn't get the exact copy of the text as in the image. Is there way or any other ocr libraries that reads text as an exact copy from the image. Please find the image attached, that I have used to read using ocr.

I have tried the following methode specified in the below url also, This also not working for me.

How to preserve document structure in tesseract

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
vijesh
  • 1,115
  • 1
  • 11
  • 27
  • 2
    > "Is there way or any other ocr libraries that reads text as an exact copy from the image" - if there was they'd be worth millions of dollars and its authors would go down in the annals of AI and computer-science as having solved some major problems in the space. – Dai May 28 '18 at 13:38
  • 2
    From @TimLepage: `There is the online API of [OCR.space](https://ocr.space/ocrapi) that gives you a JSON document of your image with the words read and their position in the image (with the option "isOverlayRequired=true"). I don't know if that could help you.` – Panagiotis Kanavos May 28 '18 at 13:53
  • 2
    There is no whitespace in the linked question, just a page with text blocks laid out in an *unknown order*, if indeed there is any. In fact, the two-column top-down order found in newspapers and magazines makes much more sense than left to right. – Panagiotis Kanavos May 28 '18 at 13:56
  • 2
    OCRs recognize text blocks for decade**s**. The cheap/freemium ones bundled with scanners actually *downgrade* the results to force people to upgrade. You'll have to export the *blocks* and their layout, not request the flat text results. You'll have to check your library's documentation for details – Panagiotis Kanavos May 28 '18 at 14:00

1 Answers1

0

I have found that the latest IronOCR has a detailed document object model of pages, blocks, paragraphs, lines, words and characters

https://ironsoftware.com/csharp/ocr/examples/results-objects/

using IronOcr;
using System.Drawing; //for image export
 
// We can delve deep into OCR results as an object model of
// Pages, Barcodes, Paragraphs, Lines, Words and Characters
// This allows us to explore, export and draw OCR content using other APIs/
var Ocr = new IronTesseract();
Ocr.Configuration.EngineMode = TesseractEngineMode.TesseractAndLstm;
Ocr.Configuration.ReadBarCodes = true;
using (var Input = new OcrInput(@"example.tiff"))
{
    OcrResult Result = Ocr.Read(Input);
    foreach (var Page in Result.Pages)
    {
        // Page object
        int PageNumber = Page.PageNumber;
        string PageText = Page.Text;
        int PageWordCount = Page.WordCount;
        // null if we dont set Ocr.Configuration.ReadBarCodes = true;
        OcrResult.Barcode[] Barcodes = Page.Barcodes;
        System.Drawing.Bitmap PageImage = Page.ToBitmap(Input);
        int PageWidth = Page.Width;
        int PageHeight = Page.Height;
        foreach (var Paragraph in Page.Paragraphs)
        {
            // Pages -> Paragraphs
            int ParagraphNumber = Paragraph.ParagraphNumber;
            String ParagraphText = Paragraph.Text;
            System.Drawing.Bitmap ParagraphImage = Paragraph.ToBitmap(Input);
            int ParagraphX_location = Paragraph.X;
            int ParagraphY_location = Paragraph.Y;
            int ParagraphWidth = Paragraph.Width;
            int ParagraphHeight = Paragraph.Height;
            double ParagraphOcrAccuracy = Paragraph.Confidence;
            OcrResult.TextFlow paragrapthText_direction = Paragraph.TextDirection;
            foreach (var Line in Paragraph.Lines)
            {
                // Pages -> Paragraphs -> Lines
                int LineNumber = Line.LineNumber;
                String LineText = Line.Text;
                System.Drawing.Bitmap LineImage = Line.ToBitmap(Input); ;
                int LineX_location = Line.X;
                int LineY_location = Line.Y;
                int LineWidth = Line.Width;
                int LineHeight = Line.Height;
                double LineOcrAccuracy = Line.Confidence;
                double LineSkew = Line.BaselineAngle;
                double LineOffset = Line.BaselineOffset;
                foreach (var Word in Line.Words)
                {
                    // Pages -> Paragraphs -> Lines -> Words
                    int WordNumber = Word.WordNumber;
                    String WordText = Word.Text;
                    System.Drawing.Image WordImage = Word.ToBitmap(Input);
                    int WordX_location = Word.X;
                    int WordY_location = Word.Y;
                    int WordWidth = Word.Width;
                    int WordHeight = Word.Height;
                    double WordOcrAccuracy = Word.Confidence;
                    if (Word.Font != null)
                    {
                        // Word.Font is only set when using Tesseract Engine Modes rather than LTSM
                        String FontName = Word.Font.FontName;
                        double FontSize = Word.Font.FontSize;
                        bool IsBold = Word.Font.IsBold;
                        bool IsFixedWidth = Word.Font.IsFixedWidth;
                        bool IsItalic = Word.Font.IsItalic;
                        bool IsSerif = Word.Font.IsSerif;
                        bool IsUnderLined = Word.Font.IsUnderlined;
                        bool IsFancy = Word.Font.IsCaligraphic;
                    }
                    foreach (var Character in Word.Characters)
                    {
                        // Pages -> Paragraphs -> Lines -> Words -> Characters
                        int CharacterNumber = Character.CharacterNumber;
                        String CharacterText = Character.Text;
                        System.Drawing.Bitmap CharacterImage = Character.ToBitmap(Input);
                        int CharacterX_location = Character.X;
                        int CharacterY_location = Character.Y;
                        int CharacterWidth = Character.Width;
                        int CharacterHeight = Character.Height;
                        double CharacterOcrAccuracy = Character.Confidence;
                        // Output alternative symbols choices and their probability.
                        // Very useful for spellchecking
                        OcrResult.Choice[] Choices = Character.Choices;
                    }
                }
            }
        }
    }
}
Stephanie
  • 600
  • 13
  • 24