[Fixed] RuntimeError: Failed to init API possibly an invalid tessdata path

Today, I installed the tesseract lib for my Python project, however, encounters an error “RuntimeError: Failed to init API, possibly an invalid tessdata path: /usr/share/tesseract/” in the demo.

How to fix the RuntimeError: Failed to init API possibly an invalid tessdata path?

The code of demo as following:

import tesserocr

from PIL import Image
image = Image.open('image.png')
print(tesserocr.image_to_text(image))

The image.png is the logo of FAQgeek, but when running the python code, it with error as following:

raceback (most recent call last):
  File "sc.py", line 5, in <module>
    print(tesserocr.image_to_text(image))
  File "tesserocr.pyx", line 2467, in tesserocr.image_to_text
RuntimeError: Failed to init API, possibly an invalid tessdata path: /usr/share/tesseract/

Maybe, you can see the tip in the error info, then to make sure if the path to tessdata is right.

I did that too. But it’s don’t work.

The Surprisingly thing is that I changed the path to tessdata directory itself, instead of the parent directory.

Then, it works well! When I run the code again, it output the faqgeek text in the terminal.

Maybe, there are other solutions to fix the RuntimeError: Failed to init API, possibly an invalid tessdata path. But, this method should be the most hidden but the simplest.

Done.

Leave a Reply

Your email address will not be published. Required fields are marked *