5

私はpython 3.xを使用しており、次のコードを使用して画像をテキストに変換しています:

from PIL import Image
from pytesseract import image_to_string

image = Image.open('image.png', mode='r')
print(image_to_string(image))

次のエラーが表示されます。

Traceback (most recent call last):
  File "C:/Users/hp/Desktop/GII/Image_to_text.py", line 12, in <module>
    print(image_to_string(image))
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 161, in image_to_string
    config=config)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\site-packages\pytesseract\pytesseract.py", line 94, in run_tesseract
    stderr=subprocess.PIPE)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 950, in __init__
    restore_signals, start_new_session)
  File "C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\lib\subprocess.py", line 1220, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

私のpythonが存在するのと同じディレクトリに画像を置いたことに注意してください。また、エラーは発生しません image = Image.open('image.png', mode='r')が、行では発生しprint(image_to_string(image))ます。

ここで何が間違っているのでしょうか?ありがとう

4

5 に答える 5

7

tesseractパスにインストールしてアクセス可能にする必要があります。

sourceによると、実行するバイナリとして tesseract バイナリを使用するpytesseractための単なるラッパーです。subprocess.PopenOCR自体は一切実行しません。

ソースの関連部分:

def run_tesseract(input_filename, output_filename_base, lang=None, boxes=False, config=None):
    '''
    runs the command:
        `tesseract_cmd` `input_filename` `output_filename_base`

    returns the exit status of tesseract, as well as tesseract's stderr output
    '''
    command = [tesseract_cmd, input_filename, output_filename_base]

    if lang is not None:
        command += ['-l', lang]

    if boxes:
        command += ['batch.nochop', 'makebox']

    if config:
        command += shlex.split(config)

    proc = subprocess.Popen(command,
            stderr=subprocess.PIPE)
    return (proc.wait(), proc.stderr.read())

ソースの別の部分を引用:

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
tesseract_cmd = 'tesseract'

tesseractパスを変更する簡単な方法は次のとおりです。

import pytesseract
pytesseract.tesseract_cmd = "/absolute/path/to/tesseract"  # this should be done only once 
pytesseract.image_to_string(img)
于 2016-07-21T16:07:22.207 に答える
1

tesseract OCR セットアップもダウンロードする必要があります。このリンクを使用してセットアップをダウンロードします: http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.01.exe

次に、コードに次の行を含めて、tesseract 実行可能ファイルを使用します: pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract'

これは、tesseract がインストールされるデフォルトの場所です。

それでおしまい。また、これらの手順に従って、最後にコードを実行しました。

これが役立つことを願っています。

于 2017-09-12T17:19:42.653 に答える
0

あなたの「現在の」ディレクトリは、あなたが考えている場所ではありません。

==> イメージへのフル パスを指定できます。例: image = Image.open(r'C:\Users\hp\Downloads\WinPython-64bit-3.5.1.2\python-3.5.1.amd64\image .png', モード='r')

于 2016-07-21T16:04:09.350 に答える
0

この python ライブラリを使用して試すことができます: https://github.com/prabhakar267/ocr-convert-image-to-text

パッケージの README に記載されているように、使用方法は非常に簡単です。

usage: python main.py [-h] input_dir [output_dir]

positional arguments:
  input_dir
  output_dir

optional arguments:
  -h, --help  show this help message and exit
于 2018-10-18T22:48:50.493 に答える