私的AI研究会 > Tesseract

文字認識エンジン「Tesseract」

 実用的な AI開発に向けて、文字認識エンジン「Tesseract」(テッセラクト) を試してみる。

※ 最終更新:2021/12/23 

環境構築

「PyTorch ではじめる AI開発」で使用した Anaconda プラットホームを利用する。
 あらかじめプロジェクトフォルダ「pyocr」を作成しておく。

$ conda activate py37
(py37) $ cd ~/workspace_py37/
(py37) $ mkdir pyocr

必要なパッケージをインストール

環境構築と対応言語の確認

OCR としての簡単なテスト

Webカメラ入力の簡単なテスト

OCR テストプログラム

  Python 用の光学式文字認識 (OCR) ツール ラッパー PyOCR のドキュメントを参考にテストプログラムを作成する。
 デフォールトのテスト画像は「test1.png」→
 できるだけコマンド入力でパラメータを設定できるようにする。

Step 1 イメージ画像からテキストを認識する「ocrtest1.py」

Step 2 イメージ画像から文字と文字の位置を認識する「ocrtest2.py」

Step 3 イメージ画像から文章と文章の位置と信頼性スコアを取得する「ocrtest3.py」

Step 4 文章と文章の位置と信頼性スコアを表示する「ocrtest4.py」

【追加した主な機能】
・信頼性スコアの閾値を設けて信頼性の低いものは除外する。
・入力画像の前処理として「グレイスケール変換/2値化」「罫線消去」の有無を設定できる。
・Tesseract 動作パラメータの一つである「tesseractレイアウト(0-13)」を指定できる。
 この値は入力画像の内容により認識精度に大きく影響する。デフォールト値は一般的な文章画像(帳票を含む)の反復テストにより決定した。
・入力画像のサイズの制限値(--maxsize)により値が'0'でなければ縦横このサイズ内に収まるように同比率で自動リサイズする。
・認識結果の文はコンソール出力も行うが各文字の詳細出力も可能なように設定フラグ(--log)がある。
・タイトル表示が有効であれば実行時の設定値の情報を表示する。
・GUI 出力表示結果を画像ファイルとして出力できる。

OCR エンジン「tesseract」の詳細

 認識精度の向上にはさらに調整が必要と思われる。

「tesseract」のコマンド入力パラメータ

(py37) $ tesseract --help-extra
Usage:
  tesseract --help | --help-extra | --help-psm | --help-oem | --version
  tesseract --list-langs [--tessdata-dir PATH]
  tesseract --print-parameters [options...] [configfile...]
  tesseract imagename|imagelist|stdin outputbase|stdout [options...] [configfile...]

OCR options:
  --tessdata-dir PATH   Specify the location of tessdata path.
  --user-words PATH     Specify the location of user words file.
  --user-patterns PATH  Specify the location of user patterns file.
  --dpi VALUE           Specify DPI for input image.
  -l LANG[+LANG]        Specify language(s) used for OCR.
  -c VAR=VALUE          Set value for config variables.
                        Multiple -c arguments are allowed.
  --psm NUM             Specify page segmentation mode.
  --oem NUM             Specify OCR Engine mode.
NOTE: These options must occur before any configfile.

Page segmentation modes:
  0    Orientation and script detection (OSD) only.
  1    Automatic page segmentation with OSD.
  2    Automatic page segmentation, but no OSD, or OCR. (not implemented)
  3    Fully automatic page segmentation, but no OSD. (Default)
  4    Assume a single column of text of variable sizes.
  5    Assume a single uniform block of vertically aligned text.
  6    Assume a single uniform block of text.
  7    Treat the image as a single text line.
  8    Treat the image as a single word.
  9    Treat the image as a single word in a circle.
 10    Treat the image as a single character.
 11    Sparse text. Find as much text as possible in no particular order.
 12    Sparse text with OSD.
 13    Raw line. Treat the image as a single text line,
       bypassing hacks that are Tesseract-specific.

OCR Engine modes:
  0    Legacy engine only.
  1    Neural nets LSTM engine only.
  2    Legacy + LSTM engines.
  3    Default, based on what is available.

Single options:
  -h, --help            Show minimal help message.
  --help-extra          Show extra help for advanced users.
  --help-psm            Show page segmentation modes.
  --help-oem            Show OCR Engine modes.
  -v, --version         Show version information.
  --list-langs          List available languages for tesseract engine.
  --print-parameters    Print tesseract parameters.
 

更新履歴

参考資料

 

Last-modified: 2021-12-21 (火) 13:42:52