DesignAssembler

備忘録に近い

c++でtesseract

この前のtesseractの続きです。

hyottokoaloha.hatenablog.com

tesseractにはapiが用意されています。これをc++で使ってみます。

失敗

このサイトを見て始めました。

Tesseract-OCRの導入(その2)Visual Studio2013でのAPIの利用 | 株式会社インデペンデンスシステムズ横浜

コンパイルするとSTRINGが見つからないと怒られる。

ずっと彷徨っていたらincludeのタイポ

//#include "tesseract/strings.h"
//ではなく、
#include "tesseract/strngs.h"

これは腹が立つ。

修正してコンパイルすると次はそもそもSTRINGじゃねえよと怒られる。

tesseract_practice.cpp:21:41: error: cannot initialize a parameter of type 'tesseract::TessResultRenderer *' with an rvalue of type 'STRING *'
  api.ProcessPages("tess.png", NULL, 0, &text_out);

だがリファレンスみるとSTRINGと書いてある。 http://zdenop.github.io/tesseract-doc/group___advanced_a_p_i.html#gaf89df932c29b0abe4fb4ca3ae852506b

んで、GitHubのソースのProcessPages見るとTessResultRendererと書いてある・・・・・

https://github.com/tesseract-ocr/tesseract/blob/master/api/baseapi.h

ここでもう一度参考にしたページを見るとtesseractのバージョンが古かった。これが原因だ。

APIExample

githubWikiに丁寧なexampleが載ってました。

github.com

ここのコードをコピペするといとも簡単に動きました。

この画像を認識してみます。 f:id:hyottokoaloha:20160322203347p:plain 以下が出力です。

Developers can use libtesseract C or C++ API to build their own application. If you need bindings to libtesseract for

other programming languages, please see the wrapper section on AddOns wiki page.

Documentation of Tesseract generated from source code by doxygen can be found on tesseract-ocr.github.io.
The code in this repository is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE—2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
NOTE: This software depends on other packages that may be licensed under different open source licenses.

英語はかなり正確ですね。

日本語対応

日本語のtraineddataをダウンロードします。

https://github.com/tesseract-ocr/tessdata/blob/master/jpn.traineddata

これを/usr/local/share/tessdata/に入れれば準備完了です。

人間失格を入れてみます。

f:id:hyottokoaloha:20160322211302p:plain

出力はこのようになりました。

私は、 その男の写真を三葉、 見たことがある。

ー葉は、 その男の、 幼年時代、 とでも言うベきであろうか、 亡唇剪後かと推定される頃の写真であって、 その子供が大勢窮嘗のひとに取り
かこまれ、 (それは、 その子供の姉たち、 妹たち、 それから、 従姉妹たちかと想像される) 庭園の池のほとりに、 荒い縞の袴をはいて立ち、
首を三十度ほど左に傾け、 醜く笑っている写真でぁる。 醜く ? けれども、 鈍い人たち (つま り、 美醜などに関心を持たぬ人たち) は、 面白
<も何とも無いような顔をして、

「可愛い坊ちゃんですね」 から い

といい加減なお世辞を言っても、 まんざら空お世辞に聞えなぃく らいの、 謂わば通俗の 「可愛らしさ」 みたいな影もその子供の笑顔に無い
わけではなぃのだが、 しかし、 いささかでも、 美醜に就いての訓練を経て来たひとならヽ ひとめ見てすぐ、

「なす舎監ヽ いゃな子堡輻」

と顔る不快そうに呟き、 毛虫でも払いのける時のような手つきで、 その写真をほう り投げるかも知れない。

まったく、 その子供の笑顔は、 よ<見れば見るほど、 何とも知れず、 イヤな蒲気味悪いものが感ぜられて来る。 どだい、 それは、 笑顔でな
い。 この子は、 少しも笑ってはいなぃのだ。 その証拠には、 この子は、 厩方のこぶしを固く握って立っている。 人間は、 こぷしを固く握りな

やるじゃんって感じです。

次はOpenCVと連携させたいと思います。