2014-01-09

rubyでOCR

このエントリーをブックマークに追加 このエントリーを含むはてなブックマーク
rubyで画像上の文字を取得するOCRをやってみした。

OCRのライブラリとしてtesseract-ocrを利用します。

なので、まずはtesseract-ocrを導入する必要があります。

環境は、CentOS6の場合です。

まず必要となるものをyumで入れておきます。

yum -y install autoconf automake libtool
yum -y install libpng-devel libjpeg-devel libtiff-devel zlib-devel


そしてLeptonica という画像解析ライブラリが必要なようなので、これをダウンロードしてコンパイルします。

wget http://leptonica.googlecode.com/files/leptonica-1.69.tar.gz
tar xvzf leptonica-1.69.tar.gz
cd leptonica-1.69
./configure
make
make install
cd ..


いよいよtesseract-ocrと解析に利用する辞書(?)をダウンロードしてコンパイルします。
辞書は英語と日本語のものを入れます。

wget http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.02.tar.gz
tar xvzf tesseract-ocr-3.02.02.tar.gz
wget http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.eng.tar.gz
wget http://tesseract-ocr.googlecode.com/files/tesseract-ocr-3.02.jpn.tar.gz
tar xvzf tesseract-ocr-3.02.eng.tar.gz
tar xvzf tesseract-ocr-3.02.jpn.tar.gz
cd tesseract-ocr
./autogen.sh
./configure
make
make install
mv tessdata/*.traineddata /usr/local/share/tessdata/


利用できるかどうか試してみます。
tesseract-ocrの中にphototest.tifという画像ファイルがあるので、これから文字を取得してみます。

tesseract phototest.tif out
cat out.txt

out.txtになんか文字が出ていればOKです。
なんかいい感じに文字が認識されているように見えます。

日本語辞書も入っていることを確認します。
tesseract phototest.tif out -l jpn
cat out.txt

こちらも何か文字がでればOKです。
こちらは内容は、いけてない感じになります。

ここまでは以下が参考になりました。
http://www.psi-net.co.jp/blog/?p=1080



そしてrubyで使うためのgemをインストールします。

gem install tesseract-ocr


rubyから使ってみます。

以下の画像をOCRにかけてみます。



rubyでocrを使うサンプルは以下のとおりです。



実際の出力は、以下のとおりです。
==== 画像全体から文字を取得します
加順ス 卜ですー
メイリ赫すー ーF^ゴシックを
使ってみました〝
ーF“明朝で†〟
ヒロシで†〟 メ
赤し丶字での 古 琶
メイリ祀す〟 茗 で
麦


==== 画像のメイリオの部分のみ
伽R折ス 卜です〟
メイリ祀す〟


==== 画像のゴシックの部分のみ
ーP^ゴシツクを
使ってみましナこ〝


==== 画像の明朝の部分のみ
ーPA明朝です〟
ヒロシです〟


==== 画像の赤いメイリオの部分のみ
赤し`字での
メイリ祀す〟


==== 画像の縦書きメイリオの部分のみ
メ縦
ィ管
リ實
オで
で
麦


==== 画像の富士山の上のメイリオの部分のみ
\ ` '/ f
~

j での

丿 イリ村す~

/


==== 認識した一文字ずつの情報
0 | text=加 pos=(x:28 y:44 w:23 h:12)
1 | text=順 pos=(x:52 y:44 w:42 h:13)
2 | text=ス pos=(x:94 y:44 w:16 h:13)
3 | text=卜 pos=(x:115 y:43 w:11 h:14)
4 | text=で pos=(x:126 y:45 w:16 h:12)
5 | text=す pos=(x:143 y:43 w:15 h:14)
6 | text=ー pos=(x:159 y:53 w:5 h:4)
7 | text=メ pos=(x:29 y:76 w:14 h:13)
8 | text=イ pos=(x:45 y:75 w:14 h:14)
9 | text=リ pos=(x:63 y:75 w:11 h:14)
10 | text=赫 pos=(x:76 y:75 w:32 h:14)
11 | text=す pos=(x:109 y:75 w:15 h:14)
12 | text=ー pos=(x:125 y:85 w:5 h:4)
13 | text=ー pos=(x:385 y:84 w:6 h:11)
14 | text=F pos=(x:393 y:84 w:7 h:11)
15 | text=^ pos=(x:400 y:84 w:8 h:11)
16 | text=ゴ pos=(x:410 y:83 w:14 h:13)
17 | text=シ pos=(x:426 y:84 w:13 h:12)
18 | text=ッ pos=(x:443 y:87 w:10 h:9)
19 | text=ク pos=(x:458 y:84 w:12 h:13)
20 | text=を pos=(x:474 y:83 w:13 h:14)
21 | text=使 pos=(x:384 y:106 w:16 h:14)
22 | text=っ pos=(x:402 y:111 w:12 h:8)
23 | text=て pos=(x:417 y:108 w:14 h:11)
24 | text=み pos=(x:433 y:107 w:15 h:13)
25 | text=ま pos=(x:450 y:106 w:12 h:13)
26 | text=し pos=(x:468 y:107 w:10 h:13)
27 | text=た pos=(x:481 y:106 w:14 h:13)
28 | text=〝 pos=(x:496 y:116 w:6 h:4)
29 | text=ー pos=(x:122 y:137 w:4 h:11)
30 | text=F pos=(x:128 y:137 w:8 h:11)
31 | text=“ pos=(x:136 y:137 w:8 h:11)
32 | text=明 pos=(x:145 y:136 w:14 h:14)
33 | text=朝 pos=(x:160 y:136 w:16 h:14)
34 | text=で pos=(x:177 y:137 w:15 h:12)
35 | text=† pos=(x:193 y:136 w:15 h:14)
36 | text=〟 pos=(x:208 y:146 w:5 h:4)
37 | text=ヒ pos=(x:122 y:161 w:12 h:12)
38 | text=ロ pos=(x:138 y:162 w:12 h:10)
39 | text=シ pos=(x:154 y:161 w:13 h:12)
40 | text=で pos=(x:169 y:161 w:15 h:12)
41 | text=† pos=(x:185 y:160 w:15 h:14)
42 | text=〟 pos=(x:200 y:170 w:5 h:4)
43 | text=メ pos=(x:506 y:170 w:13 h:14)
44 | text=赤 pos=(x:301 y:189 w:15 h:15)
45 | text=し pos=(x:318 y:192 w:7 h:11)
46 | text=丶 pos=(x:328 y:192 w:4 h:9)
47 | text=字 pos=(x:333 y:189 w:15 h:15)
48 | text=で pos=(x:349 y:192 w:15 h:12)
49 | text=の pos=(x:365 y:192 w:15 h:12)
50 | text=古 pos=(x:506 y:185 w:14 h:31)
51 | text=琶 pos=(x:537 y:169 w:16 h:46)
52 | text=メ pos=(x:302 y:223 w:13 h:13)
53 | text=イ pos=(x:318 y:222 w:14 h:14)
54 | text=リ pos=(x:336 y:222 w:10 h:14)
55 | text=祀 pos=(x:349 y:222 w:31 h:14)
56 | text=す pos=(x:382 y:222 w:14 h:14)
57 | text=〟 pos=(x:398 y:232 w:5 h:4)
58 | text=茗 pos=(x:506 y:217 w:14 h:31)
59 | text=で pos=(x:538 y:219 w:14 h:13)
60 | text=麦 pos=(x:506 y:249 w:14 h:22)

==== 認識した文字ブロックの情報
0 | text=加順ス 卜ですー
メイリ赫すー ーF^ゴシックを
使ってみました〝
ーF“明朝で†〟
ヒロシで†〟 メ
赤し丶字での 古 琶
メイリ祀す〟 茗 で
麦

 pos=(x:0 y:0 w:607 h:476)

==== 認識した文字段落(ブロックとの違いがよくわからない)の情報
0 | text=加順ス 卜ですー
メイリ赫すー ーF^ゴシックを
使ってみました〝
ーF“明朝で†〟
ヒロシで†〟 メ
赤し丶字での 古 琶
メイリ祀す〟 茗 で
麦

 pos=(x:28 y:43 w:525 h:228)

==== 認識した文字の行ごとの情報
0 | text=加順ス 卜ですー
 pos=(x:28 y:43 w:136 h:14)
1 | text=メイリ赫すー ーF^ゴシックを
 pos=(x:29 y:75 w:458 h:22)
2 | text=使ってみました〝
 pos=(x:384 y:106 w:118 h:14)
3 | text=ーF“明朝で†〟
 pos=(x:122 y:136 w:91 h:14)
4 | text=ヒロシで†〟 メ
 pos=(x:122 y:160 w:397 h:24)
5 | text=赤し丶字での 古 琶
 pos=(x:301 y:169 w:252 h:47)
6 | text=メイリ祀す〟 茗 で
 pos=(x:302 y:217 w:250 h:31)
7 | text=麦

 pos=(x:506 y:249 w:14 h:22)

==== 認識した単語のごとの情報
0 | text=加順ス pos=(x:28 y:44 w:82 h:13)
1 | text=卜ですー pos=(x:115 y:43 w:49 h:14)
2 | text=メイリ赫すー pos=(x:29 y:75 w:101 h:14)
3 | text=ーF^ゴシックを pos=(x:385 y:83 w:102 h:14)
4 | text=使ってみました〝 pos=(x:384 y:106 w:118 h:14)
5 | text=ーF“明朝で†〟 pos=(x:122 y:136 w:91 h:14)
6 | text=ヒロシで†〟 pos=(x:122 y:160 w:83 h:14)
7 | text=メ pos=(x:506 y:170 w:13 h:14)
8 | text=赤し丶字での pos=(x:301 y:189 w:79 h:15)
9 | text=古 pos=(x:506 y:185 w:14 h:31)
10 | text=琶 pos=(x:537 y:169 w:16 h:46)
11 | text=メイリ祀す〟 pos=(x:302 y:222 w:101 h:14)
12 | text=茗 pos=(x:506 y:217 w:14 h:31)
13 | text=で pos=(x:538 y:219 w:14 h:13)
14 | text=麦 pos=(x:506 y:249 w:14 h:22)


認識レベルは、なんかちょっとザンネンな感じです。

認識レベルを上げるにはどんな調整をすればよいのかは、よくわかっていません。

rubyでocrを利用することに関しては、以下が参考になりました。
http://schima.hatenablog.com/entry/2013/10/16/203616


0 件のコメント: