LoginSignup
1
6

More than 5 years have passed since last update.

tesserocrを使ってみる

Posted at

はじめに

tesserocr を利用してみた際の設定から実行についてのメモです。
tesserocr は、python用のtesseract-ocr APIのラッパーライブラリです。
https://github.com/sirfz/tesserocr

ハマったポイントも記載しました。

インストール

上記サイトを参考にインストールします。

$ 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

エラー②

!strcmp(locale, "C"):Error:Assert failed:in file baseapi.cpp, line 209
Segmentation fault (core dumped)
1
6
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
1
6