OCR pytesseract, recognize the biggest word in an image
<p>I'm using <strong>pytesseract</strong> to recognize text from an image, is there anyway i can detect/recognize the biggest word in that image</p>

<p><a href="http://i.stack.imgur.com/7lgZs.jpg" rel="nofollow">Click here to display the image</a></p>

<p>In the image above, i'm trying to detect/recognize "Atacand Plus" which is the biggest words in the whole image in size. I tried to apply filters like sharppen, smooth, and Max filter and repeated them many times between 5-10 times depends on the picture, to remove the smaller words, but is there any other way to do it?</p>

<p>Thanks,
Reagards</p>

<p>The issue here is that when you use the Tesseract segmentation it doesn't segment the image word by word, rather it does it by zones in the image, so paragraphs and blocks of words. So if you are trying to do this pre-recognition, then you are going to run into issues and errors. </p>

<p>Another option is to do post processing on the image and after recognition get the size of the characters and words and then filter out the ones that aren't the biggest words.</p>

<p>Tesseract OCR can return position information but it is not easy to get to. Here is some information on how to get the position information from Tesseract:</p>

<p><a href="http://stackoverflow.com/questions/20831612/getting-the-bounding-box-of-the-recognized-words-using-python-tesseract">Getting the bounding box of the recognized words using python-tesseract</a></p>

<p>Another option is to use a commercial library that makes this process easier. The <a href="https://www.leadtools.com/sdk/ocr" rel="nofollow">LEADTOOLS OCR Library</a> has an <a href="https://www.leadtools.com/help/leadtools/v19/dh/fo/leadtools.forms.ocr~leadtools.forms.ocr.ocrword.html" rel="nofollow">OcrWord struct</a> that you can use to get the location of the word and then compare it to the rest of the words. Disclaimer : I am an employee of this product.</p>

<p>I wrote a sample application using the .NET libraries from LEADTOOLS to show you how to achieve the functionality you are looking for:</p>

<pre><code>string filename = @"7lgZs.jpg";
using (RasterCodecs codecs = new RasterCodecs())
using (RasterImage image = codecs.Load(filename))
using (IOcrEngine ocrEngine = OcrEngineManager.CreateEngine(OcrEngineType.Advantage, false))
{
   ocrEngine.Startup(null, null, null, null);
   using (IOcrPage ocrPage = ocrEngine.CreatePage(image, OcrImageSharingMode.None))
   {
      ocrPage.Recognize(null);
      string biggestWord = GetBiggestWord(ocrPage);

      Console.WriteLine("The biggest word in this image is : {0}" , biggestWord);
      Console.ReadLine();
   }
}

static string GetBiggestWord(IOcrPage ocrPage)
  {
     IOcrPageCharacters ocrPageCharacters = ocrPage.GetRecognizedCharacters();
     OcrWord biggestWord = new OcrWord();
     biggestWord.Bounds = LogicalRectangle.Empty;
     biggestWord.Value = "";

     foreach (IOcrZoneCharacters ocrZoneCharacters in ocrPageCharacters)
     {
        ICollection&lt;OcrWord&gt; words = ocrZoneCharacters.GetWords(ocrPage.DpiX, ocrPage.DpiY, LogicalUnit.Pixel);
        foreach (var word in words)
        {
           double area = word.Bounds.Width * word.Bounds.Height;
           double biggestArea = biggestWord.Bounds.Width * biggestWord.Bounds.Height;
           if (area &gt; biggestArea)
              biggestWord = word;
        }
     }
     return biggestWord.Value;
  }
</code></pre>

<p>and here is a screenshot of the console output:</p>

<p><a href="https://i.stack.imgur.com/OLGJ9.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/OLGJ9.jpg" alt="demo screenshot"></a></p>

<p>I know you mentioned you were wanting to use python in your question, but you can call C# libraries from python as can be seen from this link:</p>

<p><a href="http://stackoverflow.com/questions/7367976/calling-a-c-sharp-library-from-python">Calling a C# library from python</a></p>

python image-processing ocr