ホンモノのエンジニアになりたい

ITやビジネス、テクノロジーの話を中心とした雑記ブログです。

CentOS7にTesseractをインストール【Tesseract5.0.0-alpha奮戦記】

robot

 

みなさん、こんにちわ。今日も元気にOCRしてますか?


えっ?してない?働き方改革で生産性がホゲフガなこの時代に、してないですって?
だってOCRソフトってお高いんでしょう?持ち合わせがございませんもの。ですって?
HAHAHA、なるほどなるほど、大丈夫!

そう、Tesseractがあればね。

 

OCRなんてそんな古臭いプロダクトで遊んでる暇はない!ですって?
AIやRPAじゃないとカッコ付かないし、予算も下りないんだよ!ですって?
HAHAHA、なるほどなるほど、大丈夫。

そう、Tesseractがあればね。

 

 

Tesseractは現在Googleが開発してるオープンソースのOCRエンジンです。タダで使えます。そのうえ最近のバージョンではいま流行りの機械学習の仕組みが入っています。更にこのソフトウェアの得意な作業はいわゆるRPAに通じるところがあります。

偉い人は「当社もAIとRPAの導入に着手しております」と高々と言えるし、下っ端雑兵ショッカーの皆さんは「AIかRPAで何かやれ」と命令された時に「御意。Tesseractします」と胸を張って言えます。

 

そんな誰にも優しいTesseract。このソフトのインストール記をここに上梓いたします。

 

Tesseractを使ってOCRをやってみた結果は、こちら↓のエントリに書いています。Googleドライブを使った方法と、Webサービスを使った方法と比較した結果を掲載していますので、是非ご覧くださいませ。

 

 もくじ

 

0.インストール環境と前提知識

インストール環境

OS:CentOS7 

# uname -a
Linux localhost.localdomain 3.10.0-693.21.1.el7.x86_64 #1 SMP Wed Mar 7 19:03:37 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)

 
稼働環境 :Virtual Box
リソース :1Core, 2GB (で頑張る)
作業日  :2019/10/17から数日かけてダラダラと
実行ユーザ:root
 
→このエントリではPCローカルにたてたCentOS7で弄り回していますので、基本rootで操作しています。参考にする方は適宜sudoするなり、スイッチするなり、煮るなり、焼くなりしてください。
 

前提知識

  • Tesseractって何ぞや?

現在Googleが中心となって開発しているOCRエンジンです。Githubでソースが公開されているので、我こそはという方は是非ご貢献を。

元はHPが開発していて、2006年からGoogleがやっているみたいです。PythonやRubyなどいくつかの言語でラッパーが開発されているので、アプリに取り込むのも楽チン(だと思う)

 

1.yumでバッと入れりゃいーじゃん!→v3系でした

こちらのQiitaエントリを参照しながらやってみました。

 

もうこれだけでインストール完了して動く。素晴らしい。

sudo yum install -y epel-release
sudo yum install -y tesseract tesseract-langpack-jpn.noarch leptonica
tesseract -l eng file.jpg stdout

 

とりあえずテスト用にテキトーな画像を食べさせてみよう。

 

f:id:kwnflog:20191026162146j:plain

 この画像をTesseractでOCRする。

# tesseract test1.JPG stdout -l eng
01. A large collection of actual questions

02. A comprehensive presentation of English grammar points

03. A combination of fundamental theories and actual practice

04. Typical questions and useful tips for doing the TOEIC Reading test

05. Essential vocabulary and expressions for each part 01 the TOEIC Reading
test

06. Two actual tests

 

おぉっ!'of'を'01'に間違えただけだ(赤字のところ)。割とはっきり写っててコントラストもバッチシの画像ではありますが、けっこう精度出るんですね。この画像くらいの傾きと汚れくらいは余裕って感じ。

 

とりあえず動いたけど、学習データをブッ込むことで読取精度が上がるらしい。やっちゃいましょう。学習データは↓このパスに入れるらしいです。yumインストールしたときに ”tesseract-langpack-jpn” を入れていたのでjpn言語パッケージが存在してます。engは標準で入るっぽいです。

# ls -l /usr/share/tesseract/tessdata/
合計 69796
drwxr-xr-x 2 root root 266 10月 28 15:44 configs
-rw-r--r-- 1 root root 171918 6月 25 2015 eng.cube.bigrams
-rw-r--r-- 1 root root 38 6月 25 2015 eng.cube.fold
-rw-r--r-- 1 root root 181 6月 25 2015 eng.cube.lm
-rw-r--r-- 1 root root 857304 6月 25 2015 eng.cube.nn
-rw-r--r-- 1 root root 254 6月 25 2015 eng.cube.params
-rw-r--r-- 1 root root 13020078 6月 25 2015 eng.cube.size
-rw-r--r-- 1 root root 2444187 6月 25 2015 eng.cube.word-freq
-rw-r--r-- 1 root root 996 6月 25 2015 eng.tesseract_cube.nn
-rw-r--r-- 1 root root 21876550 6月 25 2015 eng.traineddata
-rw-r--r-- 1 root root 33072615 6月 25 2015 jpn.traineddata
-rw-r--r-- 1 root root 568 1月 26 2016 pdf.ttf
drwxr-xr-x 2 root root 98 10月 28 15:44 tessconfigs
 

GithubにTesseractの学習データを管理してるレポジトリがありますので、そこからbest(高精度)というデータを持ってきて↑のディレクトリに配置します。このレポジトリです。

GitHub - tesseract-ocr/tessdata_best: Best (most accurate) trained LSTM models.

では、bestデータで読みこんでみましょう。

# tesseract test1.JPG stdout -l eng
Failed loading language 'eng'
Tesseract couldn't load any languages!
Could not initialize tesseract.

 

あれ・・・何か怒られた・・・もしやして、 

# tesseract --version
tesseract 3.04.00

 

そーゆーこと?

CentOS7のyumでインストールしたTesseractはバージョン3.04、学習データはバージョン4系だからダメってこと?どうやらそうっぽいです。つかv4出てるなら、v4使いたいよね。機械学習の仕組みが入ってるらしいし。

 

 

2.Tesseract v4 をインストールしよう

続いては、こちらのエントリを参照。

 

このエントリ内にGithubのWikiページのリンクがありました。

Home · tesseract-ocr/tesseract Wiki · GitHub

で、ここに各OS別にバイナリインストールの方法が掲載されています。CentOSの場合はこう。

 

yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_7/
sudo rpm --import https://build.opensuse.org/projects/home:Alexander_Pozdnyakov/public_key
yum update
yum install tesseract 
yum install tesseract-langpack-deu

Alexanderさんのレポジトリを追加して、公開鍵をインポートして、yumアップデートかけて、本体インストールして、言語パックも入れる。ベリーシンプル

 

yum-config-managerって何だ?そんなコマンド知らねーぞ、と思ったんですが、これで入りました。こんなん前からあったっけ?

# yum install yum-utils
 

上のコマンドではlangpackでdeu(ドイチュ)を指定していますが、素直な心でjpnに変えれば日本語用の言語パックが入ってきます。

# yum install tesseract-langpack-jpn

 

 バージョンも見ておきましょう。

# tesseract --version
tesseract 4.1.0
leptonica-1.76.0
libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0
Found AVX2
Found AVX
Found SSE

 

うん、ちゃんとバージョン4系が入ってきた。

 

では、前項と同じ画像を読みこんでみましょう。(もっと読みづらい画像にすればえがった...) 

# tesseract test1.JPG stdout -l eng
O1.a large collection of actual questions

02.4 comprehensive presentation of English grammar points

03.4 combination of fundamental theories and actual practice

04. Typical questions and useful tips for doing the TOEIC Reading test

05. Essential vocabulary and expressions for each part of the TOEIC Reading
test

06. Two actual tests

 

バージョン3の時と比べてちょっと劣化してますな。バージョン4からは追加学習ができるらしいので、自分たちで調教していくスタイルが前提なんでしょうかね。あとはまぁ画像に依るんだろうなぁ。前項で試したGithub上のbestデータを使ってやってみました。 

GitHub - tesseract-ocr/tessdata_best: Best (most accurate) trained LSTM models. 

# tesseract test1.JPG stdout -l eng
01.4 large collection of actual questions

02. A comprehensive presentation of English grammar points

03. a combination of fundamental theories and actual practice

04. Typical questions and useful tips for doing the TOEIC Reading test

05. Essential vocabulary and expressions for each part of the TOEIC Reading
test

06. Two actual tests

 

あ、ちょっと精度上がった。2文字正解が増えましたね。目視で数えたところ、この画像は大体60語くらいの単語から成り立っています。このくらいの解像度の画像で誤判定率3%程度と結構いい感じ。さらに調教していけば自分色に染めていくことができるのではないでしょうか。

 

さて、このTesseract4は問題なく動作しました。けど、わたし、気付いちゃったんす。現時点(19年10月)でGithubのMasterブランチにあるのって、「5.0.0-alpha」なんすよね。

 

どうせなら、新しいの使いたいなぁ。何が違うのかも知らないけど。

 

3.Tesseract 5.0.0-alphaをインストール

ソースコードに含まれるバージョン情報を見てみると「5.0.0-alpha」であるためまだ開発中のバージョンなんでしょう。ドキュメント周りを見てみてもバージョン4系の記述がまだ残っています。つーかどこまで開発が進んでいるのかも不明です。まぁインストールごっこだと思って気軽な気持ちで(!)弄っていきましょう。

ちなみに、開発中の最新版なのでソースインストールになります。

CATION:ここからインストール奮戦記になります。ミスったり上手くいかなかったこと、最終的に?のまま終わったことも書いてます。上から順にトレースして実行していくと同じくエラーになると思うのでご留意くださいませ。一応最後は動作しますが。

 

参考ページ

インストールのためのオフィシャル情報は以下の2つのページ。

プロジェクトのWikiページ

Githubソースコード内のInstall.Git.md

 

両方のページを比べながら読んでいくと、片方にあるけどもう一方に無いとか、こっちではバージョンがいくつと書いてあるけど、こっちでは違うとか、そういうことがありました。まぁ開発中ですし、両方のページを読みながらインストール前提を探っていきましょう。

 

インストールに必要な材料一覧

本体のビルド

①Tesseract本体のソースコード(Githubから)
②学習済みデータ(少なくとも英語とOSDのデータが必要)
③前提パッケージ
automake, ca-certificates, gcc, g++ (compiler with c++11 support), git, libtool,libleptonica-dev v1.74.2 or later, make, pkg-config

オプション機能のビルド

★manpageのビルド
 asciidoc, docbook-xsl, xsltproc
★Tesseract training toolsのビルド
 libpango1.0-dev, libicu-dev, libcairo2-dev
★Tesseract with TensorFlowのビルド
 libprotoc-dev, libtensorflow-dev
★ScrollViewのビルド
 curl

 

前項、前々項ではyumさんが良い感じにパッケージを調整してくれていたので前提パッケージを意識する必要はありませんでした。しかし、ソースからのビルド・インストールとなると、その前提となるライブラリやソフトを入れていかねばなりません。

 

オプション機能を利用するときは、本体インストールとは別にいくつかのオプション機能向けの前提パッケージが必要のようですね。manページ、Training Tool、with Tensorflow、ScrollView、それぞれに前提のパッケージが要求されます。

 

ちなみにScrollViewはViewDebuggingっていうツールを使う時に必要になるものみたいです。GUIツールですね。ここに説明があります↓

ViewerDebugging

Tesseract has a built-in capability to display its internal state, so that you can view its segmentation and recognition.

https://github.com/tesseract-ocr/tesseract/wiki/ViewerDebugging

 ↑の引用部のURLにアクセスしてページなかほどまでいくと、こんな風なやつだよ、と紹介されていました。うーん、とりあえず今はいらないかなぁ。そもそもGUI環境ないし。

 

本体インストールとその前提パッケージの他には、学習済みのデータ(trained data)も必要となります。少なくとも、EnglishとOSDの学習済みデータファイルが必要。OSDは”Orientation and Script Detection”の略のようで、文字が並ぶ方向と書式を検出するための学習データのようです。正直よくわからんです。これに加えて日本語OCRをやるなら日本語用のデータファイルが必要。

 

さて、必要なものはわかりました。ここで問題。上記リンクのページを見ると前提パッケージのインストールコマンドが記載されているんですが、こんな感じで記載されています。

apt-get install automake ca-certificates g++ git libtool libleptonica-dev make pkg-config

 

そう、apt-getコマンドなんですね。Debian系が前提となっている模様。CentOSではapt-getをyumに書き換えるだけでいいのかしら?後から知ったんですが、CentOSでaptコマンドを入れることもできるようなので、そういう手もあったなと思います(やったことないから上手くいくかは知りません)。

  

Leptonicaのインストール

まずはLeptonicaからいきましょう。前項に書いた通り、現在のTesseractではバージョン1.74以上が必要です。Leptonicaは画像処理、画像解析に使われるオープンソースソフトウェアです。と、Leptonica公式に書いてありました。

Leptonica is a pedagogically-oriented open source site containing software that is broadly useful for image processing and image analysis applications.

http://www.leptonica.org/

  

まずは軽くyumってみましょう。 

# yum info leptonica-devel
(中略)
利用可能なパッケージ
名前 : leptonica-devel
アーキテクチャー : x86_64
バージョン : 1.72
リリース : 2.el7
容量 : 108 k
リポジトリー : epel/x86_64
要約 : Development files for leptonica
URL : http://code.google.com/p/leptonica/
ライセンス : Leptonica
説明 : The leptonica-devel package contains header files fo developing applications that use leptonica.
  

むむむ、Leptonicaのバージョンが届かない。んー、ソースインストールしたくないなーと思っていたら、閃いた。前項でTesseract4のバイナリインストールをしてるわけだから、Tesseract4を取ってきたレポジトリにLeptonicaがあるんじゃまいか、と。

 

yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_7/

私は前項のTesseract4を入れてから仮想環境のスナップショットを使ってインストール前の環境に戻っていました。なので、yum-config-managerを使って再度レポジトリを追加しています。アゲーン。

# yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_7/

# yum info leptonica-devel
(中略)
利用可能なパッケージ
名前 : leptonica-devel
アーキテクチャー : x86_64
バージョン : 1.76.0
リリース : 2.2
容量 : 116 k
リポジトリー : download.opensuse.org_repositories_home_Alexander_Pozdnyakov_CentOS_7_

 

 

正解っ!leptonica-develの1.76をバイナリインストールできる。したらば、キチンと公開鍵をインポートしてyumインストール!

yum-config-manager --add-repo https://download.opensuse.org/repositories/home:/Alexander_Pozdnyakov/CentOS_7/
sudo rpm --import https://build.opensuse.org/projects/home:Alexander_Pozdnyakov/public_key
yum update
yum install leptonica-devel

 

ぽちっ

# yum install leptonica-devel
================================================================================
Package アーキテクチャー
バージョン リポジトリー 容量
================================================================================
インストール中:
leptonica-devel x86_64 1.76.0-2.2 download.opensuse.org_repositories_home_Alexander_Pozdnyakov_CentOS_7_
116 k
依存性関連でのインストールをします:
jbigkit-libs x86_64 2.0-11.el7 base 46 k
leptonica x86_64 1.76.0-2.2 download.opensuse.org_repositories_home_Alexander_Pozdnyakov_CentOS_7_
1.0 M
libjpeg-turbo x86_64 1.2.90-8.el7 base 135 k
libtiff x86_64 4.0.3-32.el7 base 171 k
libwebp x86_64 0.3.0-7.el7 base

 

leptonica-develをインストールしたときの出力の抜粋です。どうやらleptonica本体も一緒にインストールされている模様。

 

あと/usr/include/leptonicaと/usr/lib64に何かいっぱい置かれてますね。

# rpm -ql leptonica-devel
/usr/include/leptonica
/usr/include/leptonica/allheaders.h

(/usr/include/leptonica/〇〇.hがいっぱい出るので略)

/usr/lib64/liblept.so
/usr/lib64/pkgconfig/lept.pc

 

ソースからLeptonicaを入れてうまくいかない場合は、これが参考になるかも。

Finally, if you find that the installed programs are unable to link at runtime to the installed library, which is in /usr/local/lib, try to run configure in this way:
LDFLAGS="-Wl,-rpath -Wl,/usr/local/lib" ./configure

http://www.leptonica.org/source/README.html

 

後述するTesseractのconfigureをたたいた時にLeptonicaを認識しない場合は、pkgconfig向けに以下の環境変数をセットする。

$ export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

https://stackoverflow.com/questions/49107733/error-when-installing-tesseract-3-05-in-centos7-error-leptonica-1-74-or-higher

 

 

その他ライブラリをインストール

Wikiページの手順を参照するとmakeだとか、pkg-configだとか、あれ入れろこれ入れろと書いてあるので1つずつやっていきましょう。まずは復習。

 

本体のビルド

①Tesseract本体のソースコード(Githubから)
②学習済みデータ(少なくとも英語とOSDのデータが必要)
③前提パッケージ
automake, ca-certificates, gcc, g++ (compiler with c++11 support), git, libtool,libleptonica-dev v1.74.2 or later, make, pkg-config

オプション機能のビルド

★manpageのビルド
 asciidoc, docbook-xsl, xsltproc
★Tesseract training toolsのビルド
 libpango1.0-dev, libicu-dev, libcairo2-dev
★Tesseract with TensorFlowのビルド
 libprotoc-dev, libtensorflow-dev
★ScrollViewのビルド
 curl

 

基本的には ”yum install パッケージ名” を実行してインストールしていきます。パッケージの存在やインストール状況をみるときは、”yum info パッケージ名”ですね。

私のCentOS7は過去に色々いじくり回していたサーバだったからか、けっこうインストール済みでした。調べた内容とやったこと、注意点をメモします。

 

g++:

「gcc-c++」という名前のパッケージでインストール済み。

pkg-config:

「pkgconfig」という名前のパッケージでインストール済み

asciidoc:

未インストールだったのでインストール。asciidocの前提となるlibxsltとdocbook-style-xslが一緒にインストールされた。cairo-1.15、pango-1.42、libicu-50.2も一緒にインストールされるけど、この3つはフェイクなので注意。後述。

docbook-xsl:

CentOSでは「docbook-style-xsl」というパッケージ名。asciidocのインストールで勝手に入ってきた。

xsltproc:

CentOSでは「libxslt」というパッケージ名。これもasciidocのインストールで勝手に入ってきた。

libpango1.0-dev:

CentOSでは「pango-devel」というパッケージ名。asciidocのインストールでpango-1.42が入ってきたけど、「pango-devel」じゃないとだめ。pango-develをインストールすると「cairo-devel」↓も一緒に入ってくる。ちなみにpangoはテキストのレンダリングとレイアウトをやってくれるライブラリです。(Pango is a library for laying out and rendering of text :https://pango.gnome.org/

libcairo2-dev:

CentOSでは「cairo-devel」というパッケージ名。これもasciidocで入ってくるcairo-1.15ではだめ。cairo-develが必要。pango-develを入れると一緒に入ってくるのでインストール作業は不要。cairoはベクトル型の描画ができるグラフィックライブラリです。(https://ja.wikipedia.org/wiki/Cairo

libicu-devel:

CentOSでは「libicu-devel」というパッケージ名。これもpango-develのインストールで勝手に入ってきてくれる。icuはUnicodeの変換をやってくれるライブラリ。(http://site.icu-project.org/home

※最初準備しているときは知らなかったですが、libicuはバージョン52.1以降が必要となります。以降、インストール作業のところですったもんだを書きます。

libprotoc-dev:

CentOSでは「protobuf」「protobuf-devel」というパッケージ名らしい。が、これらのパッケージ内にはTesseractをビルドするためのファイルが含まれてなかった(バージョン依存っぽい感じ)です。Tensorflowをいれたときに一緒に入ってくるやつでいいっぽい。Protocol Buffersはインタフェース定義言語。詳しくはこちら。https://ja.wikipedia.org/wiki/Protocol_Buffers

libtensorflow-dev:

libtensorflow-devというパッケージは見つからなかった。結果的にpipでTensorFlowを入れることでTesseractのconfigureには認識された。あとpipで入れることでprotobufも入ってくる。 

# pip install tensorflow
(中略)
Successfully installed absl-py-0.8.1 astor-0.8.0 gast-0.3.2 google-pasta-0.1.7 grpcio-1.24.3 h5py-2.10.0 keras-applications-1.0.8 keras-preprocessing-1.1.0 markdown-3.1.1 numpy-1.17.3 protobuf-3.10.0 setuptools-41.5.1 tensorboard-1.14.0 tensorflow-1.14.0 tensorflow-estimator-1.14.0 termcolor-1.1.0 werkzeug-0.16.0 wheel-0.33.6 wrapt-1.11.2

 

Protocol BuffersとTensorflowは無くても大丈夫。というか組み込んでビルドするのは出来なかった。最終章を参照。

 

と、まぁ色々とインストールしたわけですが、足りなかったり認識できないパッケージがあったら、インストール工程の中で「〇〇が見つかりません」ってエラーが吐かれるんで、それを元にポチポチ対応してく感じになるでしょう。

 

 

Tesseract5.0.0-alphaビルド

よし。前提パッケージのインストールが終わったので、本体いきましょう、本体。再掲となりますが、GithubのWikiページに手順があるので、そこを見ておさらい。ここね。

Compiling – GitInstallation · tesseract-ocr/tesseract Wiki · GitHub

 

まずはgitコマンドでソースをがっつり持ってくると。

git clone https://github.com/tesseract-ocr/tesseract.git

んで、持ってきたソースディレクトリの中に入って、autogen.sh、configureとシェルを叩き、makeとmake installとldconfig。

    cd tesseract
    ./autogen.sh
    ./configure
    make
    sudo make install
    sudo ldconfig
    make training
    sudo make training-install

めっちゃシンプルやないの。いつものやつですね。

 

早速やっていきましょう。ポチッ、ポチッ、ポチッ

# git clone https://github.com/tesseract-ocr/tesseract.git
# cd tesseract
# ./autogen.sh

 

続いてconfigureです。ポチッ

# ./configure

(中略)

configure: WARNING: icu 52.1 or higher is required, but was not found.
configure: WARNING: Training tools WILL NOT be built.
configure: WARNING: Try to install libicu-devel package.

Configuration is done.
You can now build and install tesseract by running:

$ make
$ sudo make install
$ sudo ldconfig

This will also build the documentation.

You can not build training tools because of missing dependency.
Check configure output for details.

 

 

一応最後まで行ったけど、Training Toolのビルドが出来なかったと書いてあります。

Training Toolに必要なパッケージ:libpango1.0-dev, libicu-dev, libcairo2-dev

 

どうやらyumでいれたlibicuがバージョン50.2で、tesseractは52.1以上を要求してるので、そこが問題みたいです。(ちっ)

 

 

icuを入れてきましょう。まず以下のサイトでバージョンを確認する。
Downloading ICU - ICU - International Components for Unicode

で、インストールしたいバージョンのリンクを踏み、ソースファイルの場所(Github)に飛ぶ。現時点ではv65.1が最新のようなので、以下のリンク先が最新。
Release ICU 65.1 · unicode-org/icu · GitHub

このリンク先ページの下部にあるファイル一覧からソースファイルのURLを取得。65.1のソースファイルは、こんなURL。
https://github.com/unicode-org/icu/releases/download/release-65-1/icu4c-65_1-src.zip

 

で、このURLを使って、 

# mkdir icu-install
# cd icu-install
# wget https://github.com/unicode-org/icu/releases/download/release-65-1/icu4c-65_1-src.tgz
# tar zxvf icu4c-65_1-src.tgz
# cd icu4c-65_1-src
# ./configure
# make
# make install

  

最後に確認してるときに気づきましたが、Release ICU 65.1 · unicode-org/icu · GitHubここにある「icu4c-65_1-Fedora30-x64.tgz」このファイルってバイナリ版でしたね。このファイル落としてきて解凍して/usr/local配下にコピーするだけでよかったぽいですわ。ざわざわビルドする必要なかったじゃないの。

 

これでicuの最新版が入った。ということで、再度tesseractディレクトリの./configureをぽちっとな。 

# ./configure

(中略)

configure: WARNING: icu 52.1 or higher is required, but was not found.
configure: WARNING: Training tools WILL NOT be built.
configure: WARNING: Try to install libicu-devel package.

 

むむむ、変わらず。きっと今どきのサーバ屋さんなら速攻解決なんだろうけど、なかなかこいつを解消できなかったです。LD_LIBRARY_PATHに書いたり、/etc/ld.so.conf配下に書き込んだり。で、ふと思い出した。パッケージ管理をやってるのは、pkgconfigやんかと。pkgconfigに新しいlibicuのパスを教えてやらんといけんはずや!(既に以下の環境変数を持ってる方は追記にしてください)

# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

 

再々度、ぽちっ!

# ./configure

(中略) 

Configuration is done.
You can now build and install tesseract by running:

$ make
$ sudo make install
$ sudo ldconfig

This will also build the documentation.

Training tools can be built and installed with:

$ make training
$ sudo make training-install

  

通った!!!
よし、出力されたメッセージ通り、続いてmakeだね。ポチッ。

# make
(中略)
params.cpp:43:23: error: ‘nameoffset’ was not declared in this scope if (!fp.Open(file + nameoffset, nullptr)) {

 

エラーでござる。

 

ここは一つ、ググれの精神。なんと私がトライした前日にStack overflowに同じエラーをポストしている人がいた。これは運命なのかしら、、、助かりますた。このあたりのエラーはそのうち出なくなると思いますけど。解決策はこちら。手動でsrc/ccutil/params.cppファイルを修正。

 

再実行。

# make

(中略)

/usr/include/c++/4.8.2/functional:2471:71: error: return-statement with a value, in function
returning 'void' [-fpermissive]
return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...);

 

エラーでござる2。

 

こちらもググって見ると、↓のIssueで上がってる話と完全な同件でした。

 

曰く、(意訳です)

「てめーのコンパイラのバージョンが古すぎんだよっ!」
「最新のコンパイラが使えないなら、最新のソフトウェアは使えないもんだ」

 

Github怖い・・・ガクガク((( ;゚Д゚)))ブルブル

 

Issueを上げた人の環境ではgccのバージョンが4.8.5と記載されています。そして私の環境も同じく4.8.5。ってちょっと待って。ここにこう書いてあるじゃない。

GCC 4.8 and above

https://github.com/tesseract-ocr/tesseract

 

まぁ開発中のプロジェクトですしね。。。文句は言えないところです。とりあえず考えられる原因から潰していかにゃならんので、コンパイラのアップデートしますか。(したくないなー・・・)

 

gccのアップデート

ミラー一覧
https://gcc.gnu.org/mirrors.html

バージョン一覧
http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/

現時点の最新は9.2.0
http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-9.2.0/

 

こちらの方のブログエントリを参照してアップデートしました。

 

おらっ!

# wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-9.2.0/gcc-9.2.0.tar.gz
# tar zxvf gcc-9.2.0.tar.gz
# cd gcc-9.2.0
# ./contrib/download_prerequisites
# ./configure
# make
(待つこと2時間)
../.././libgomp/task.c:2328:1: fatal error: error writing to /tmp/cceVUPnA.s: デバイスに空き領域がありません

 

うがぁぁぁあああああ!今日の昼まで5GB空いてたのにーっ!!!

・・・はぁあ・・・(ちっ)

 

LVM拡張しようかとも思いましたが、幸いスナップショットを撮っていたので巻き戻してクリーンな環境からやり直し。ゴミもないしこっちからなら行けるでしょ。

 

今度は上でリンクをはったブログエントリを ”ちゃんと” 読んで、CとC++だけインストールするようにしました。 

# ./configure --enable-languages=c,c++ --prefix=/usr/local --disable-bootstrap --disable-multilib
# make
# make install

 

よし、gcc-9.2.0のインストール完了。

gccのインストール後、gcc --versionを実行しても古い方を見に行ってしまってました。再起動orシェルの入り直しで新バージョンを見に行くようになったので、ご参考までに。

 

スナップショットで巻き戻した分の作業を再度実施。えーっとlibicuを手動で入れるんやったね。  

# wget ~~~~~~
# ./configure
# make

警告警告警告警告警告警告警告警告警告警告警告警告警告警告
(すいません、発狂してエラーメッセージを保存し忘れました)

  

何かがOverflowしてるとかってエラーが出ました。環境的に変えたのはgcc(g++)のバージョンくらい。エラーメッセージでググって見ましたが、いまいち原因がわからず。

再度、gcc-9.2.0をインストールする前の環境までスナップショットで戻って、 libicuをインストール。問題無く入りました。つまりgcc9.2.0でlibicuの最新版(65-1)をコンパイルするとダメってことみたいす。いや、ダメってことは無いだろうけど、もうひと手間なにかが必要ということでしょう。純粋なアプリケーションの世界はよくわからないです。

 

libicuは無事に最新版が入りました。巻き戻した作業をもう一度。 

・gccのバージョンアップ
・他のライブラリも入れなおす(スナップショットで戻った分)
 Leptonica-devel, asciidoc, pango-devel, Tensorflow, もろもろ

 

スナップショットで戻ったりしたので一旦整理しておきます。

①gcc-4.8.5でTesseractビルド
 →ダメ。GithubのIssueを読むと、「古すぎ」と言われてる。

②gcc-9.2.0にバージョンアップ
 →ダメ。libicuの最新版がビルドできなくなった。

③gcc-4.8.5でlibicuをビルドインストールしてから、gccを9.2.0にアップ
 →うまくいった。

 

それではもう一度、心をこめて。 

# ./autogen.sh
# ./configure

Configuration is done.
You can now build and install tesseract by running:
$ make
$ sudo make install
$ sudo ldconfig

This will also build the documentation.

Training tools can be built and installed with:

$ make training
$ sudo make training-install

 

よしっ!configureはちゃんと通る!

ん?うーん、うまくいったっぽいけど、よくよく出力を見ると、以下が気になる。 

checking tensorflow/core/framework/graph.pb.h usability... no
checking tensorflow/core/framework/graph.pb.h presence... no
checking for tensorflow/core/framework/graph.pb.h... no

 

たぶんこれgccを上げたから出たわけじゃなくて、最初から出てたメッセージだと思います。(ログを保存してなかったので確かじゃないですけど)

うーん、TensorFlow入れたのになぁ。TensorFlowのgraph.pb.hというファイルを探しているらしい。これをググって見ると、

graph.pb.h missing · Issue #1890 · tensorflow/tensorflow · GitHub

うーん、bazelでビルドしろと?(bazel:ビルドツールです)

 

とりあえずエラーが出てるわけじゃないし、makeが通るかみてみましょう。うりゃっ! 

# make

・・・・(待つこと30分)・・・・

make[2]: ディレクトリ `/root/tesseract/tesseract/tesseract/unittest' から出ます
make[1]: ディレクトリ `/root/tesseract/tesseract/tesseract' から出ます

 

特にエラーなくmakeが通った!!!よしっ!!!
ゆけっ!すすめっ!インストールのその先へっ!

# make install

(中略)

make[2]: `install-exec-am' に対して行うべき事はありません.
make[2]: `install-data-am' に対して行うべき事はありません.
make[2]: ディレクトリ `/root/tesseract/tesseract-src/tesseract/unittest' から出 ます
make[1]: ディレクトリ `/root/tesseract/tesseract-src/tesseract/unittest' から出 ます

 

完了しました。成功です!おめでとう!
とか、そういう祝福のメッセージなどもなく普通に終了。これ出来たんか???

 

とりあえずバージョン情報を見てみる。

# tesseract --version
tesseract 5.0.0-alpha-526-gc7c2
leptonica-1.76.0
libjpeg 6b (libjpeg-turbo 1.2.90) : libpng 1.5.13 : libtiff 4.0.3 : zlib 1.2.7 : libwebp 0.3.0
Found AVX2
Found AVX
Found SSE

 

おっ、入ったっぽいぞ!えいっ! 

# tesseract test1.JPG stdout -l eng
Error opening data file /usr/local/share/tessdata/eng.traineddata
Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory.
Failed loading language 'eng'
Tesseract couldn't load any languages!
Could not initialize tesseract.

  

エラーですけど、とりまインストールはできたらしい。上の方で調べたこれか。

You need to install at least English language and OSD traineddata files to TESSDATA_PREFIX directory.

https://github.com/tesseract-ocr/tesseract/blob/master/INSTALL.GIT.md

 

学習済みデータがねーぞと。ここに置かないといけなかったですね。

# ls -l /usr/local/share/tessdata/
合計 8
drwxr-xr-x 2 root root 4096 10月 21 18:43 configs
-rw-r--r-- 1 root root 572 10月 21 18:43 pdf.ttf
drwxr-xr-x 2 root root 98 10月 21 18:43 tessconfigs

 

うん、確かに無い。Githubから落としてきたengとosdのデータセットを配置して、再度実行しましょう。

 

f:id:kwnflog:20191026162146j:plain

※誤認識してる文字は赤字にしています。

# tesseract test1.JPG stdout -l eng
01. a large collection of actual questions
02. a comprehensive presentation of English grammar points
03. a combination of fundamental theories and actual practice
04. Typical questions and useful tips for doing the TOEIC Reading test
05. Essential vocabulary and expressions for each part of the TOEIC Reading
test
06. Two actual tests

 

おーっ!動いたっ!!!
ちなみにtesseract4で普通の言語パックを使って読み取ったときは、こんな感じでした。

# tesseract test1.JPG stdout -l eng
O1.a large collection of actual questions
02.4 comprehensive presentation of English grammar points
03.4 combination of fundamental theories and actual practice
04. Typical questions and useful tips for doing the TOEIC Reading test
05. Essential vocabulary and expressions for each part of the TOEIC Reading
test
06. Two actual tests

 
 

ちょっとだけ賢くなったような印象ですね。v4では、0(半角ゼロ)をO(大文字オー)に読んでいたのが、v5では正しくゼロと読めています。Aの一部が4と読まれるのもなくなりましたが、今度はそれが小文字aになっています。

これはよくわからんです。行頭に「01.」なんかの文字が含まれているので、大文字Aはおかしいと判断してるのかなぁ。他の部分の精度から考えてAをaと誤認識したというより、自然言語のルール的におかしいと判断した結果としてaになっているような気がします。精度はそこそこって感じですけど、やっぱり調教するのがデフォってことなんでしょうか。

 

とりあえず動いたんで、training toolもビルドしてみましょう。./configureの最後に出力されたメッセージから、以下のコマンドで入るようです。 

# make training
# make training-install

  

これもうまく入ったっぽい。/usr/local/binに以下のファイルが追加されてました。

# ls -lt /usr/local/bin
合計 81924
-rw-r--r-- 1 root root 22203 10月 21 20:49 tesstrain_utils.sh
-rwxr-xr-x 1 root root 4097 10月 21 20:49 tesstrain.sh
-rwxr-xr-x 1 root root 34578 10月 21 20:49 language-specific.sh
-rwxr-xr-x 1 root root 2234952 10月 21 20:49 shapeclustering
-rwxr-xr-x 1 root root 2427488 10月 21 20:49 mftraining
-rwxr-xr-x 1 root root 2166888 10月 21 20:49 cntraining
-rwxr-xr-x 1 root root 2235888 10月 21 20:49 classifier_tester
-rwxr-xr-x 1 root root 577312 10月 21 20:49 ambiguous_words
-rwxr-xr-x 1 root root 460776 10月 21 20:49 wordlist2dawg
-rwxr-xr-x 1 root root 2447160 10月 21 20:49 unicharset_extractor
-rwxr-xr-x 1 root root 4190168 10月 21 20:49 text2image
-rwxr-xr-x 1 root root 3728160 10月 21 20:49 set_unicharset_properties
-rwxr-xr-x 1 root root 228000 10月 21 20:49 merge_unicharsets
-rwxr-xr-x 1 root root 4484704 10月 21 20:49 lstmtraining
-rwxr-xr-x 1 root root 4349240 10月 21 20:49 lstmeval
-rwxr-xr-x 1 root root 303560 10月 21 20:49 dawg2wordlist
-rwxr-xr-x 1 root root 825600 10月 21 20:49 combine_tessdata
-rwxr-xr-x 1 root root 2442704 10月 21 20:49 combine_lang_model
(以下、省略)

 

 

Tensorflowを入れてコンパイル(したかったが出来なかった話)

ふー、何とかTraining Toolのインストールまで出来ましたけど、ここまで出来ると逆にTensorflowを認識してくれなかったのが気になってきます。(Tesseractを使う中でTensorflowがどう使われるのか具体的なところは全く知らないのですが)

checking tensorflow/core/framework/graph.pb.h usability... no
checking tensorflow/core/framework/graph.pb.h presence... no
checking for tensorflow/core/framework/graph.pb.h... no

  

うーんなんか↓のパス見覚えがあるんだよなぁ

tensorflow/core/framework/graph.pb.h

つってたらふいに思い出しました。これPythonのパッケージのパスに似てるなと。

 

graph.pb.hでシステム内検索してみましょう。 

# find / -name "graph.pb.h"
/usr/local/python365/lib/python3.6/site-packages/tensorflow/include/tensorflow/core/framework/graph.pb.h

  

変なパス名ですけど、Python3のパッケージを格納しているパスですね。つーか探しているファイルとパスは存在している。が、それをconfigureスクリプトが見つけられていないと。なぜなんだぜ。

 

あぁ、もしやしてCentOS7のデフォルトPythonが2系だからか。Python2のsite-packageを見に行っちゃってるパティーンじゃないかしら? 

# ls -al /usr/bin/python*
lrwxrwxrwx 1 root root 7 10月 17 21:15 /usr/bin/python -> python2
lrwxrwxrwx 1 root root 9 10月 17 21:15 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 7216 8月 7 09:52 /usr/bin/python2.7
lrwxrwxrwx. 1 root root 32 4月 2 2018 /usr/bin/python3 -> /usr/local/python365/bin/python3

 

/usr/bin/pythonでpython3が動作するようにシンボリックリンクを変えて、./configure。
→変わらずTesseractのconfigureスクリプトは認識してくれず。

 

調べていてちょっと気になったのがこれ。

On Debian, installation of the packages libtensorflow-dev and libprotoc-dev is required. The include path is currently hardcoded for those to /usr/include/tensorflow.

/usr/includeにハードコードされてる?

 

ちょっと待て。略して、ちょま。configureスクリプトを走らせたときにでるメッセージをもう一度みてみよう。 

checking tensorflow/core/framework/graph.pb.h usability... no
checking tensorflow/core/framework/graph.pb.h presence... no
checking for tensorflow/core/framework/graph.pb.h... no

  

このtensorflowから始まるパスってどこを指しているんだろう。本来はソースを追っかけていくべきなんでしょうが、私にはそんなスキルはありません。(というか調べてみてわからんかった)

つーわけで逆説的に考えよう。上の情報で/usr/includeにハードコードされているという情報があるわけなので、/usr/include/tensorflow/core/framework/graph.pb.hを探しているんじゃなかろうか。configureスクリプトもどうせgraph.pb.hファイルの中身までは見てないと仮定して、上記パスを手動で追加、graph.pb.hの空ファイルを作成してconfigureを走らせてみよう。 

# mkdir -p /usr/include/tensorflow/core/framework
# touch /usr/include/tensorflow/core/framework/graph.pb.h
# ./configure

(中略)

checking tensorflow/core/framework/graph.pb.h usability... yes
checking tensorflow/core/framework/graph.pb.h presence... yes
checking for tensorflow/core/framework/graph.pb.h... yes

 

あ、当たった(笑)。ここに落ちてくるようにtensorflowを入れればよろしいのですね。

・・・これ、tensorflowを手動でビルドしないといけないのかなぁ。なんかコンパイルツールのBazelとかいうのが必要らしいので、正直やりたくない。もうサーバの空き容量が全くもってないのでござるよ。

ただちょいと調べると、Bazelを使ってビルドしたファイルは最終的にはpipでインストールするらしい。pipの時にディレクトリを指定すればいいのではないか。

# pip install tensorflow -t /usr/include/tensorflow

→全然ダメ。パス名が合わない。

 

うーん・・・閃いたっ!/usr/include/tensorflow/core/framework/graph.pb.h が見えればいいだけならば、何もここにブッ込まなくてもリンク貼っておけばよろしいのではないか? 

# cd/usr/include
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/tensorflow tensorflow

 

だがしかし、tesseractのconfigureがうまく認識してくれない。パスと空ファイルを作ったときはconfigureで認識してくれたけど、実態のgraph.pb.hへつながるようにシンボリックリンクを貼っただけじゃ認識してくれない。なぜなんだぜ。おかしいのだぜ。

 

ここで初心に戻ってconfigureのログを見てみた。

# view config.log

(中略)

configure:5050: checking tensorflow/core/framework/graph.pb.h usability
configure:5050: g++ -c -g -O2 conftest.cpp >&5
In file included from conftest.cpp:58:
/usr/local/include/tensorflow/core/framework/graph.pb.h:10:10: fatal err or: google/protobuf/port_def.inc: No such file or directory
10 | #include <google/protobuf/port_def.inc>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

 

あれ?これエラーになってるのって、protobufってやつじゃないの。ん?

# find / -name "port_def.inc"

/usr/local/python365/lib/python3.6/site-packages/tensorflow/include/external/protobuf_archive/src/google/protobuf/port_def.inc
/usr/local/python365/lib/python3.6/site-packages/tensorflow/include/google/protobuf/port_def.inc

 

これは後から調べてわかったんですが、このport_def.incってファイルはyum install protobufとかprotobuf-develでは入ってきませんでした。pipでTensorflowをインストールしたときにprotobuf-3.10が一緒に入ってきて、その中に含まれていました。yumで入れられるprotobufはバージョン2.5なので、おそらくその違いかと思われます。

 

# pip download -d :all: tensorflow

(もろもろ)

Saved ./:all:/protobuf-3.10.0-cp36-cp36m-manylinux1_x86_64.whl

(もろもろ)

 

そいじゃ、graph.pb.hにやったのと同じように/usr/includeに存在するかのようにリンクを貼ってみましょう。 

# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/google google

 

もういっちょconfigure

# ./configure

checking tensorflow/core/framework/graph.pb.h usability... yes
checking tensorflow/core/framework/graph.pb.h presence... yes
checking for tensorflow/core/framework/graph.pb.h... yes

 

そーゆーことかっ!

いや実をいうと、ちょっとおかしいなとは思っていたんですよ。configureスクリプトの出力にprotobufferの確認結果が表示されていなかったんです。インストールに必要な他のパッケージって全部yes/noって判定結果が表示されるんですけど、このprotobufferってのはなぜだか表示されていなかったんです。「おかしいなー、おかしいなー」と稲川某のように呟いていたんですけど適当に流していました。

configureスクリプトのtensorflowのyes/no表示はtensorflowとprotobufferの両方が認識されてはじめてyesの表示になると、そういうことらしいです。

 

あとちなみに、ここでは/usr/includeにリンクを貼ってますが、/usr/local/includeにリンクを貼ってもconfigureに認識されました。ソースを検索していると、/usr/includeはoldincludepathという変数にぶち込まれていましたので、推奨ではないor将来変更となるのかもしれません。

 

ここで最後の疑問。シンボリックリンクを貼るって対策で正解なのか?

これはぶっちゃけわかりません。とりあえずconfigureは通して、エラーが無いことは確認できましたが、Githubに書かれている手順とは違うものです。

OCRエンジンをただ使うとか、追加学習するなかではTensorflowは使わないようなので、普段使い(どういうシチュエーションだろう)する分には問題ないと思います。

 

調べてみると面白いissueを発見

 これによるとTesseractレポジトリのContributorの方もpipでインストールしたtensorflow(とprotobuf)にシンボリックリンクを貼っています。ので多分おっけー。

 

あと!

As long as we don't have a TensorFlow model which works with Tesseract, the TensorFlow code in Tesseract is rather useless. I'd compile without it (unless you have such models).

意訳:Tesseractで動作するTensorflowモデルを持っていない限り、TesseractのなかのTensorflowコードはむしろ役に立たない。ワイはTensorflowなしでコンパイルするよ

 

これって、追加学習させるのにTensorflowを意図的に使うよう明示しないなら、Tensorflowいらないってことみたいですね。たしかにTraining Toolのビルドには以下のコマンドを打てとインストールマニュアルには書いてありました。

sudo apt-get install libicu-dev
sudo apt-get install libpango1.0-dev
sudo apt-get install libcairo2-dev

 

そしてこのTraining Toolとは別に「Tensorflowを組み込んでビルドする」という項目があったので、単純に追加学習させるだけなら別にTensorflowいらないってことになるっぽいです。うん、なるんでしょう、たぶん。

 

では、最後にサクッとTensorflowを組み込んだ形でコンパイルして、インストール作業を終わりとしましょう。

 

# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/google google
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/tensorflow tensorflow
# ./configure

(中略)

checking tensorflow/core/framework/graph.pb.h usability... yes
checking tensorflow/core/framework/graph.pb.h presence... yes
checking for tensorflow/core/framework/graph.pb.h... yes

# make

(中略)
In file included from tfnetwork.h:29,
from network.cpp:42:
tfnetwork.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
17 | #error This file was generated by an older version of protoc which is
| ^~~~~
tfnetwork.pb.h:18:2: error: #error incompatible with your Protocol Buffer headers. Please
18 | #error incompatible with your Protocol Buffer headers. Please
| ^~~~~
tfnetwork.pb.h:19:2: error: #error regenerate this file with a newer version of protoc.
19 | #error regenerate this file with a newer version of protoc.
| ^~~~~
In file included from /usr/local/include/tensorflow/core/public/session.h:24,
from tfnetwork.h:31,
from network.cpp:42:
/usr/local/include/tensorflow/core/framework/tensor.h:21:10: fatal error: third_party/eigen3/unsupported/Eigen/CXX11/Tensor: No such file or directory
21 | #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [network.lo] エラー 1
make[2]: ディレクトリ `/root/tesseract/tesseract-src/tesseract/src/lstm' から出 ます
make[1]: *** [all-recursive] エラー 1
make[1]: ディレクトリ `/root/tesseract/tesseract-src/tesseract' から出ます
make: *** [all] エラー 2

 

うん、エラーだ。んー、エラーとしては以下の2つが出てるのかなぁ。

①protocが古い

#error: This file was generated by an older version of protoc which is
#error: incompatible with your Protocol Buffer headers. Please
#error: regenerate this file with a newer version of protoc.

②Tensorファイルがない

/usr/local/include/tensorflow/core/framework/tensor.h:21:10: fatal error: third_party/eigen3/unsupported/Eigen/CXX11/Tensor: No such file or directory

 

とりあえず①を潰せば②も消えそうな気がします。というか、そう期待します。①はググって見ると、これを発見。

 

このリンクの件では、AnacondaとaptのそれぞれでProtobufをインストールしていたことが原因で上記のエラーが発生しているとのことでした。うん、ちょっと思い当たる節があるぞ。Tesseractの./configure通すためにガチャガチャとサーバを弄りまわしていたときに、yumでprotobufを入れたような気が。

# whereis protoc
protoc: /usr/bin/protoc

# /usr/bin/protoc --version
libprotoc 2.5.0

# yum list installed |grep proto
protobuf.x86_64 2.5.0-8.el7 @base
protobuf-compiler.x86_64 2.5.0-8.el7 @base
protobuf-devel.x86_64 2.5.0-8.el7 @base
xorg-x11-proto-devel.noarch 2018.4-1.el7 @base

# pip show protobuf
Name: protobuf
Version: 3.10.0

 

yumで入れた方は2.5.0、pipで入れたのが3.10.0。yumで入れたほうが使われているような...

# yum remove protobuf
# make clean
# ./configure
# make

 

yumでprotobufを削除すれば、依存性の関係で一通り消えてくれました。で、makeコマンドを再実行したものの同じエラーで瞬殺されたのでお掃除のmake clean、configureでちゃんと読みこめているか確認して、トドメのmake。

 

→まったく同じエラー

 

んー、protobuf-3.10って古いのかしら?

 

現時点のリリース版最新がv3.10.1なので、古いわけではなさそう。

ということは②のエラーの方から潰していけばいいのかな。②のほうはファイルが無いよというエラーですね。探しているファイルはこれ。

third_party/eigen3/unsupported/Eigen/CXX11/Tensor

 

# find / -name third_party

(いくつか表示されるものの省略)

/usr/local/python365/lib/python3.6/site-packages/tensorflow/include/third_party

# ls -l /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor

-rw-r--r-- 1 root root 420 10月 21 16:55 /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor

 

実態はPythonのパッケージ配下のTensorflowの中にいるディレクトリのようです。いま貼っているリンクを再確認。

# ls -l /usr/local/include
合計 12
drwxr-xr-x 3 root root 19 10月 21 16:27 c++
lrwxrwxrwx 1 root root 74 10月 30 00:06 google -> /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/google
lrwxrwxrwx 1 root root 78 10月 30 00:07 tensorflow -> /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/tensorflow
drwxr-xr-x 2 root root 8192 10月 21 16:18 unicode

 

ふむ、頭がこんがらがってきた。こういうのやってると、自分の脳ミソに割り当てられたメモリ量が如何に少ないかわかって悲しくなりますよね。整理します。

 

make時にひっかかってるファイル(?)

/usr/local/include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor

 

探しているファイルの実体はこれ

/usr/local/python365/lib/python3.6/site-packages/tensorflow/include/third_party/eigen3/unsupported/Eigen/CXX11/Tensor

 

で、現状貼ってるリンクがこれ

/usr/local/include/tensorflow ->
/usr/local/python365/lib/python3.6/site-packages/tensorflow/include/tensorflow

 

つまり、赤字にしているthird_partyを/usr/local/include/third_partyで見えるようにせんといかんみたいですね。リンクを貼りましょ。ペシッ

 

エラーメッセージは変わったけど、同種のエラーが続きます。たぶん、

# ls -l /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/
合計 8
drwxr-xr-x 3 root root 4096 10月 21 16:57 Eigen
drwxr-xr-x 15 root root 200 10月 21 16:57 absl
drwxr-xr-x 45 root root 4096 10月 21 16:57 external
drwxr-xr-x 3 root root 22 10月 21 16:57 google
drwxr-xr-x 5 root root 56 10月 21 16:57 tensorflow
drwxr-xr-x 3 root root 20 10月 21 16:57 third_party
drwxr-xr-x 3 root root 19 10月 21 16:57 unsupported

 

これらのディレクトリをすべて/usr/local/include配下にあるように見せかけないといけないみたいです。/usr/local/includeは既に存在しているので、includeディレクトリ自体をリンクで貼るのは無理。ちょっとお下品な感じがありますけど、一個ずつリンクを貼りましょう。

 

# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/Eigen /usr/local/include/Eigen
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/absl /usr/local/include/absl
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/external /usr/local/include/external
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/tensorflow /usr/local/include/tensorflow
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/third_party/usr/local/include/third_party
# ln -s /usr/local/python365/lib/python3.6/site-packages/tensorflow/include/unsupported /usr/local/include/unsupported

 

 で、再度実行。 

# make

In file included from tfnetwork.h:29,
from network.cpp:42:
tfnetwork.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
17 | #error This file was generated by an older version of protoc which is
| ^~~~~
tfnetwork.pb.h:18:2: error: #error incompatible with your Protocol Buffer headers. Please
18 | #error incompatible with your Protocol Buffer headers. Please
| ^~~~~
tfnetwork.pb.h:19:2: error: #error regenerate this file with a newer version of protoc.
19 | #error regenerate this file with a newer version of protoc.
| ^~~~~
In file included from tfnetwork.h:29,
from network.cpp:42:
tfnetwork.pb.h:126:30: error: 'google::protobuf::uint8* tesseract::TFNetworkModel::InternalSerializeWithCachedSizesToArray(bool, google::protobuf::uint8*) cons ' marked 'final', but is not virtual
126 | ::google::protobuf::uint8* InternalSerializeWithCachedSizesToArray(
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tfnetwork.pb.h: In member function 'void tesseract::TFNetworkModel::clear_global_step()':
tfnetwork.pb.h:356:18: error: 'GOOGLE_LONGLONG' was not declared in this scope; did you mean 'GOOGLE_DLOG'?
356 | global_step_ = GOOGLE_LONGLONG(0);
| ^~~~~~~~~~~~~~~
| GOOGLE_DLOG
make[2]: *** [network.lo] エラー 1
make[2]: ディレクトリ `/root/tesseract/tesseract-src/tesseract/src/lstm' から出 ます
make[1]: *** [all-recursive] エラー 1
make[1]: ディレクトリ `/root/tesseract/tesseract-src/tesseract' から出ます
make: *** [all] エラー 2

 

むむむ、ぐぐぐ、ぐぐるか。

 

最後のところにこうコメントしてある。

I think to fix the linker error you just need to add -lprotobuf to your command to link against the protobuf runtime.

 

やべぇ、ハードル上がってきた。この場合どうなるんだろう。エラーが出る直前のコンパイル命令を見ると確かに -lprotobuf が入っていない。しかしどうだろう、いくつかのMakefileの中身を見ていくと、-lprotobuf という文言は存在しています。

# grep "lprotobuf" ./Makefile
TENSORFLOW_LIBS = -lprotobuf -ltensorflow_cc

 

再掲です。

このIssueの中で、TesseractのMake時にTensorflowとProtobufのファイルが見つからないというエラーが出ていて、シンボリックリンクで解決しています。さらに、protobufのバージョンについての言及もありますが、その部分はさらっと流されていました。

 

そして最終的にはContributorの2人もあきらめて、

 

Is there a way that I can disable tensorflow while building tesseract?
(Tesseractのビルド中にTensorflow使えなくする方法ある?)

 

Yes: PATH/configure --with-tensorflow=no ....
(あるよ!configureでオプション付けるんだよ)


The workaround was to build with --with-tensorflow=no .
(回避策はオプションつけてビルドすることね)

 

As long as we don't have a TensorFlow model which works with Tesseract, the TensorFlow code in Tesseract is rather useless. I'd compile without it (unless you have such models).
(Tesseractと一緒に動作するTensorflowのモデルを持ってないなら、TesseractのなかのTensorflowモデルはむしろ役に立たないよね。わいはTensorflow無しでコンパイルするよ)


Yes, I do compile without it. This is not a problem with Tesseract. I will close the issue.Thanks!
(そうだね、Tensorflow無しでコンパイルするよ。これはTesseractの問題じゃないよね。Issueクローズします。あんがと!)

 

と結論づけています。(上記コメントは間を省略し親しみやすい意訳をつけています。)

 

2019年10月時点で、Tensorflowを組み込んでビルドするのは、無理っ!

このような結論とさせていただきます。

 

おわりに

あー、やっと終わった。もともとTesseractをいじり始めた目的は、単純に「オープンソースOCRの精度ってどんなもんなんだろう?」と疑問に思って、それを調べるために始めました。

それが何でか、エラーと戦い、容量不足と戦い(拡張しろ)、ここまで来ました。きれいにまとまっているエントリではないですが、こういう奮戦記もWeb上にあっていいのではないかと思い、記録していたログを公開しました。本当にしんどかった・・・が、楽しかった。

Tesseractに興味を持った人がうまくインストールできなかったときに、このエントリが何かの助けになってくれたら幸いです。

 

さらば、すべてのTesseracterたちよ。また会う日まで

 

おわり