Mac端末上でpytesseractを使って画像文字認識(英語、日本語)を試してみた

Mac端末上でpytesseractを使って画像文字認識(英語、日本語)を試してみた

Clock Icon2022.08.19 14:56

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

pytesseractは、文字認識(OCR)エンジンおよびそれを使用するためのCLIを提供するTesseractのPython実装です。

今回は、Mac端末上でpytesseractを使って画像文字認識(英語および日本語)を試してみました。

やってみた

環境

端末はM1 MacBookとなります。

$ sw_vers
ProductName:    macOS
ProductVersion: 12.5
BuildVersion:   21G72

最初にPythonが未導入だったのでインストールします。

brew install python

インストールできました。

$ python3 -V
Python 3.8.9

$ pip3 -V
pip 20.2.3 from /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)

Tesseractの導入

Tesseractは、まずBrewで本体をインストールした後にpipでpytesseractをインストールします。 (いきなりpip3 install pytesseractしようとするとエラーとなる)

brew install tesseract
pip3 install pytesseract

英語の文字認識してみる

まず英字が写った画像(string.png)を認識させてみます。

$ python3
>>> import pytesseract
>>> from PIL import Image
>>> str_img = Image.open('string.png')
>>> str = pytesseract.image_to_string(str_img)
>>> print(str)
Classmethod

Providing continuous support of creative activities to all people

一字一句正確に文字認識できていますね!

次に表形式に英語および数字が記載された画像(numbers.png)を認識させてみます。

>>> num_img = Image.open('numbers.png')
>>> num = pytesseract.image_to_string(num_img)
>>> print(num)
FY2018 FY2019 FY2020 FY2021 FY2022

7,414 13,199 21,678 30,486 42,710

こちらも正確に文字認識できています!

日本語対応させてみる

Tesseractは既定のパッケージでは日本語に対応していません。

>>> pytesseract.get_languages()
['eng', 'osd', 'snum']

調べてみるとtesseract-ocr-jpnをHomebrewでインストールすれば良さげな情報が出てきますが、現時点でFormulaeに登録が無いようで失敗します。

## エラーとなる
$ brew install tesseract-ocr-jpn
Warning: No available formula with the name "tesseract-ocr-jpn". Did you mean tesseract?
==> Searching for similarly named formulae...
This similarly named formula was found:
tesseract ✔
To install it, run:
  brew install tesseract ✔
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.

さらに他の方法を調べたところ次の記事が参考になりました。

まず日本語の訓練データを下記よりダウンロードします。

対象は以下の2つです。後者は縦書き対応のデータとなります。

[Download]ボタンでダウンロードできます。

これらのダウンロードした訓練データをTesseractのshare/tessdata/パス配下に配置します。パスはbrew list tesseractで確認できます。

$ brew list tesseract
/opt/homebrew/Cellar/tesseract/5.2.0/bin/tesseract
/opt/homebrew/Cellar/tesseract/5.2.0/include/tesseract/ (12 files)
/opt/homebrew/Cellar/tesseract/5.2.0/lib/libtesseract.5.dylib
/opt/homebrew/Cellar/tesseract/5.2.0/lib/pkgconfig/tesseract.pc
/opt/homebrew/Cellar/tesseract/5.2.0/lib/ (2 other files)
/opt/homebrew/Cellar/tesseract/5.2.0/share/tessdata/ (35 files)

確認したパスに2つの訓練データを配置します。

mv ~/Downloads/jpn.traineddata /opt/homebrew/Cellar/tesseract/5.2.0/share/tessdata/jpn.traineddata
mv ~/Downloads/jpn_vert.traineddata /opt/homebrew/Cellar/tesseract/5.2.0/share/tessdata/jpn_vert.traineddata

Pythonのプロセスを再起動して利用可能な言語一覧を再度確認すると、jpnおよびjpn_vertが追加されています!

$ python3
>>> import pytesseract
>>> pytesseract.get_languages()
['eng', 'jpn', 'jpn_vert', 'osd', 'snum']

それでは日本語が写った画像(string_jp.png)を認識させてみます。

pytesseract.image_to_stringlangオプションを指定すると、文字認識させられました。

>>> from PIL import Image
>>> str_jp_img = Image.open('string_jp.png')
>>> str_jp = pytesseract.image_to_string(str_jp_img, lang="jpn")
>>> print(str_jp)
私 た ち は 新 し い テ クノ ロジ ー に よる
「 課 題 解 決 」 と 「 価 値 創 造 」 で
企業 の 競争 力 を 高め ます

余計なスペースは入っていますが、文字の認識の誤りはなく精度は高そうです!

おわりに

Mac端末上でpytesseractを使って画像文字認識(英語および日本語)を試してみました。

日本語対応をさせるのに結構手間取りましたが出来てよかったです。

参考

以上

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.