LoginSignup
56
49

More than 5 years have passed since last update.

Pythonで画像内の数字認識

Posted at

はじめに

画像内の数字を「手軽」に認識します。
今回はpythonライブラリの tesseract を使用します。
コード自体は非常に簡単に組むことが出来るのですが、tesseract をインストールするのが案外楽ではなかったので、メモしておきます。

インストール手順

ターミナルから以下のコマンドでインストールを完了できます。

tesseractのインストール
$ brew install tesseract
$ pip install pytesseract
$ pip install pillow
インストール完了の確認
$ python
>>> import pytesseract

これでエラーが出なければ完了です。

実行例

今回はこちらの画像 (screen.png) の数字を認識させます。
screen.png

数字認識用のスクリプトはこちら。

recogition_number.py
# -*- coding: utf-8 -*-
import pytesseract
from PIL import Image

url_img = 'screen.png'
img = Image.open(url_img)
number = pytesseract.image_to_string(img)
print number

では、実行してみましょう。

$ python recog_number.py
2048

お、さっと認識してくれました。
今回は画像内にノイズが少なく、認識が容易だったということも有りますが、うまく認識できているので満足です。

インストール失敗例

pipで素直にtesseractをインストールすると、割とハマってしまうので、お気をつけ下さい。

pip が入っていれば、他のライブラリと同様に pip経由でインストールできます。

$ pip install tesseract --user

これでインストール完了と思いきや、インポートしてみるとよくわからないことを聞かれます。

$ python
>>> import tesseract
Please enter the path to an existing directory where qhull should be installed:

何かをインストールしている場所を教えて下さいと言われますが、心当たりがありません。
お気を付け下さい

おまけ

フォローお待ちしています!
Qiita: Carat 吉崎
twitter:@carat_yoshizaki
はてなブログ:Carat COOのブログ
ホームページ:Carat

機械学習をマンツーマンで学べる家庭教師サービス「キカガク
「数学→プログラミング→Webアプリケーション」まで一気に学べる「キカガク」に興味のある方はお気軽にご連絡ください。

56
49
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
56
49