memorandums

日々の生活で問題解決したこと、知ってよかったことなどを自分が思い出すために記録しています。

tesseract-ocrでOCRしてみた

本(英語)を持ち歩くのが大変なので必要な部分をiPhoneで撮影して画像ファイルにしてみました。

照明条件が悪かったせいか小さい文字は読み難い。。。

思いつきですがOCRでテキスト抽出できないかチャレンジしてみました。

とりあえず試したのはGoogleが支援して開発しているらしいtesseract-ocrです。

Link: tesseract-ocr

いろいろ試行錯誤したのですが、結局、macportsが対応していたのでインストールは簡単でした。わかってしまえば、の話しですが。以下、手順です。macportsが導入済みであることを前提としています。

sudo port install tesseract

ブラウザを開き、ここにアクセスして、以下の2つのファイルをダウンロード&解凍します。

tesseract-ocr-3.01.eng.tar.gz
eng.traineddata.gz

解凍したtesseract-ocrフォルダとeng.traineddataというファイルを/opt/local/share/tessdataの中にコピーします。

以上で準備完了です。

では、テスト。

サンプルとしてこちらからAlice's Adventures in Wonderland by Lewis Carrollの一文を表示して画面キャプチャして用意しました。

画面キャプチャはpng形式になりますので、画像をプレビューで開き、書き出しでtiffファイルに変換します。使い方は以下。第1引数は画像ファイルの名称、第2引数は認識結果のテキストファイルの名称を指定します。

tesseract img.tiff result

認識結果は以下でした。

Aum was mgmng 10 gm my ma of smug by mmm on mn bm, ma of mvmg naming 10 ao:
om of mam sm ma pmpm mm mn book mmm was mamg, bm 1| ma no pawns of mvmmms
an ax. ‘ma wh." is mn use of A \>ook.‘ mougm Aum ‘wamom pawns of mvmmmv

画面キャプチャなので解像度が低いのが原因と思われますが。。。意外と悪いですね。言語ファイルや訓練ファイルを追加すると日本語もいけるらしいですが。。。

ちなみに、ScanSnapに付属してきたAdobe Acrobat Professional 8.1.0でこの画像ファイルをOCR処理してみました。結果は以下です。

Alice
was beginning
to get very tired
of sitting by her sister
on the bank
and of having nothing to do: once or!Wice she had peeped
into the book her sister was reading, but it had no pictures
or conversations init,'and what is
theusc
ofabook,'thoughtAlice
'withoutpictures
orconversation?

まだ、許容範囲ですね。。。結果的にあまり役に立たない情報になってしまいましたが、トライの結果として記録しておきたいと思います。

■ちょっと追記

このままではtesseractの開発者に申し訳ないので、解像度を上げて再度実験してみました。使用した画像は以下です。

結果は以下です。

Alice was beginning to get very tired of sitting
by her sister on the bank, and of having nothing
to do: once or twice she had peeped into the
book her sister was reading, but it had no
pictures or conversations in it, ‘and what is the
use of a book,‘ thought Alice ‘without pictures or
conversation?‘

おみごと!