如何解决tesseract文件未找到错误?

时间:2020-08-12 21:45:33

标签: python jupyter-notebook tesseract

!pip install detecto
!pip install tesseract
!pip install pytesseract

import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"tesseract-ocr/tesseract.exe"

text = pytesseract.image_to_string(cropped_image,lang='eng',config='--psm 10 --oem 1 -c 
tessedit_char_whitelist=0123456789')

首先,我得到了tesseract不在您的路径错误中的信息。但是我将tesseract安装在同一文件夹上,并给它指定了路径,现在我收到以下错误。我正在做一个Jupyter笔记本。

看着错误,它试图保存到输出文件,但是它不应该只是将文本返回到“ text”变量吗?

~/.local/lib/python3.6/site-packages/pytesseract/pytesseract.py in run_and_get_output(image, extension, lang, config, nice, timeout, return_bytes)
    270         run_tesseract(**kwargs)
    271         filename = kwargs['output_filename_base'] + extsep + extension
--> 272         with open(filename, 'rb') as output_file:
    273             if return_bytes:
    274                 return output_file.read()

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tess_0vpg1a42.txt'

1 个答案:

答案 0 :(得分:0)

当我尝试在WSL环境中使用“ Tesseract at UB Mannheim”运行pytesseract.image_to_string()函数(通过pytesseract命令安装的pip程序包)时遇到了同样的问题安装在Windows上。

尝试深入研究python包装器并对其进行调试,我发现文本文件(在您的情况下为/tmp/tess_0vpg1a42.txt)应该是tesseract.exe命令的输出,但是如果未创建失败的文件。就我而言,失败的根本原因是无法读取临时裁剪的 PNG 文件,该文件包含裁剪后的图像(在您的情况下为/tmp/tess_0vpg1a42.PNG),而python没有捕获到该图像。

我不知道为什么Windows环境中安装的tesseract.exe程序无法访问WSL环境中python创建的文件。但是我通过在WSL环境上安装tesseract-ocr软件包(sudo apt-get install tesseract-ocr)并在python代码中使用pytesseract.pytesseract.tesseract_cmd = 'tesseract'来解决了问题。