78

我正在尝试在 Python 中使用 pytesseract,但我总是遇到以下错误:

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

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

产生此错误的示例代码:

import cv2
import pytesseract

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

如何解决这个 TesseractNotFoundError?

4

25 回答 25

92

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

在 Linux 上

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

在 Mac 上

brew install tesseract

在 Windows 上

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

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

于 2018-09-08T16:49:27.747 回答
51

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

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

于 2018-08-02T20:59:15.157 回答
20

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

在 Mac 上,您可以使用 homebrew 安装:

brew install tesseract

之后它应该运行良好

于 2018-06-11T09:49:31.593 回答
15

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

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

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

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

希望这对你有用!

于 2019-01-15T12:55:35.433 回答
11

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

对于 Mac 操作系统

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

>brew list tesseract

这应该列出你的 tesseract.exe 在哪里,或多或少像

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

然后按照他们的指示

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'

应该做的伎俩!

于 2018-07-29T19:07:21.527 回答
10

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

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe'
于 2019-01-18T07:48:06.667 回答
9

注意:仅适用于 WINDOWS


我今天遇到了这个问题,这里提到的所有答案都对我有帮助,但我个人不得不挖掘很多来解决它。因此,让我以一种非常简单的形式提出解决方案来帮助所有其他人:

  1. 从此处下载可执行的 64 位(如果您的计算机是 32 位,则为 32 位)exe 。

    (文件名称为 tesseract-ocr-w64-setup-v5.0.0.20190526 (alpha))

  1. 安装它。让它自己安装在默认的 C 目录中。

  2. 现在转到您的环境变量(只需在开始菜单中搜索即可到达那里或转到Control Panel > System > Advanced System Settings > Environment Variables

a) 选择 PATH,然后编辑它。单击 NEW 并添加安装路径(通常C:\Program Files\Tesseract-OCR\

现在你不会得到错误!

于 2019-06-12T09:52:44.337 回答
8

对于 Mac:

  1. 安装 Pytesseract ( pip install pytesseract应该可以工作)
  2. 安装 Tesseract但只能使用自制软件,pip 安装不知何故不起作用。(酿造安装 tesseract )
  3. 在您的设备上获取 Tesseract 的 brew 安装路径(brew list tesseract
  4. 将路径添加到您的代码中,而不是 sys path中。路径将与代码一起添加,使用 pytesseract.pytesseract.tesseract_cmd = '<在步骤 3 中收到的路径>' - (例如pytesseract.pytesseract.tesseract_cmd = '/usr/local/Cellar/tesseract/4.0.0_1/bin /正方体' )

这应该可以正常工作。

于 2019-05-24T03:49:32.207 回答
7

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

sudo apt install tesseract-ocr

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

于 2018-11-20T06:33:49.680 回答
5

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

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

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

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)

请享用!

于 2018-06-12T08:05:34.760 回答
4

当我尝试使用 pytesseract 制作文本提取器时,我也遇到了同样的错误,但是解决方案 在 pypi 站点中pytesseract的安装说明中: pytesseract 有很多替代方法可以避免该错误,但是,再添加一个参数在方法pytesseract.image_to_string为我解决了它,比如

tessdata_dir_config = "/usr/share/tesseract-ocr/4.00/tessdata"
output = pytesseract.image_to_string(image, lang='eng', config=tessdata_dir_config)
于 2019-06-14T21:11:18.200 回答
4

以下三个命令将做必要的:

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
于 2019-03-04T08:33:12.683 回答
3

您可以使用以下链接下载 tesseract-ocr 设置,

用于 windows 的正方体

然后在值为 C:\Program Files (x86)\Tesseract-OCR\tesseract.exe 的环境变量中添加名称为 tesseract 的新变量

于 2018-09-23T07:41:08.443 回答
2

当当前目录位于与安装 tesseract 的驱动器不同的驱动器上时,这会在 windows 下(至少在 tesseract 版本 3.05 中)发生。

tesseract 中的某些东西期望数据文件位于 \Program Files... 中(而不是 C:\Program Files,比如说)。因此,如果您与 tesseract 不在同一个驱动器号上,它将失败。如果我们可以通过在执行 tesseract 之前将驱动器(仅在 windows 下)临时更改为 tesseract 安装驱动器并在之后更改回来来解决这个问题,那就太好了。您的示例:您可以将 yourmodule_python.py 复制到“C/Program Files (x86)/Tesseract-OCR/”并运行!

于 2019-07-04T09:52:35.010 回答
2

从https://github.com/UB-Mannheim/tesseract/wiki安装 tesseract并将 tesseract.exe 的路径添加到Path环境变量中。

于 2018-06-06T12:37:25.017 回答
2

设置路径的步骤很少

1:转到此“ https://github.com/UB-Mannheim/tesseract/wiki

2:下载最新的安装程序

3:安装

4:在“C:\Program Files\Tesseract-OCR”或“C:\ProgramFiles (x86)\Tesseract-OCR”等系统变量中设置路径

5 : 打开 CMD 类型 "tesseract" 和一些输出除了 "not regonized type errors"

于 2019-11-29T18:51:32.973 回答
2

很可能您安装了不同版本的 Python,请确保安装的 Tesseract 使用相同的 Python 版本。

which pip3显示 pip3 安装的路径,并which python3显示 Python 安装的相应路径。

确保这两个相同。

于 2019-04-25T19:17:59.137 回答
2

在烧瓶网络框架下的Ubuntu上,这应该可以工作

pytesseract.pytesseract.tesseract_cmd = r"/usr/bin/tesseract"
img = Image.open(picture_name)
print(pytesseract.image_to_string(img))
于 2020-12-03T07:26:50.940 回答
0

这是我的道路

pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"  # your path may be different
于 2021-12-01T11:05:51.673 回答
0

我也面临同样的问题,只需添加C:\Program Files (x86)\Tesseract-OCR到您的路径变量。如果它仍然不起作用,C:\Program Files (x86)\Tesseract-OCR\tessdata请在新行中添加到您的路径变量。并且不要忘记在添加路径变量后重新启动计算机。

于 2018-06-14T10:59:49.477 回答
0

小错误——我知道我必须打开/关闭我的 cmd 才能获得反映的更新路径。使用 Jupyter Notebook 我不得不关闭客户端并重新初始化它。

于 2018-08-13T20:01:56.867 回答
0

你在进口吗

from tesseract import image_to_string

不要从 pytesseract 导入

于 2018-06-02T14:39:32.660 回答
0

对我来说,它通过单引号起作用

pytesseract.pytesseract.tesseract_cmd =r'C:/Program Files/Tesseract-OCR/tesseract.exe'

实际上放在双引号内会自动插入不需要的字符

于 2021-04-06T09:33:00.770 回答
0

我目前使用的是 Windows,需要开发一个 PDF 解析器,但sysdm.cpl单独添加一个新的环境变量不起作用。对于其他 Windows 用户,我强烈建议C:\Program Files (x86)\Tesseract-OCRprofile.ps1也添加(如果使用的是 Powershell)。

于 2018-07-02T13:08:30.650 回答
0

如果您使用的是 linux,只需运行这些命令,

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

然后运行这个,

python -m pip install tesseract tesseract-ocr pytesseract
于 2020-11-12T12:14:05.290 回答