0

I am currently trying to OCR some .tiff files. Apparently, 'Tesseract' only scans the first page of each file. I've been searching for a hint on Google, but that wasn't very helpful. This code is supposed to get the FULL text from each of the .tiff files:

public async Task<List<string>> ScannFile(string file)
{
    if (Path.GetFileName(file).EndsWith(".pdf"))
    {
        MessageBox.Show("Sie können nur .tiff Dokumente einscannen!");
        return null;
    }
    else
    {
        List<string> PageContent = new();
        await Task.Run(new Action(() =>
        {
            using (var engine = new TesseractEngine(@"C:\Users\f.rigo\source\repos\FinalScanner\FinalScanner\bin\Debug\net5.0-windows/tessdata", "deu", EngineMode.TesseractOnly))
            {
                using (var img = Pix.LoadFromFile(file))
                {
                    //img.Scale((float)scann_dpi / 2, (float)scann_dpi / 2);
                    using (var page = engine.Process(img))
                    {
                        var text = page.GetText();
                        PageContent = cleanOCROutput(text);
                    }
                }
            }
        }));
        return PageContent;
    }
}

I tried to get the full file by using a for-each loop, but unfortunately the "img" doesn't contain anything enumerable. By the way, I am using the Tesseract lib. by Charles Weld.

Do you have any suggestions for how I can scan the 2nd and later pages of .tiff files?

1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Capri
  • 1

1 Answers1

0

The code below shows how to use NuGet package Tesseract to extract text from a multi-page .tiff file.

The following has been tested using Tesseract (v4.1.1) and is adapted from the OP and from here.

Add the following using statements:

using Tesseract;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Diagnostics;

Note: In the code below, it's assumed that the tessdata folder (and associated files/sub-folders) exist in the same directory as the executable.

public async Task<List<string>> ScannFile(string filename)
{
    if (Path.GetFileName(filename).EndsWith(".pdf"))
    {
        MessageBox.Show("Sie können nur .tiff Dokumente einscannen!");
        return null;
    }
    else
    {
        List<string> PageContent = new List<string>();

        await Task.Run(new Action(() =>
        {
            using (Image tiffImg = Image.FromFile(filename))
            {
                //create new instance
                using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default))
                {
                    //get total pages
                    int pageCount = tiffImg.GetFrameCount(System.Drawing.Imaging.FrameDimension.Page);

                    for (int i = 0; i < pageCount; i++)
                    {
                        Debug.WriteLine(String.Format("---Page {0}---", i + 1));

                        //select active frame
                        tiffImg.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, i);

                        byte[] imageBytes = null;
                        using (MemoryStream ms = new MemoryStream())
                        {
                            //save to MemoryStream
                            tiffImg.Save(ms, System.Drawing.Imaging.ImageFormat.Tiff);

                            //copy to byte[]
                            imageBytes = ms.ToArray();
                        }

                        //load image 
                        using (var img = Pix.LoadTiffFromMemory(imageBytes))
                        {
                            using (var page = engine.Process(img))
                            {
                                //get text
                                string imageText = page.GetText();
                                Debug.WriteLine("imageText: " + imageText);

                                //add
                                PageContent.Add(imageText);
                            }
                        }
                    }
                }
            }
        }));

        return PageContent;
    }
}

Resources:

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24