JKになりたい

何か書きたいことを書きます。主にWeb方面の技術系記事が多いかも。

PythonでOCRがんばる(1)

tesseractでOCR

これ(https://github.com/tesseract-ocr/tesseract)を入れる

Pythonで使いたいのでラッパーを入れる。

https://github.com/jflesch/pyocr

日本語に対応させるためにここ(https://github.com/tesseract-ocr/tessdata)からjapn.traineddataを/usr/local/tessdataに入れる。
パスを貼る。

export TESSDATA_PREFIX="/usr/local/share/tessdata/tessdata/

プログラムを書く。

from PIL import Image
import sys
import pyocr
import pyocr.builders
tools = pyocr.get_available_tools()
if len(tools) == 0:
    print("No OCR tool found")
    sys.exit(1)
# The tools are returned in the recommended order of usage
tool = tools[0] txt = tool.image_to_string(
    Image.open('./img_file_path.png'),
    lang="jpn+eng",
    builder=pyocr.builders.TextBuilder(tesseract_layout=6)
    )
print(txt)

結果

f:id:deeptoneworks:20160916040224p:plain
pct Hmschold Ind Ana ol'Floor Splec pct PM by Typc oanilding (7 Groups) Ind Timur: of Dwelling (~ Groups) . All Shi. All Gun Ind

お?まあまあええ感じやん!近いで!

f:id:deeptoneworks:20160916040251p:plain
leh: 22. Privalr. Households Illd Household Mclnbas Living in Dwelling Ind Avcng: Number of Household Mcmbcls Ind Ale- :1me Spin:

おしい!おしいわ!!!

f:id:deeptoneworks:20160916035256p:plain
(~ ~) 惟}が攫んでいる疇

なんやこれ・・。

f:id:deeptoneworks:20160916040427p:plain
令債の薫ー

もうあかん・・・。

f:id:deeptoneworks:20160916040453p:plain
讐璽の囁雛の鷹翼儡区分

あああああ・・・・・。

f:id:deeptoneworks:20160916040635p:plain
↑真っ白な画像

さすがに何もないやつは何も表示されへんのやな。

次は数字シリーズいくで!

f:id:deeptoneworks:20160916041649p:plain
数字の1や!結果は・・なんも出んかったわ・・。

f:id:deeptoneworks:20160916041728p:plain
f:id:deeptoneworks:20160916041755p:plain
これもこれもあかんかったわ・・。もうやめよ。

総評

これ以外にちゃんと文字が大きいやつでやったらまあまあやったで!(英語は)
日本語はあかんっぽいなあ・・・。
自分でデータセット作って学習させたらええらしいけど、そんなんだるいて!!