Pytesseract:“ TesseractNotFound错误:tesseract未安装或不在您的路径中”,如何解决此问题?

时间:2018-06-20 15:32:39

标签: python tesseract

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

from PIL import Image
import pytesseract

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

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

print(text)

这是它的外观,我实际上已经通过安装程序为Windows安装了tesseract。我是Python的新手,不确定如何继续?

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

27 个答案:

答案 0 :(得分:10)

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

1 。使用https://github.com/UB-Mannheim/tesseract/wiki

上的Windows安装程序安装tesseract

2 。请注意安装中的tesseract路径。默认安装路径为:C:\Program Files (x86)\Tesseract-OCR

3 pip install pytesseract

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

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

答案 1 :(得分:4)

在Windows中:

点安装tesseract

pip安装tesseract-ocr

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

答案 2 :(得分:3)

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

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

sudo apt-get install tesseract-ocr

对于Mac:

brew install tesseract

答案 3 :(得分:3)

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

答案 4 :(得分:2)

在Linux上安装:

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

答案 5 :(得分:2)

我在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'

请注意,由于Python与\的解释相同,因此在tesseract之前我必须多加一个\t,您会收到以下错误消息:

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

答案 6 :(得分:2)

仅适用于Windows


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

  

从这里获取。   https://github.com/UB-Mannheim/tesseract/wiki

     

下载合适的版本。

2-将tesseract路径保存到系统环境。即编辑系统变量。

3-点安装pytesseract和点安装tesseract

4-将此行添加到您的python脚本

  

pytesseract.pytesseract.tesseract_cmd = 'C:\\OCR\\Tesseract-OCR\\tesseract.exe'   ^您的路径可能不同。

5-输入代码错误。

答案 7 :(得分:1)

来自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'

答案 8 :(得分: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

我在this blog post(西班牙语)找到了它。还有installation of Spanish language in Windows的帖子(显然不太容易)。

注意:由于该问题使用lang = 'eng',因此在特定情况下可能不是答案。但是在其他情况下也可能发生相同的错误,这就是为什么我在此处发布答案的原因。

答案 9 :(得分:1)

它确实对我有用,只需使用 tesseract 安装 conda

conda install -c conda-forge tesseract

答案 10 :(得分:1)

对于 Linux 发行版 (Ubuntu)

试试

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

答案 11 :(得分:1)

您将需要安装tesseract。

  

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

查看有关安装的上述文档。

答案 12 :(得分:1)

您可以安装此软件包... https://github.com/UB-Mannheim/tesseract/wiki 之后,您应该使用以下路径C:\ Program Files(x86)\ Tesseract-OCR \ tesseract.exe 然后运行tesseract文件。 我认为这会帮助您...

答案 13 :(得分:1)

使用以下命令安装tesseract

pip install tesseract

答案 14 :(得分:0)

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

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

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

答案 15 :(得分:0)

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))
    

答案 16 :(得分:0)

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

答案 17 :(得分: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 中工作

答案 18 :(得分:0)

适用于 UBUNTU 的解决方案对我有用:

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

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

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

Tesseract running error

答案 19 :(得分:0)

仅适用于Windows用户:

使用以下命令安装tesseract:

pip install tesseract

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

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

答案 20 :(得分:0)

已经有很多不错的解决方案,但是我想分享一个很棒的网站,当我无法解决“ TesseractNotFound错误:tesseract未安装或不在您的路径中”时,请访问此网站: https://www.thetopsites.net/article/50655738.shtml

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

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

brew install tesseract

之后应该可以正常运行!

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

  1. 转到this link并下载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 /

希望它对您有用!

答案 21 :(得分:0)

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

答案 22 :(得分: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

答案 23 :(得分: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
# ==================================

答案 24 :(得分:0)

在Mac上(对我有用)

brew install tesseract

答案 25 :(得分:0)

在Windows中,对于默认的Windows tesseract安装,必须重定向命令路径。

  1. 在32位系统中,在导入命令后添加此行。

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

  2. 在64位系统中,请添加此行。

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

答案 26 :(得分:0)

步骤1:

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

中找到

步骤2: 使用安装以下依赖库:    点安装pytesseract    pip安装opencv-python    点安装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 -------")
相关问题