120

我正在尝试在 python 中运行一个基本且非常简单的代码。

from PIL import Image
import pytesseract

im = Image.open("sample1.jpg")

text = pytesseract.image_to_string(im, lang = 'eng')

print(text)

这就是它的样子,我实际上已经通过安装程序安装了 tesseract for windows。我对 Python 很陌生,我不确定如何继续?

这里的任何指导都会非常有帮助。我尝试重新启动我的 Spyder 应用程序,但无济于事。

4

30 回答 30

198

我看到步骤分散在不同的答案中。根据我最近在 Windows 上遇到此 pytesseract 错误的经验,依次编写不同的步骤以更轻松地解决错误:

1 . 使用 Windows 安装程序安装 tesseract:https ://github.com/UB-Mannheim/tesseract/wiki

2 . 请注意安装中的 tesseract 路径。此编辑时的默认安装路径是:C:\Users\USER\AppData\Local\Tesseract-OCR. 它可能会改变,所以请检查安装路径。

3 .pip install pytesseract

4 . 在调用之前在脚本中设置 tesseract 路径image_to_string

pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USER\AppData\Local\Tesseract-OCR\tesseract.exe'

于 2018-12-07T15:16:44.543 回答
87

首先你应该安装二进制文件:

在 Linux 上

sudo apt-get update
sudo apt-get install libleptonica-dev 
sudo apt-get install tesseract-ocr tesseract-ocr-dev
sudo apt-get 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'到您的脚本中。

然后你应该使用 pip 安装 python 包:

pip install tesseract
pip install tesseract-ocr

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

于 2018-09-08T03:45:11.827 回答
24

仅适用于 Windows

1 - 您需要在计算机上安装 Tesseract OCR。

从这里得到它。 https://github.com/UB-Mannheim/tesseract/wiki

下载合适的版本。

2 - 将 Tesseract 路径添加到您的系统环境。即编辑系统变量。

3 - 运行pip install pytesseractpip install tesseract

4 -每次都将这一行添加到您的 python 脚本中

pytesseract.pytesseract.tesseract_cmd = 'C:/OCR/Tesseract-OCR/tesseract.exe'  # your path may be different

5 - 运行代码。

于 2019-03-06T12:50:15.627 回答
14

此错误是因为您的计算机上未安装 tesseract。

如果您使用的是 Ubuntu,请使用以下命令安装 tesseract:

sudo apt-get install tesseract-ocr

对于 mac:

brew install tesseract
于 2020-07-09T17:32:22.027 回答
10

来自https://pypi.org/project/pytesseract/

pytesseract.pytesseract.tesseract_cmd = '<full_path_to_your_tesseract_executable>'
# Include the above line, if you don't have tesseract executable in your PATH
# Example tesseract_cmd: 'C:\\Program Files (x86)\\Tesseract-OCR\\tesseract'
于 2018-07-18T08:20:29.003 回答
7

在窗口中:

pip install tesseract

pip install tesseract-ocr

并检查存储在系统usr/appdata/local/programs/site-pakages/python/python36/lib/pytesseract/pytesseract.py文件中的文件并编译文件

于 2018-10-30T06:23:06.517 回答
5

在 Mac 上,您可以如下所示安装它。这对我有用。

brew install tesseract
于 2019-07-03T22:53:37.260 回答
3

你可以安装这个包... https://github.com/UB-Mannheim/tesseract/wiki 之后你应该去这个路径 C:\Program Files (x86)\Tesseract-OCR\ tesseract.exe 然后运行 ​​tesseract 文件. 我想这会帮助你...

于 2018-11-08T10:23:21.517 回答
3

步骤1:

根据操作系统在您的系统上安装 tesseract。最新的安装程序可以在https://github.com/UB-Mannheim/tesseract/wiki找到

第 2 步:安装以下依赖库: pip install pytesseract pip install opencv-python pip install numpy

第 3 步:示例代码

import cv2
import numpy as np
import pytesseract
from PIL import Image
from pytesseract import image_to_string

# Path of working folder on Disk Replace with your working folder
src_path = "C:\\Users\\<user>\\PycharmProjects\\ImageToText\\input\\"
# If you don't have tesseract executable in your PATH, include the 
following:
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files (x86)/Tesseract- 
OCR/tesseract'
TESSDATA_PREFIX = 'C:/Program Files (x86)/Tesseract-OCR'

def get_string(img_path):
    # Read image with opencv
    img = cv2.imread(img_path)

    # Convert to gray
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Apply dilation and erosion to remove some noise
    kernel = np.ones((1, 1), np.uint8)
    img = cv2.dilate(img, kernel, iterations=1)
    img = cv2.erode(img, kernel, iterations=1)

    # Write image after removed noise
    cv2.imwrite(src_path + "removed_noise.png", img)

    #  Apply threshold to get image with only black and white
    #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)

    # Write the image after apply opencv to do some ...
    cv2.imwrite(src_path + "thres.png", img)

    # Recognize text with tesseract for python
    result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))

    # Remove template file
    #os.remove(temp)

    return result


print('--- Start recognize text from image ---')
print(get_string(src_path + "image.png") )

print("------ Done -------")
于 2019-03-05T05:26:41.503 回答
3

在 Windows 64 位上,只需将以下内容添加到 PATH 环境变量中: "C:\Program Files\Tesseract-OCR"它就会起作用。

于 2019-11-25T16:06:18.417 回答
3

我可以通过使用 pytesseract.py 文件中的 bin/tesseract 路径更新 tesseract_cmd 变量来解决它

于 2020-04-12T01:34:02.080 回答
3

我在 Windows 上遇到了同样的问题。我试图更新 tesseract 路径的环境变量,但它不起作用。

对我有用的是修改 pytesseract.py 可以在路径C:\Program Files\Python37\Lib\site-packages\pytesseract或通常在C:\Users\YOUR USER\APPDATA\Python

我按以下方式更改了一行:

#tesseract_cmd = 'tesseract' 
#tesseract_cmd = 'C:\Program Files\Tesseract-OCR\\tesseract.exe'

请注意,我必须\在 tesseract 之前添加一个额外的内容,因为 Python 的解释与此相同\t,您将收到以下错误消息:

pytesseract.pytesseract.TesseractNotFoundError: C:\Program Files\Tesseract-OCR esseract.exe 未安装或不在您的路径中

于 2020-04-15T18:14:49.037 回答
3

对于 Linux 发行版 (Ubuntu)

尝试

sudo apt install tesseract-ocr
sudo apt install libtesseract-dev
于 2021-04-15T08:49:26.640 回答
3

蟒蛇安装:

适用于 Mac、Linux 和 Windows

在此处输入图像描述

conda-forge/packages/tesseract 4.1.1

步骤1:

conda install -c conda-forge tesseract

第 2 步:如果您还没有找到 Tesseract PATH

for r,s,f in os.walk("/"):
    for i in f:
        if "tesseract" in i:
            print(os.path.join(r,i))

例如,我的 Tesseract PATH 是 /anaconda/bin/tesseract

第 3 步:将 tesseract 添加到 PATH

pytesseract.pytesseract.tesseract_cmd = r'/anaconda/bin/tesseract'
于 2021-08-27T18:09:31.097 回答
2

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

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

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

于 2021-04-06T09:44:19.833 回答
1

您将需要安装 tesseract。

https://github.com/tesseract-ocr/tesseract/wiki

查看上面的安装文档。

于 2018-06-20T16:16:29.717 回答
1

在 windows 中,必须重定向命令路径,以进行默认的 windows tesseract 安装。

  1. 在 32 位系统中,在导入命令后添加此行。
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe' 
  1. 在 64 位系统中,改为添加此行。
 pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\tesseract.exe'
于 2019-06-28T02:14:12.407 回答
1

可能发生这种情况是因为,即使 Tesseract 安装正确,您还没有安装您的语言,就像我的情况一样。幸运的是,这很容易解决,我什至不需要弄乱tesseract_cmd.

sudo apt-get install tesseract-ocr -y
sudo apt-get install tesseract-ocr-spa -y
tesseract --list-langs

请注意,在第二行中,我们指定-spa了西班牙语。

如果安装成功,您应该获得可用语言的列表,例如:

List of available languages (3):
eng
osd
spa

我在这篇博文(西班牙语)中找到了这个。还有一篇关于在 Windows 中安装西班牙语的帖子(显然不那么容易)。

注意:由于问题使用lang = 'eng',这可能不是特定情况下的答案。但是在其他情况下可能会发生同样的错误,这就是我在这里发布答案的原因。

于 2020-07-21T10:08:58.453 回答
1

仅适用于 Windows 用户:

使用以下命令安装 tesseract:

pip install tesseract

然后将此行添加到您的代码中,请注意“\”

pytesseract.pytesseract.tesseract_cmd = "C:\Program Files (x86)\Tesseract-OCR\\tesseract.exe" 
于 2020-10-24T13:26:54.050 回答
1

tesseract仅通过安装使用它对我有用conda

conda install -c conda-forge tesseract
于 2021-03-12T11:47:06.480 回答
1

对于 Windows,只需简单的步骤:

  1. 从https://github.com/UB-Mannheim/tesseract/wiki下载 Windows 版本

  2. 安装

  3. 在您的 .py 文件中写入以下内容(检查安装位置)

     pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
     img_text = pytesseract.image_to_string(Image.open(filename))
    
于 2021-04-04T14:46:11.880 回答
0

使用以下命令安装 tesseract

pip install tesseract

于 2018-09-23T07:32:42.317 回答
0
# {Windows 10 instructions}
# before you use the script you need to install the dependence
# 1. download the tesseract from the official link:
#   https://github.com/UB-Mannheim/tesseract/wiki
# 2. install the tesseract
#   i chosed this path
#       *replace the user string in the below path with you name of user that you are using in your current machine
#       C:\Users\user\AppData\Local\Tesseract-OCR\
# 3. Install the  pillow for your python version
# * the best way for me is to install is this form(i'am using python3.7 version and in my CMD i run this version of python by     typing py -3.7):
# * if you are using another version of python first look how you start the python from you CMD
# * for some machine the run of python from the CMD is different
    # [examples]
    # =================================
    # PYTHON VERSION 3.7
    # python
    # python3.7
    # python -3.7
    # python 3.7
    # python3
    # python -3
    # python 3
    # py3.7
    # py -3.7
    # py 3.7
    # py3
    # py -3
    # py 3
    # PYTHON VERSION 3.6
    # python
    # python3.6
    # python -3.6
    # python 3.6
    # python3
    # python -3
    # python 3
    # py3.6
    # py -3.6
    # py 3.6
    # py3
    # py -3
    # py 3
    # PYTHON VERSION 2.7
    # python
    # python2.7
    # python -2.7
    # python 2.7
    # python2
    # python -2
    # python 2
    # py2.7
    # py -2.7
    # py 2.7
    # py2
    # py -2
    # py 2
    # ================================
# we are using pip to install the dependences
# because for me i start the python version 3.7 with the following line 
    # py -3.7
# open the CMD in windows machine and type the following line:
    # py -3.7 -m pip install pillow
# 4. Install the  pytesseract and tesseract for your python version
# * the best way for me is to install is this form(i'am using python3.7 version and in my CMD i run this version of python by     typing py -3.7):
# we are using pip to install the dependences
# open the CMD in windows machine and type the following lines:
    # py -3.7 -m pip install pytesseract
    # py -3.7 -m pip install tesseract


#!/usr/bin/python
from PIL import Image
import pytesseract
import os
import getpass

def extract_text_from_image(image_file_name_arg):

    # IMPORTANT
    # if you have followed my instructions to install this dependence in above text explanatin
    # for my machine is
    # if you don't put the right path for tesseract.exe the script will not work
    username = getpass.getuser()
    # here above line get the username for your machine automatically
    tesseract_exe_path_installation="C:\\Users\\"+username+"\\AppData\\Local\\Tesseract-OCR\\tesseract.exe"
    pytesseract.pytesseract.tesseract_cmd=tesseract_exe_path_installation

# specify the direction of your image files manually or use line bellow if the images are in the script directory in     folder  images
    # image_dir="D:\\GIT\\ai_example\\extract_text_from_image\\images"
    image_dir=os.getcwd()+"\\images"
    dir_seperator="\\"
    image_file_name=image_file_name_arg
    # if your image are in different format change the extension(ex. ".png")
    image_ext=".jpg"
    image_path_dir=image_dir+dir_seperator+image_file_name+image_ext

    print("=============================================================================")
    print("image used is in the following path dir:")
    print("\t"+image_path_dir)
    print("=============================================================================")

    img=Image.open(image_path_dir)
    text=pytesseract.image_to_string(img, lang="eng")
    print(text)

# change the name "image_1" whith the name without extension for your image name
# image_file_name_arg="image_1"
image_file_name_arg="image_2"
# image_file_name_arg="image_3"
# image_file_name_arg="image_4"
# image_file_name_arg="image_5"
extract_text_from_image(image_file_name_arg)

# ==================================
# CREATED BY: SHERIFI
# e-mail: sherif_co@yahoo.com
# git-link for script: https://github.com/sherifi/ai_example.git
# ==================================
于 2019-07-18T12:02:33.697 回答
0
For Ubuntu 18.04

如果您收到类似的错误

 tesseract is not installed or it's not in your path

 and 

 OSError: [Errno 12] Cannot allocate memory

这可能是交换内存分配问题的问题

你可以检查这个答案分配更多的交换内存希望有帮助:)

https://askubuntu.com/questions/920595/fallocate-fallocate-failed-text-file-busy-in-ubuntu-17-04?answertab=active#tab-top

于 2019-10-22T04:46:39.710 回答
0

这个问题已经有很多很好的答案,但我想分享一个我在无法解决“TesseractNotFound 错误:tesseract 未安装或不在您的路径中”时遇到的精彩网站,请参考此网站:https:/ /www.thetopsites.net/article/50655738.shtml

我意识到我收到了这个错误,因为我用 pip 安装了pytesseract但忘记安装二进制文件。您的机器上可能缺少 tesseract-ocr。在此处查看安装说明:https ://github.com/tesseract-ocr/tesseract/wiki

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

brew install tesseract

之后它应该运行良好!

在 Windows 10 OS 环境下,以下方法适用于我:

  1. 转到此链接并下载 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 = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe' (这是安装 Tesseract-OCR 的路径,因此请检查安装位置并相应地更新路径)

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

希望这对你有用!

于 2020-09-26T06:43:23.913 回答
0

为我工作的 UBUNTU 解决方案:

通过以下链接在 ubuntu 中安装 tesseract

https://medium.com/quantrium-tech/installing-tesseract-4-on-ubuntu-18-04-b6fcd0cbd78f

后来通过以下链接将traindata语言添加到tessdata

正方体运行错误

于 2021-01-19T09:34:13.337 回答
0

最新版本的 pip 模块 pytesseract=0.3.7 似乎存在问题。我已将其降级为 pytesseract=0.3.6 并且没有看到错误。

于 2021-01-28T10:46:16.760 回答
0

上面的提示并没有帮助我解决问题,因为在安装 pytesseract (pycharm, python 2.7) 时出现了该部分中指定的错误。奇怪的是,tesseract 从命令行工作,所以安装是正确的。

我可以按照以下步骤解决此问题:

  1. 从库下载 pytesseract.py https://github.com/madmaze/pytesseract
  2. 删除所有与解释器(2.7 和 3.*)差异相关的语法错误,包括 try catch 方法
  3. 将编辑后的脚本作为自己编写的脚本导入您的程序,并根据存储库中的建议配置 tesseract_cmd 变量。

随后,图像到文本的翻译功能在 python 2.7 中工作

于 2021-07-10T17:06:51.953 回答
0

我已经在我的树莓派上试过这个了。我只是从这里改变了路径:

C:/Program Files/Tesseract-OCR/tesseract.exe'

(因为,它适用于 Windows)对此:

/usr/local/lib/python3.7/dist-packages

因为,这是我尝试运行此命令时看到的路径:

pip3 show pytesseract

为了更清楚,这里是消息。 命令行在这里

于 2021-11-01T14:53:09.633 回答
0

在 Windows 中安装 tesseract 时,我也面临同样的错误。

根据我最近解决的问题,我正在按照以下步骤进行操作

  1. 使用 gievn 链接中提供的 Windows 安装程序安装 tesseract:https ://github.com/UB-Mannheim/tesseract/wiki

  2. 请注意安装中的 tesseract 路径。此编辑时的默认安装路径为:C:\Users\USER\AppData\Local\Tesseract-OCR。它可能会改变,所以请检查安装路径。

安装后,它仍然显示错误或未安装您面临的错误然后按 windows + R 键并运行您的文件路径(C:\Program Files\Tesseract-OCR\tesseract.exe)它会为我工作,

3. pip install pytesseract

使用命令提示符

  1. 在调用```image_to_string:``之前在脚本中设置tesseract路径

  2. 对于 Windows 文件路径 -

pytesseract.pytesseract.tesseract_cmd=r'C:\Program Files(x86)\Tesseract-OCR\tesseract.exe'
  1. 要安装opencv,请参考这个问题链接

  2. 对于 linux 安装

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

$ tesseract --version

2.运行此命令后,您应该是这样的:

tesseract 4.0.0-beta.1 
 leptonica-1.75.3

3.一旦你的tesseract安装成功,你可以运行以下命令来检查

$ tesseract --list-langs

4.您可以期待以下输出:

List of available languages (2):
eng 
osd

5.linux文件路径如下

pytesseract.pytesseract.tesseract_cmd = r'home/user/bin/tesseract'
于 2022-02-03T16:25:59.620 回答