はじめに
tesserocr を利用してみた際の設定から実行についてのメモです。
tesserocr は、python用のtesseract-ocr APIのラッパーライブラリです。
https://github.com/sirfz/tesserocr
ハマったポイントも記載しました。
インストール
上記サイトを参考にインストールします。
- 前提条件としては、tesseract 4系 がインストール済みであることです。
参考: tesseractを使ってみよう②(初心者編)
$ tesseract -v
tesseract 4.0.0-rc4
- tesserocrのインストール
$ CPPFLAGS=-I/usr/local/include pip install tesserocr
- 依存ライブラリのPillowのインストール
$ pip install pillow
使ってみる
- 上記サイトにあるソースをほぼコピーして試してみて、エラーがでなければインストール成功です。
import locale
locale.setlocale(locale.LC_ALL, 'C')
import tesserocr
from PIL import Image
print(tesserocr.tesseract_version()) # print tesseract-ocr version
print(tesserocr.get_languages()) # prints tessdata path and list of available languages
image = Image.open('test.png')
print(tesserocr.image_to_text(image, lang=('jpn'))) # print ocr text from image
エラー対処
いくつかエラーが出てハマったので下記に記載します。
エラー①
- 普通にpipでインストールした後に、スクリプトを実行したら下記のエラーが出たので、一旦tesserocrをアンインストール
import tesserocr
ImportError: libtesseract.so.3: cannot open shared object file: No such file or directory
- 再インストール時は下記コマンドを実行
$ CPPFLAGS=-I/usr/local/include pip install tesserocr
Collecting tesserocr
Using cached https://files.pythonhosted.org/packages/f8/6d/4e81e041f33a4419e59edcb1dbdf3c56e9393f60f5ef531381bd67a1339b/tesserocr-2.3.1.tar.gz
Installing collected packages: tesserocr
Running setup.py install for tesserocr ... done
Successfully installed tesserocr-2.3.1
エラー②
- ロケールを設定しないと下記のエラーが発生
詳細はこちら: https://github.com/sirfz/tesserocr/issues/137
!strcmp(locale, "C"):Error:Assert failed:in file baseapi.cpp, line 209
Segmentation fault (core dumped)