python - Pytesseract: "TesseractNotFound Error:tesseract is not installed or it's in your path"、どうすれば修正できますか?

Translate

私はPythonで基本的で非常に単純なコードを実行しようとしています。

from PIL import Image
import pytesseract

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

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

print(text)

これはどのように見えるかです、私は実際にインストーラーを通してウィンドウズのためのtesseractをインストールしました。私はPythonを初めて使用しますが、どのように進めればよいかわかりません。

ここでのガイダンスは非常に役立ちます。 Spyderアプリケーションを再起動しようとしましたが、役に立ちませんでした。

This question and all comments follow the "Attribution Required."

すべての答え

Translate

ステップはさまざまな答えに散らばっています。 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'

ソース
Ali
Translate

まず、バイナリをインストールする必要があります。

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

ソース
Translate

Windowsのみ

1-コンピュータにTesseractOCRをインストールする必要があります。

ここから入手してください。https://github.com/UB-Mannheim/tesseract/wiki

適切なバージョンをダウンロードします。

2-システム環境にTesseractパスを追加します。つまり、システム変数を編集します。

3-実行pip install pytesseractそしてpip install tesseract

4-毎回この行をPythonスクリプトに追加します

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

5-コードを実行します。

ソース
Translate

から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'
ソース
Translate

Windowsの場合:

pip install tesseract

pip install tesseract-ocr

システムに保存されているファイルを確認し、usr / appdata / local / programs / site-pakages / python / python36 / lib / pytesseract /pytesseract.pyファイルをコンパイルします。

ソース
Translate

Macでは、以下のようにインストールできます。これは私にとってはうまくいきます。

brew install tesseract
ソース
Translate

このパッケージをインストールできます...https://github.com/UB-Mannheim/tesseract/wikiその後、このパスC:\ Program Files(x86)\ Tesseract-OCR \ tesseract.exeに移動し、tesseractファイルを実行する必要があります。これはあなたを助けると思います...

ソース
Translate

pytesseract.pyファイルのbin / tesseractパスでtesseract_cmd変数を更新することで解決できます

ソース
Translate

tesseractをインストールする必要があります。

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

インストールに関する上記のドキュメントを確認してください。

ソース
Translate

Windows 64ビットでは、PATH環境変数に以下を追加するだけです。"C:\Program Files\Tesseract-OCR"そしてそれは動作します。

ソース
Translate

次のコマンドを使用して、tesseractをインストールします

pip install tesseract

ソース
Translate

ステップ1:

OSに従ってシステムに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 -------")
ソース
Translate
# {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
# ==================================
ソース
Translate

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'
ソース
Translate
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

ソース
Translate

Windowsでも同じ問題が発生しました。動作しなかった正八胞体のパスの環境変数を更新しようとしました。

私のために働いたのは、パスにあるpytesseract.pyを変更することでしたC:\Program Files\Python37\Lib\site-packages\pytesseractまたは通常C:\Users\YOUR USER\APPDATA\Python

以下のように1行変更しました。

#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がインストールされていないか、パスにありません

ソース
Leave a Reply
You must be logged in to post a answer.
著者について
同様の問題