udon's blog

思いついたことを、思いついた時に。忘れないように。

ようやくビルド成功!(続・AndroidでTesseract)

先日のエントリの続きみたいなもので。。。
datsuns's blog

ようやくビルドが行けましたよっと。。

といっても、
Making an OCR Android App using Tesseract - Gautam Gupta's Blog | Gautam Gupta's Blog
このサイトを見つけてそのとおりにやっただけ。。。

一応確認込めて上のサイトの和訳意訳でも。

                                                    • -

This post tells you how you can easily make an Android application to extract the text from the image being captured by the camera of your Android phone! We’ll be using a fork of Tesseract Android Tools by Robert Theis called Tess Two. They are based on the Tesseract OCR Engine (mainly maintained by Google) and Leptonica image processing libraries.

このポストはAndroid端末のカメラでキャプチャしたデータからテキストを抽出するアプリを簡単に作っちまうポストだぜ!
このアプリにはTess Twoって呼ばれてるRobert Theis作のTesseract Android Toolsっていうライブラリを使うぜ!このライブラリってTesseractっていうOCRエンジン(メインメンテナはGoogle先生!)を元にして、Leptonicaっていう画像操作ライブラリを元にして作られてるみたいだ!

Note: These instructions are for Android SDK r14+ and Android NDK r6b. This does NOT work with NDK r7+, at least for the time being (written at this tree). You would also need proper PATH variables added.

ただし、ちょっとばかし気をつけてくれ。こいつはAndroid SDKのr14以降Android NDKのr6bでだけで動くからな。(少なくとも今は)NDKのr7以降じゃぁ動かないから気をつけてくれ!もちろんPATH設定を忘れるなんてヘマすんじゃねぇぞ!

※ 2012-07-24 追記
コメント欄でのやりとから、NDK r8以降だとすんなりビルドできるらしいことが判明。
ただしcygwinは(以下略

1.Download or check out the source from this git repository. This project contains tools for compiling the Tesseract, Leptonica, and JPEG libraries for use on Android. It contains an Eclipse Android library project that provides a Java API for accessing natively-compiled Tesseract and Leptonica APIs.

まずはこのgitレポジトリrmtheis/tess-two · GitHubからソースをチェックアウトしてくれ。このプロジェクトはTesseract、Leptonica、JPEGライブラリが含まれてる(もちろんAndroid向けのな!)そしてEclipse用のプロジェクトファイルにTesseractとLeptonicaのAPIを呼び出すためのAPI(JNIっていうんだぜ!)も含まれてる。
こいつはクールだろ!!

Open tess-two/jni/Android.mk and uncomment the lines starting with TESSERACT_PATH, LEPTONICA_PATH, LIBJPEG_PATH.

落としたファイルのtess-two/jni/Android.mkを開いてくれ。で、そのファイルには「TESSERACT_PATH」「LEPTONICA_PATH」「LIBJPEG_PATH」で始まる行があるはずだから、そいつらをコメントから外して、お天道さんの眩しい光を当ててやってくれ!

Build this project using these commands (it is easy to do it on Mac and Linux, you might try Cygwin for Windows) (here, tess-two is the directory inside tess-two – the one at the same level as of tess-two-test):

                  • -

cd /tess-two
ndk-build
android update project --path .
ant release

                  • -

ライブラリのビルドは下のコマンドの通りでできるぜ。MacやLinuxだときっとうまくいく。Windows(Cygwin)?んなもんしったこっちゃねぇよ!(Android.mkのマクロの展開がうまく行かねえんだ。どうしたらいいかまだオレもわかってねぇんだよ聞くなよ!)

Now import the project as a library in Eclipse. File -> Import -> Existing Projects into workspace -> tess-two directory. Right click the project, Android Tools -> Fix Project Properties. Right click -> Properties -> Android -> Check Is Library.

よし。ビルドはできたな?(tess-two/libsディレクトリの下に.soファイル、tess-two/binにtess-two.jarが転がってるはずだ!)
じゃぁEclipseから「ファイル -> インポート -> 既存のプロジェクトをワークスペースへ」と選んでインポートしてくれ。一応インポートした後に、プロジェクトで「右クリック -> Androidツール -> プロジェクト・プロパティーを修正」の実行と、「右クリック -> プロパティ」として、Androidタブで "Is Library"にチェックが入っているかを確認してくれな!
つまりこれは「ライブラリプロジェクト」として使うわけだ。わかるよな?

Configure your project to use the tess-two project as a library project: Right click your project name -> Properties -> Android -> Library -> Add, and choose tess-two. You’re now ready to OCR any image using the library.

自分でプロジェクト作って、設定でtess-twoプロジェクトをライブラリプロジェクトとして追加してくれ。(「右クリック -> プロパティー」として、Android -> ライブラリーに追加してくれ)
たったもうこれだけでOCRが使えるんだ。
最高にクールだろ?そう思うよな!!

First, we need to get the picture itself. For that, I found a simple code to capture the image here. After we have the bitmap, we just need to perform the OCR which is relatively easy. Be sure to correct the rotation and image type by doing something like:

ん?まだわかんねぇって?まったく手のかかるやつだな!とりあえず写真をとるんだよ!
まぁコードにするとざっとこんなもんだ。これでbitmapデータが取れるだろ?そうしたらもうしめたもんだ!
ただし画像が回転してちゃぁうまく行かねぇからな。そこは気を付けろよ!

// _path = path to the image to be OCRed
ExifInterface exif = new ExifInterface(_path);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);

int rotate = 0;

switch (exifOrientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
}

if (rotate != 0) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();

// Setting pre rotate
Matrix mtx = new Matrix();
mtx.preRotate(rotate);

// Rotating Bitmap & convert to ARGB_8888, required by tess
bitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, false);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
}

この

bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);

ってのは、とりあえずおまじないだと思っとけ!(Leptonicaの野郎が怒るんだよ!)

Now we have the image in the bitmap, and we can simple use the TessBaseAPI to run the OCR like:

オッケイ。これでちゃんとbitmapがとれたな!
次はTessBaseAPIを叩くんだ。

TessBaseAPI baseApi = new TessBaseAPI();
// DATA_PATH = Path to the storage
// lang for which the language data exists, usually "eng"
baseApi.init(DATA_PATH, lang); baseApi.setImage(bitmap);
String recognizedText = baseApi.getUTF8Text();
baseApi.end();

もう説明はいいよな?

Now that you’ve got the OCRed text in the variable recognizedText, you can do pretty much anything with it – translate, search, anything! ps. You can add various language support by having a preference and then downloading the required language data file from here. You might even put them in the assets folder and copy them to the SD card on start.

オッケイ。これでrecognizedTextに解析結果のテキストが入ってるはずだ。これでもう怖いものなしだ。煮るなり焼くなり好きにしろってこと!
おまけに言っとくと、Tesseractを使ってるおかげでいろんな言語にも対応できちまうぜ。ここから言語データベース落としてプロジェクトのassetsにぶっこんどけ!Downloads - tesseract-ocr - An OCR Engine that was developed at HP Labs between 1985 and 1995... and now at Google. - Google Project Hosting
アプリの起動時にちゃんとデータを展開しておくのをわすれるなよ!(TessBaseAPI#init()のドキュメントでも穴が開くほど読んでおくんだな!)

To make things easy, and for you to have a better understanding, I have uploaded a simple application on OCR that makes use of Tess Two on Github called Simple Android OCR (for beginners). If you want a full-fledged application, that has a selectable region while capturing the image, translating the text, preferences etc., then you can checkout Robert Theis’s Android OCR application on Github too (for intermediate+)!

どうだい。簡単にできちまうだろ?オレだってびっくりよ。
で、これで終りと思ったそこのお前!最後のおまけだぜ!
オレが作ったサンプルアプリをアップしておいてやったんだよ!
GautamGupta/Simple-Android-OCR · GitHub

とはいえこいつは初心者向けにすっげー簡単な処理しかしてねぇからよ、もし物足りなかったこっちもトライしてみるんだな!
rmtheis/android-ocr · GitHub

                                              • -
  • 追記

あんまり言及なかった部分で一番ハマったところはというと、
Tesseractの初期化(TessBaseAPI#init())ですね。
実は今回意訳したサイトに辿り着く前にrmtheis/android-ocr · GitHubは見つけてて、各ライブラリのビルド自体は成功してたんですけど、いざ動かすと初期化がうまく行かなかったんです
(nativeに飛んだ後で例外も何もなく死んだりとか・・)

結局は

  1. Tesseractのデータベースパスの指定方法
  2. 結局のところどのファイル置けばええの?

がわからんかったのですが、例えば日本語向けなら
Downloads - tesseract-ocr - An OCR Engine that was developed at HP Labs between 1985 and 1995... and now at Google. - Google Project Hosting
ここからjpn.traineddata.gzをダウンロード、解凍して、それを置いたパスを指定すればええんですね。
(言語指定は"jpn"になります)

rmtheis/android-ocr · GitHubで作られてるアプリは、「データベースがなかったら自分で落としてきて(zip解凍して!)配置する」様な動きになってたので逆にわからんかったのです orz

今回のサイト主が作ったアプリは「手動でassetsに配置してAssetManagerを使って適当なディレクトリに展開」するので、それでようやくわかりました。。。

しかし探せば誰かがやってるもんなんですねぇ。
あと一番びっくりしたのは、サイトの作者が16歳っていう。。。
ワシも頑張らなな〜〜〜。

お世話になったのでコメントでもしてきまっさ!