【问题标题】:Pytesseract : "TesseractNotFound Error: tesseract is not installed or it's not in your path", how do I fix this?Pytesseract:“TesseractNotFound 错误:tesseract 未安装或不在您的路径中”,我该如何解决?
【发布时间】:2018-11-29 19:22:58
【问题描述】:

我正在尝试在 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 应用程序,但无济于事。

【问题讨论】:

  • 什么不起作用?您可以添加您在问题中遇到的错误吗?

标签: python tesseract


【解决方案1】:

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

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

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

3pip install pytesseract

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

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

【讨论】:

  • 这对我有用!如果其他人在查找“Tesseract-OCR”文件夹时遇到问题,请在“C:\Program Files\”文件夹中搜索
  • 你是互联网的英雄
  • 必须重新启动计算机才能启动环境设置
  • 这个答案比the documentation好,因为tesseract_cmd的路径确实需要指向tesseract.exe。文档中缺少此内容。
  • pytesseract.pytesseract.tesseract_cmd = r'C:\Users\USER\AppData\Local\Tesseract-OCR\tesseract.exe' 这个答案使我免于计算机视觉的重要截止日期 - OCR 项目非常感谢@Nafeez Quraishi :-)
【解决方案2】:

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

在 Linux 上

sudo apt-get update
sudo apt-get install libleptonica-dev tesseract-ocr tesseract-ocr-dev libtesseract-dev python3-pil tesseract-ocr-eng tesseract-ocr-script-latn

在 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://tesseract-ocr.github.io/tessdoc/Installation.html

【讨论】:

  • 2021 年 8 月更新,对于 linux,我需要运行 sudo apt-get install python3-pil tesseract-ocr libtesseract-dev tesseract-ocr-eng tesseract-ocr-script-latn。第二个参考(github)已经没有了,我参考了tesseract-ocr.github.io/tessdoc/Installation.html
  • 如果您遇到此问题tesseract: symbol lookup error: /usr/lib/x86_64-linux-gnu/libtesseract.so.4: undefined symbol: pixaDisplayTiledInColumns Package tesseract-ocr-dev is not available, but is referred to by another package. ,请执行此操作sudo apt install libtesseract-dev libleptonica-dev liblept5
  • @David17 在 Ubuntu 20.04 LTS (2022/03) 上为我工作。
【解决方案3】:

仅适用于 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 - 运行代码。

【讨论】:

  • 在哪里可以找到 Linux 的路径?
  • @Pallavi 这是一个 windows 的答案,所以请不要在这里使用 Linux。第 2 步似乎不需要因为第 4 步。
【解决方案4】:

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

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

sudo apt-get install tesseract-ocr

Mac 版:

brew install tesseract

【讨论】:

  • 这对我有用。安装 pytesseract 后必须这样做
【解决方案5】:

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

【讨论】:

    【解决方案6】:

    在窗口中:

    pip install tesseract
    
    pip install tesseract-ocr
    

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

    【讨论】:

    • 请注意,这在 Windows 上不起作用,您需要安装二进制文件(来自 github.com/tesseract-ocr/tesseract/wiki)并添加行 pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe" 以使 tesseract 工作。
    【解决方案7】:

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

    brew install tesseract
    

    【讨论】:

      【解决方案8】:

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

      【讨论】:

        【解决方案9】:

        第 1 步:

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

        第 2 步: 使用安装以下依赖库: 点安装 pytesseract 点安装 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 -------")
        

        【讨论】:

          【解决方案10】:

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

          【讨论】:

            【解决方案11】:

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

            【讨论】:

              【解决方案12】:

              我在 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 未安装或不在您的路径中

              【讨论】:

                【解决方案13】:

                适用于 Linux 发行版 (Ubuntu)

                试试

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

                【讨论】:

                • 这项工作我还使用 pip3 install tesseract 安装了 tesseract。
                【解决方案14】:

                Anaconda 安装:

                适用于 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'
                

                【讨论】:

                  【解决方案15】:

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

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

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

                  【讨论】:

                    【解决方案16】:

                    您需要安装 tesseract。

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

                    查看以上安装文档。

                    【讨论】:

                    • 嗨,我已经尝试过这样做,我使用 Windows 安装程序从 github 安装,但它仍然无法正常工作。不知道为什么或什么,我认为它与路径有关?我再去看看wiki
                    【解决方案17】:

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

                    【讨论】:

                      【解决方案18】:

                      可能发生这种情况是因为,即使 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',因此在特定情况下这可能不是答案。但是在其他情况下可能会发生同样的错误,这就是我在这里发布答案的原因。

                      【讨论】:

                        【解决方案19】:

                        仅适用于 Windows 用户:

                        使用以下方式安装 tesseract:

                        pip install tesseract
                        

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

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

                        【讨论】:

                          【解决方案20】:

                          只需使用conda 安装tesseract,它就对我有用。

                          conda install -c conda-forge tesseract
                          

                          【讨论】:

                            【解决方案21】:

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

                            【讨论】:

                              【解决方案22】:

                              使用以下命令安装tesseract

                              pip install tesseract

                              【讨论】:

                                【解决方案23】:
                                # {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
                                # ==================================
                                

                                【讨论】:

                                • 您可以在您发布的 sn-p 中添加文字说明吗?
                                • 我在 sn-p 中添加了文本,以便更轻松地访问和完成答案
                                【解决方案24】:
                                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

                                【讨论】:

                                  【解决方案25】:

                                  这个问题已经有很多不错的答案,但是我想分享一个很棒的网站,当我无法解决“TesseractNotFound Error: tesseract is not installed or it's not in your path”时遇到的问题请参考这个网站: 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/

                                  希望它对你有用!

                                  【讨论】:

                                    【解决方案26】:

                                    适用于我的 UBUNTU 解决方案:

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

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

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

                                    Tesseract running error

                                    【讨论】:

                                      【解决方案27】:

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

                                      【讨论】:

                                        【解决方案28】:

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

                                        我可以通过以下步骤解决此问题:

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

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

                                        【讨论】:

                                          【解决方案29】:

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

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

                                          (因为是windows)至此:

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

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

                                          pip3 show pytesseract
                                          

                                          为了更清楚,这里是消息。 Command line here

                                          【讨论】:

                                            【解决方案30】:

                                            在 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请参考question link

                                            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'
                                            

                                            【讨论】:

                                              猜你喜欢
                                              • 2019-01-11
                                              • 1970-01-01
                                              • 2020-04-10
                                              • 1970-01-01
                                              • 2016-05-24
                                              • 2017-06-14
                                              • 1970-01-01
                                              • 2014-08-23
                                              • 2015-07-10
                                              相关资源
                                              最近更新 更多