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?

More than 1 year has passed since last update.

sesseract OCRにて画像からの文字認識を試したメモ

Last updated at Posted at 2023-10-22

Anacondaにて tesseract-ocr を試したメモ。

環境

Anacondaを使って、Opencvも入れている前提だが、
TesseractというオープンソースのOCRエンジンと、それをPythonで使えるようにしたライブラリであるpytesseractを使用します。

それぞれAnaconda.orgで調べたコマンドにて仮想環境にインストールする

conda install -c conda-forge pytesseract

すぐにインストールできた。

conda install -c conda-forge tesseract

試したが、tesseractはインストール完了できず、exeでインストールする方法に作戦変更。手順は以下。

1.download,install
https://github.com/UB-Mannheim/tesseract/wiki

からtesseractのセットアップ.exeファイルを直接downloadしてインストールする方法に変更。
tesseract-ocr-w64-setup-5.3.3.20231005.exe (64 bit)をDownloadして、ガイダンス通りに言語を選択した上、インストール。

スクリーンショット 2023-10-22 130213.png

2.環境変数を設定
pathの中に"C:\Program Files\Tesseract-OCR"を追加する

スクリーンショット 2023-10-22 133407.png

TESSDATA_PREFIX変数を新規作り、"C:\Program Files\Tesseract-OCR\tessdata"を追加しておく。
スクリーンショット 2023-10-22 133331.png

3.環境変数設定できたかを試す。AnacondaでTesseractを認識できたかをチェック。
スクリーンショット 2023-10-22 133723.png
このようにVersionがちゃんと表示できたらOK。そうじゃなければ、内部コマンドじゃないと弾かれる。

4.最後、pytesseractのpytesseract.py中身を変える。tesseract_cmd = ‘tesseract’,をtesseract_cmd =r’C:\Program Files\Tesseract-OCR\tesseract.exe’に直せば、importできるようになるはず。

スクリーンショット 2023-10-22 135310.png

実行してみる

test.py
import sys
import pytesseract
from PIL import Image

# 画像を読み込む
image = Image.open('C:/Users/ryuka/Documents/test.png')

# 画像からテキストを探して取り出す、langは英語、日本語などのパラメータ設定できる
text = pytesseract.image_to_string(image, lang='eng')

print(text)

使う画像
test.png

実行結果

スクリーンショット 2023-10-22 142553.png

完璧ではないが、9割以上の識別はできた感じ。

memo:荒い画像で試した結果、sesseractよりもeasyOCRの方がよりよい結果が得られましたので、インストールも扱いもeasyOCRの方がおすすめ。

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?