Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 992
Aman Shrivastava Avatar asked Oct 14 '25 07:10

Aman Shrivastava


1 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

like image 148
K41F4r Avatar answered Oct 16 '25 21:10

K41F4r