【Python】pytesseractを使ってみた
OCRモジュールのpytesseractのPython版を使ってみた。
最初はtesseractを使ってみたけど何故かPythonが動作停止に。
その前にまずpythonのtesseractはC++のラッパーなのでtesseract-OCRのインストールが必要。
github.com
その次にPythonで下記を実行
import tesseract #tesseractオブジェクト作成 api = tesseract.TessBaseAPI() api.Init(".", "eng", tesseract.OEM_DEFAULT) api.SetPageSegMode(tesseract.PSM_AUTO) #画像ファイル読み込み mBuffer=open("img.jpg", "rb").read() #文字情報取得 print tesseract.ProcessPagesBuffer(mBuffer,len(mBuffer),api)
すると tesseract.ProcessPagesBuffer()を実行するところで何故か必ずpythonが落ちる。
原因が分からなかったので違うラッパーのpytesseractを使ってみた。
pypi.python.org
その結果うまくいった。下記がその時のコード
import pytesseract import Image img = Image.open("sample.jpg") print pytesseract.image_to_string(img, config = "nobatch digits")
configに"nobatch digits"を指定すると全て数値に変換(数値のみ読み取り?)するようになる。
\Tesseract-OCR\tessdata\configs\digits がその設定ファイルで
tessedit_char_whitelist 0123456789-.
を書き換えると任意の文字のみにも対応可能らしい。
参考↓
www.netplan.co.jp