In a project where I want fast OCR of an incoming image, I wanted to use tesserocr which is supposed to be faster than pytesseract. My project is already working with pytesseract but at 2fps.
Tesserocr module: GitHub - sirfz/tesserocr: A Python wrapper for the tesseract-ocr API
So I followed all the steps in this blog post,
Creating a python 3.11 environment and adding the exact same execute DAT described in the blog post.
The procedure is working, because I can install modules like sklearn in the conda environment and import them in touchdesigner.
However with the module tesserocr, it’s not working. Something related with DLLs, since when I type import tesserocr in the textport, I get the following message:
ImportError: DLL load failed while importing tesserocr: The specified procedure could not be found.
I’m using build 2023.11280 with python 3.11.1 (+ conda env 3.11). But same bug was already occurring with the last 2022 stable build with python 3.9.5 (+ conda env 3.9.5).
Using Windows 11 22H2
Not sure if it’s a bug related to Touchdesigner so apologies if it’s not the right place to post it!
Did you make sure the Execute DAT toggle is turned on ?
In TD Textport, after the project is started, type import os, press enter, then type os.environ['PATH'], do you see the specified paths from the script in the list printed out ?
Hi Michel and thanks for your answer!
Here are the contents of my execute DAT.
The procedure works for other modules, so I guess I got the paths right, except maybe for the DLLs that are bundled in this tesserocr module.
import sys
import os
import platform
def onStart():
user = 'Ohme' # Update accordingly
condaEnv = 'td311' # Update accordingly
if platform.system() == 'Windows':
if sys.version_info.major >= 3 and sys.version_info.minor >= 8:
"""
Double check all the following paths, it could be that your anaconda 'envs' folder is not in your User folder depending on your conda install settings and conda version.
"""
os.add_dll_directory('C:/Users/'+user+'/.conda/envs/'+condaEnv+'/DLLs')
os.add_dll_directory('C:/Users/'+user+'/.conda/envs/'+condaEnv+'/Library/bin')
else:
"""
Double check all the following paths, it could be that your anaconda 'envs' folder is not in your User folder depending on your conda install settings and conda version.
"""
# Not the most elegant solution, but we need to control load order
os.environ['PATH'] = 'C:/Users/'+user+'/.conda/envs/'+condaEnv+'/DLLs' + os.pathsep + os.environ['PATH']
os.environ['PATH'] = 'C:/Users/'+user+'/.conda/envs/'+condaEnv+'/Library/bin' + os.pathsep + os.environ['PATH']
sys.path = ['C:/Users/'+user+'/.conda/envs/'+condaEnv+'/Lib/site-packages'] + sys.path
else:
"""
MacOS users should include path to .dlybs / MacOS binaries, site-packages
"""
os.environ['PATH'] = '/Users/'+user+'/opt/.conda/envs/'+condaEnv+'/lib' + os.pathsep + os.environ['PATH']
os.environ['PATH'] = '/Users/'+user+'/opt/.conda/envs/'+condaEnv+'/bin' + os.pathsep + os.environ['PATH']
# The following path might need editing (python3.9) based on the python version used with conda
sys.path = ['/Users/'+user+'/opt/.conda/envs/'+condaEnv+'/lib/python3.9/site-packages'] + sys.path
return
So when I type os.environ['PATH'], I get the following
Indeed it does not contain the paths of the conda environment. I guess that’s because the onStart script uses os.add_dll_directory instead of adding to os.environ[‘PATH’] based on the system version. Should I try to execute as well the contents of the script in the else: section ?
I initially installed tesserocr using the recommended method from the module developer conda install -c conda-forge tesserocr. But I noticed this method does not install any DLL files in the environment tree.
So I installed with the pip method instead pip install tesserocr-2.6.0-cp311-cp311-win_amd64.whl. This one installs a tesserocr folder in site-packages with all supossedly required DLLs. However I still get the same error
python >>> import tesserocr
ImportError: DLL load failed while importing _tesserocr: The specified procedure could not be found.
In the miniconda prompt however, import tesserocr works just fine. Therefore it makes me think this is about where tesserocr puts its DLL files and how touchdesigner is supposed to find them.
However I have no additionnal clue on how to fix this
Just a quick note that I am encountering the same issue so far using conda forge package. I will dig further and see if I can get the lib going by building the wheel myself.
Thanks for investivating Michel! Please let me know if there’s any development with this, I’m still interested in making this work and I think it could be useful for other people
Hi @JetXS , has there been any new development about this issue? I would be curious what the issue is, as I’ve had a similar issue (DLL load failed) with another package. And of course if there is a workaround it would be fantastic. For the moment I’m still stuck with the slow pytesseract solution.
The next build we post will have a fix for the tesserocr error.
Short story, we are internally using a DLL, zlib, that tesserocr is also depending on. This was a version conflict since the process, TouchDesigner, had already loaded it’s own DLL.
There is nothing much we can do about this other than updating / making things forward compatible for third party libraries like tesserocr to load properly.
I would be curious what the issue is, as I’ve had a similar issue (DLL load failed) with another package
It’d be great for us to also have the name of that other package you had issues with, if you can get back to us on that.
Thanks a lot @JetXS and the rest of the team! That’s awesome news. And thanks for the explanation, it makes sense.
I’ve been trying out the modules I used recently to find the culprit but I’ve had no luck so far reproducing the error with any module. I’ll let you know for sure if I find it!
Thanks again for the investigation and the fix!