0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pyocrで NameError: name 'tools' is not defined が発生する

0
Posted at

この記事は過去のエラー解決メモを整理したものです。
現在の推奨手順とは異なる可能性があります。
公式ドキュメントを確認して最新情報と差分がないかを確認してください。

事象

Tesseract OCRをPythonから使おうとしたところ、tools が定義されていないというエラーが発生した。

Microsoft Windows [Version 10.0.19043.1288]
(c) Microsoft Corporation. All rights reserved.

D:\programs>D:/python/python.exe
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> if len(tools) == 0:
...   print("No OCR tool found")
...   sys.exit(1)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'tools' is not defined

環境

  • Windows 10
  • Python 3.9.6
  • Tesseract OCR
  • pyocr
  • VSCode 1.62.0

原因

Windowsのコンソールと実行環境の文字コードが一致していなかったため。?

対策

まず、次のコードで利用可能なOCRツールを取得しようとした。

from PIL import Image
import sys
import pyocr
import pyocr.builders
import matplotlib.pyplot as plt

tools = pyocr.get_available_tools()
tools
  • 下のように
    pyocr.tesseract.TESSERACT_CMD にTesseract OCRの実行ファイルパスを指定する。tesseract.exe のパスは、インストールした場所に合わせて変更する。
from PIL import Image
import sys
import pyocr
import pyocr.builders
import matplotlib.pyplot as plt

pyocr.tesseract.TESSERACT_CMD = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
tools = pyocr.get_available_tools()
tools

参考情報

pyocrのget_available_toolsが取得できない|teratail

pyocr.get_available_toolsがうまくいきません|teratail

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?