RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Users\hp\Anaconda3\/tessdata/

Viewed 2620

I am using windows 10 and tesserocr version is 2.4. Want to detect text from an image and then the language of that text.

While running this piece of code:

from tesserocr import PyTessBaseAPI
import argparse
parser = argparse.ArgumentParser("Enter Image Path")
parser.add_argument('-i' , '--image' , help="Image Path")
args, unknown = parser.parse_known_args()
images = ['b.jpg']
images[0] = args.image
count = 0
count2 = 0
count3 = 0
with PyTessBaseAPI() as api:
    for img in images:
        api.Init(lang = 'eng')
        api.SetImageFile(img)
        # print api.AllWordConfidences()
        arr = list(api.AllWordConfidences())
        sumarr = sum(arr) / float(len(arr))

Getting below error :

<ipython-input-3-152e90ab44c3> in <module>
     13 count2 = 0
     14 count3 = 0
---> 15 with PyTessBaseAPI() as api:
     16    for img in images:
     17        api.Init(lang = 'eng')

tesserocr.pyx in tesserocr._tesserocr.PyTessBaseAPI.__cinit__()

tesserocr.pyx in tesserocr._tesserocr.PyTessBaseAPI._init_api()

RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Users\hp\Anaconda3\/tessdata/

Anyone aware of this issue ?

3 Answers

Add an environment variable on your system to point to the tessdata directory

enter image description here

You can also pass it in to PyTessBaseAPI as per the docs:

Args:
    path (str): The name of the parent directory of tessdata. Must end in /.

Should be:

 with PyTessBaseAPI(path='./') as api:

this is if you have the trainedata in the same folder as the script, otherwise substitute './' with your path

For others who still got this issue if you are using another language other than English, you might need to pass lang parameter to tessaocr like this

with PyTessBaseAPI(path='./',lang='tur') as api:
        api.SetImage(imageI)
        ocr = api.GetUTF8Text()
        confidence = api.AllWordConfidences()
        print(ocr,confidence)
        return ocr

For Windows when we are installing tesserocr library, by default it is going in the below path: default path-C:\Users\sagar\Anaconda_New\envs\Image_to_OCR\share/tessdata*

On executing our code we are getting this error : RuntimeError: Failed to init API, possibly an invalid tessdata path: C:\Users\sagar\Anaconda_New\envs\Image_to_OCR/tessdata/

Solution to this problem is cut the tessdata folder from the default path mentioned above and paste it in this path: C:\Users\sagar\Anaconda_New\envs\Image_to_OCR.

If you follow this steps your problem will be solved.

Related