0

Hello I am using Tesseract 5.2 to capture text from images but the problem is orientation of text in image file may vary, I am sharing 2 examples for the same. Sample 1

Sample 2

below is the code to capture text from image

using (var engine = new Tesseract.TesseractEngine(path, "eng", Tesseract.EngineMode.Default))
{
    using (var page = engine.Process(img, Tesseract.PageSegMode.Auto))
    {
        return page.GetText();
    }
}

I just want to know the orientation of text and based on that capture the text in proper way but I don't know how to get this

Rand Random
  • 7,300
  • 10
  • 40
  • 88
Ibrahim shaikh
  • 235
  • 2
  • 15

2 Answers2

0

you can use skew detection i've tried in python libraries not c# but here are the links may help you https://www.codeproject.com/Articles/104248/Detect-image-skew-angle-and-deskew-image

emgucv: pan card improper skew detection in C#

or: you can create a python app(like simple .py file and run with cmd) or api(like flask), pass image to python app as input and get results and then use in c#. here is some links to help you https://pyimagesearch.com/2017/02/20/text-skew-correction-opencv-python/

Python OpenCV skew correction for OCR

you don't need to know python just copy the codes

also i think EasyOCR better than Tesseract https://github.com/JaidedAI/EasyOCR

Amin Memar
  • 13
  • 3
0

Tesseract/Leptonica is all you need.

Want to add some caveats before answering though:

  1. Tesseract's Auto OSD should handle this by default. Try that option first and you might find you are already getting the right results.
  2. Tesseract-OCR is not suitable for handwritten text, which makes up most of your content (the answer on how to extract handwritten text is out-of-scope for this question).

You can find the orientation and also correct it using Tesseract libraries.

  1. Make sure you have osd.trainneddata in your tessdata folder. OSD here stands for Orientation Script Detection, which actually enables you to understand the orientation. It can be found here if you do not have it: https://github.com/tesseract-ocr/tessdata/blob/main/osd.traineddata
  2. Use Tesseract.PageSegMode.AutoOsd. This is different than what you had before as it not only does Automated Page Segmentation but also does Orientation Script Detection.
  3. Utilize page.AnalyseLayout()
    • This returns both the detected orientation of the text. It comes as an enum: (Page Up (0 degree, what you expect), Page Right (90 degree angle), Page Down (180 degree angle), and Page Left (270 degree angle).
    • It also returns the deskew angle that can be passed to Leptonica and fixed via rotation. You can do this through the Tesseract libraries however, no need to install another package.
 using (var pix = PixConverter.ToPix(image))
 {
     using (var page = engine.Process(pix, Tesseract.PageSegMode.AutoOsd))
     {
         using (var pageIter = page.AnalyseLayout())
         {
             pageIter.Begin();
             var pageProps = pageIter.GetProperties();
             // Get page orientation (Page Up, Page Down, Page Left, Page Right)
             var orientation = pageProps.Orientation;
             // Rotate image based on DeskewAngle (in radians)
             pix.Rotate(pageProps.DeskewAngle);
         }
                               
     }
}

I've ran this on your images and found that the orientation is found correctly for both sample images (for image 1 it detects as "Page Right", and for image 2 as "Page Left").