Python 如何解决 TesseractNotFoundError?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50655738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:34:57  来源:igfitidea点击:

How do I resolve a TesseractNotFoundError?

pythonpython-3.xtesseractpython-tesseract

提问by PreetyP

I am trying to use pytesseract in Python but I always end up with the following error:

我正在尝试在 Python 中使用 pytesseract,但最终总是出现以下错误:

    raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path

However, pytesseract and Tesseract are installed on my system.

但是,pytesseract 和 Tesseract 安装在我的系统上。

Example code that produces this error:

产生此错误的示例代码:

import cv2
import pytesseract

img = cv2.imread('1d.png')
print(pytesseract.image_to_string(img))

How do I resolve this TesseractNotFoundError?

我该如何解决这个 TesseractNotFoundError?

回答by Ben Hooper

I tried adding to the path variable like others have mentioned, but still received the same error. what worked was adding this to my script:

我尝试像其他人提到的那样添加到路径变量,但仍然收到相同的错误。有效的是将它添加到我的脚本中:

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe"

回答by Ali

I got this error because I installed pytesseractwith pipbut forget to install the binary.

我收到此错误是因为我安装pytesseractpip但忘记安装二进制文件。

On Linux

在 Linux 上

sudo apt update
sudo apt install tesseract-ocr
sudo apt install libtesseract-dev

On Mac

在 Mac 上

brew install tesseract

On Windows

在 Windows 上

download binary from https://github.com/UB-Mannheim/tesseract/wiki. then add pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'to your script. (replace path of tesseract binary if necessary)

https://github.com/UB-Mannheim/tesseract/wiki下载二进制文件。然后添加pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'到您的脚本中。(如有必要,替换 tesseract 二进制文件的路径)

references: https://pypi.org/project/pytesseract/(INSTALLATION section) and https://github.com/tesseract-ocr/tesseract/wiki#installation

参考资料:https: //pypi.org/project/pytesseract/(安装部分)和 https://github.com/tesseract-ocr/tesseract/wiki#installation

回答by KornholioBeavis

You are probably missing tesseract-ocrfrom your machine. Check the installation instructions here: https://github.com/tesseract-ocr/tesseract/wiki

tesseract-ocr的机器可能丢失了。在此处查看安装说明:https: //github.com/tesseract-ocr/tesseract/wiki

On a Mac, you can just install using homebrew:

在 Mac 上,您可以使用自制软件进行安装:

brew install tesseract

brew install tesseract

It should run fine after that

之后它应该运行良好

回答by ahbon

Under Windows 10 OS environment, the following method works for me:

在 Windows 10 操作系统环境下,以下方法对我有用:

  1. https://github.com/tesseract-ocr/tesseract/wikiDownload tesseract and install it. Windows version is available here: https://github.com/UB-Mannheim/tesseract/wiki

  2. Find script file pytesseract.py from C:\Users\User\Anaconda3\Lib\site-packages\pytesseract and open it. Change the following code from tesseract_cmd = 'tesseract'to: tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'

  3. You may also need add environment variable D:/Program Files (x86)/Tesseract-OCR/

  1. https://github.com/tesseract-ocr/tesseract/wiki下载tesseract并安装。Windows 版本可在此处获得:https: //github.com/UB-Mannheim/tesseract/wiki

  2. 从 C:\Users\User\Anaconda3\Lib\site-packages\pytesseract 中找到脚本文件 pytesseract.py 并打开它。将以下代码从更改tesseract_cmd = 'tesseract'为:tesseract_cmd = 'D:/Program Files (x86)/Tesseract-OCR/tesseract.exe'

  3. 您可能还需要添加环境变量 D:/Program Files (x86)/Tesseract-OCR/

Hope it works for you!

希望这对你有用!

回答by evanca

One simple thing that actually worked for me in Jupyter Notebook, was using double backslashinstead of a single backslash in the pytesseract.pytesseract.tesseract_cmd path:

在 Jupyter Notebook 中对我有用的一件简单的事情是在 pytesseract.pytesseract.tesseract_cmd 路径中使用双反斜杠而不是单个反斜杠:

pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe'

回答by Louis Lin

I'm running on a Mac OS and installed tesseract with brew so here's my take on this. Since pytesseract is just how you can access tesseract from python, you have to specify where tesseract is already on your computer.

我在 Mac OS 上运行并使用 brew 安装了tesseract,所以这是我的看法。由于 pytesseract 正是您从 python 访问 tesseract 的方式,因此您必须指定 tesseract 在您的计算机上的位置。

For Mac OS

对于 Mac 操作系统

Try finding where the tesseract.exe is- if you installed it using brew, on your the terminal use:

尝试找到 tesseract.exe 的位置 - 如果您使用brew安装它 ,请在终端上使用:

>brew list tesseract

This should list where your tesseract.exe is, somewhere more or less like

这应该列出您的 tesseract.exe 所在的位置,或多或少像

> /usr/local/Cellar/tesseract/3.05.02/bin/tesseract

Then following their instructions:

然后按照他们的指示

pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'

pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/3.05.02/bin/tesseract'

pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/3.05.02/bin/tesseract'

should do the trick!

应该做的伎俩!

回答by Mihir Verma

For Mac:

对于 Mac:

  1. Install Pytesseract (pip install pytesseractshould work)
  2. Install Tesseract but only with homebrew, pip installation somehow doesn't work. (brew install tesseract)
  3. Get the path of brew installation of Tesseract on your device (brew list tesseract)
  4. Add the path into your code, not in sys path. The path is to be added along with code, using pytesseract.pytesseract.tesseract_cmd = '<path received in step 3>' - (e.g. pytesseract.pytesseract.tesseract_cmd = '/usr/local/Cellar/tesseract/4.0.0_1/bin/tesseract')
  1. 安装 Pytesseract(pip install pytesseract应该可以工作)
  2. 安装 Tesseract但只能使用自制软件,pip 安装不知何故不起作用。( brew install tesseract)
  3. 在你的设备上获取 Tesseract 的 brew 安装路径(brew list tesseract
  4. 将路径添加到您的代码中,而不是在 sys path 中。路径将与代码一起添加,使用 pytesseract.pytesseract.tesseract_cmd = '<path received in step 3>' - (例如pytesseract.pytesseract.tesseract_cmd = '/usr/local/Cellar/tesseract/4.0.0_1/bin /tesseract')

This should work fine.

这应该可以正常工作。

回答by Vivekanand Panda

I face this same issue. I just use this command that will help me.

我面临同样的问题。我只是使用这个可以帮助我的命令。

sudo apt install tesseract-ocr

Note that this will only work on Ubuntu.
sudois a Unix exclusive command (Linux, Mac, Rasbian, etc.) while aptis Ubuntu specific.

请注意,这仅适用于 Ubuntu。
sudo是 Unix 专有命令(Linux、Mac、Rasbian 等),而apt特定于 Ubuntu。

回答by aniket

I faced the same problem. I hope you have installed from hereand have also done pip install pytesseract.

我遇到了同样的问题。我希望你已经从这里安装并完成了pip install pytesseract.

If everything is fine you should see that the path C:\Program Files (x86)\Tesseract-OCR where tesseract.exeis available.

如果一切正常,您应该看到路径C:\Program Files (x86)\Tesseract-OCR where tesseract.exe可用。

Adding Path variable did not helped me, I actually added new variable with name tesseractin environment variables with a value of C:\Program Files (x86)\Tesseract-OCR\tesseract.exe.

添加 Path 变量对我没有帮助,我实际上tesseract在环境变量中添加了名称为C:\Program Files (x86)\Tesseract-OCR\tesseract.exe.

Typing tesseractin the command line should now work as expected by giving you usage informations. You can now use pytesseractas such (don't forget to restart your python kernel before running this!):

tesseract通过为您提供使用信息,在命令行中键入现在应该可以按预期工作。你现在可以pytesseract这样使用(在运行之前不要忘记重新启动你的python内核!):

import pytesseract
from PIL import Image

value=Image.open("text_image.png")
text = pytesseract.image_to_string(value, config='')    
print("text present in images:",text)

enjoy!

请享用!

回答by Shivam Bharadwaj

The following three commands will do the needful :

以下三个命令将完成必要的工作:

sudo apt update
# This will update your packages
sudo apt install tesseract-ocr
# This will install OCR
sudo apt install libtesseract-dev
# This will add it as development dependency