尝试访问“/usr/local/Cellar/tesseract/4.1.1/share/tessdata/”时发生 PermissionError

PermissionError occurred when tried to access '/usr/local/Cellar/tesseract/4.1.1/share/tessdata/'

我正在学习如何使用 tesseract,我刚刚使用 homebrew 安装了 tesseract,使用 pip 安装了 pytesseract。
我的代码如下所示:

pytesseract.pytesseract.tesseract_cmd = "/usr/local/Cellar/tesseract/4.1.1/share/tessdata/"

#...

但是当我 运行 它时,我得到了这个错误:

PermissionError: [Errno 13] Permission denied: '/usr/local/Cellar/tesseract/4.1.1/share/tessdata/'

如果您需要,我会提供更多信息。

这对我有用

try:
    from PIL import Image
except ImportError:
    import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = "/usr/local/bin/tesseract"

print(pytesseract.image_to_string(Image.open("./test.jpg")))

我已经使用 brew install tesseract 安装了 tesseract,使用 pip install pytesseract 在新的 virtualenv 中安装了 pytesseract。我对 tesseract 可执行文件 (ls -la $(which tesseract) 的许可是

lrwxr-xr-x  1 p13rr0m  admin  39 May 26  2020 /usr/local/bin/tesseract -> ../Cellar/tesseract/4.1.1/bin/tesseract    

这可能是因为 tessdata 不可执行。在自述文件中,他们说如下:

import pytesseract

# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'

变量tesseract_cmd在函数run_tesseract下的this file中使用如下:

    cmd_args = []
...
    cmd_args += (tesseract_cmd, input_filename, output_filename_base)
...
    try:
        proc = subprocess.Popen(cmd_args, **subprocess_args())
    except OSError as e:
        if e.errno != ENOENT:
            raise e
        raise TesseractNotFoundError()

异常可能是从try-except块引发的,因为你不能执行一个不可执行的文件,导致权限错误。

您应该做的是找到 tesseract 可执行文件并引用它而不是 tessdata。正如@p13rr0m 在他的 中所指出的,它可能位于 "/usr/local/bin/tesseract"。在这种情况下,您不应修改变量 tesseract_cmd,因为它最初指的是命令 tesseract,该命令已可从您的 PATH 环境变量访问。

我通过这样做解决了这个问题:

  • 像这样通过自制程序安装 tesseractbrew install tesseract

  • 创建一个新的 venv:python -m venv yourVenvName

  • 在venv里面,运行 pip install pytesseract

如果您使用 mac,则无需使用 tesseract_cmd。 并尝试是否可以在 venv.

中使用 tesseract