.NET에서 Tesseract OCR을 사용하여 문서 이미지 레이아웃 분석

블로그 카테고리: OCR.NET

2022/02/17

문서 이미지에서 텍스트를 인식하는 과정은 두 단계로 구성됩니다. 첫 번째 단계는 문서 이미지의 레이아웃을 분석하는 것으로, 문서 이미지에서 단락, 텍스트 줄, 단어 및 기호의 위치를 ​​파악합니다. 두 번째 단계는 문서 이미지에서 문자를 인식하고 문자를 단락, 텍스트 줄 및 단어로 분리합니다.

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();
    }
}