.NET で Tesseract OCR を使用してドキュメント イメージのレイアウトを分析します。

ブログ カテゴリ: OCR.NET

2022/02/17

ドキュメント イメージからのテキストの認識は 2 つの手順で構成されます。最初の手順では、ドキュメント イメージのレイアウトを分析します。つまり、ドキュメント イメージ内の段落、テキスト行、単語、記号の位置を決定します。2 番目の手順では、ドキュメント イメージ内で文字認識を実行し、文字を段落、テキスト行、単語に分離します。

VintaSoft Imaging .NET SDKVintaSoft OCR .NET Plug-in を使用すると、TesseractOcr.Recognize メソッドを使用してドキュメント イメージ内のテキストを認識できます。このメソッドは、テキスト認識の両方のステップ、つまりレイアウトの分析と文字認識を実行します。

文字を認識せずに、文書画像内の段落、テキスト行、単語、記号の位置を分析する必要がある場合があります。
VintaSoft Imaging .NET SDKVintaSoft OCR .NET Plug-inを組み合わせると、TesseractOcr.AnalyzeLayoutメソッドを使用して文書画像のレイアウトを分析(文書画像内の段落、テキスト行、単語、記号の位置を決定)できます。TesseractOcr.AnalyzeLayoutメソッドはTesseractOcr.Recognize メソッドは、TesseractOcr.AnalyzeLayout メソッドはドキュメント イメージのレイアウトを分析しますが、文字認識は実行しないためです。

Tesseract OCR エンジン (TesseractOcr.AnalyzeLayout メソッド) を使用してドキュメント画像のレイアウトを分析する方法を示す C# コードは次のとおりです。
/// <summary>
/// Analyzes page layout using Tesseract OCR engine.
/// </summary>
/// <param name="filename">The name of document image file.</param>
public static void AnalyzePageLayoutUsingTesseractOCR(string filename)
{
    // create an image collection
    using (Vintasoft.Imaging.ImageCollection images =
        new Vintasoft.Imaging.ImageCollection())
    {
        // add images from file to the image collection
        images.Add(filename);

        System.Console.WriteLine("Create Tesseract OCR engine...");
        // create the Tesseract OCR engine
        using (Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr tesseractOcr =
            new Vintasoft.Imaging.Ocr.Tesseract.TesseractOcr())
        {
            System.Console.WriteLine("Initialize OCR engine...");
            // init the Tesseract OCR engine for recognition of English characters
            tesseractOcr.Init(new Vintasoft.Imaging.Ocr.OcrEngineSettings(Vintasoft.Imaging.Ocr.OcrLanguage.English));

            // for each image in image collection
            foreach (Vintasoft.Imaging.VintasoftImage image in images)
            {
                System.Console.WriteLine("Recognize the image...");

                // set image for Tesseract OCR engine
                tesseractOcr.SetImage(image);

                // analyze page layout and get result as OCR page
                Vintasoft.Imaging.Ocr.Results.OcrPage ocrPage = tesseractOcr.AnalyzeLayout();

                // clear image in Tesseract OCR engine
                tesseractOcr.ClearImage();

                // calculate count of regions, paragraphs, lines, words, symbols

                int regionCount = ocrPage.Regions.Count;
                int paragraphCount = 0;
                int lineCount = 0;
                int wordCount = 0;
                int symbolCount = 0;

                foreach (Vintasoft.Imaging.Ocr.Results.OcrRegion region in ocrPage.Regions)
                {
                    Vintasoft.Imaging.Ocr.Results.OcrTextRegion textRegion =
                        region as Vintasoft.Imaging.Ocr.Results.OcrTextRegion;
                    paragraphCount += textRegion.Paragraphs.Count;
                    foreach (Vintasoft.Imaging.Ocr.Results.OcrParagraph paragraph in textRegion.Paragraphs)
                    {
                        lineCount += paragraph.TextLines.Count;
                        foreach (Vintasoft.Imaging.Ocr.Results.OcrTextLine line in paragraph.TextLines)
                        {
                            wordCount += line.Words.Count;
                            foreach (Vintasoft.Imaging.Ocr.Results.OcrWord word in line.Words)
                            {
                                symbolCount += word.Symbols.Count;
                            }
                        }
                    }
                }

                // output information about count of regions, paragraphs, lines, words, symbols

                System.Console.WriteLine("Layout result:");
                System.Console.WriteLine(string.Format("- Region count: {0}", regionCount));
                System.Console.WriteLine(string.Format("- Paragraph count: {0}", paragraphCount));
                System.Console.WriteLine(string.Format("- Line count: {0}", lineCount));
                System.Console.WriteLine(string.Format("- Word count: {0}", wordCount));
                System.Console.WriteLine(string.Format("- Symbol count: {0}", symbolCount));
                System.Console.WriteLine();
                System.Console.ReadKey();
            }

            // shutdown the Tesseract OCR engine
            tesseractOcr.Shutdown();
        }

        // free images
        images.ClearAndDisposeItems();
    }
}