LoginSignup
4
6

More than 5 years have passed since last update.

Cloud Vision APIでのテキスト検出

Last updated at Posted at 2017-08-27

Cloud Vision APIのテキスト検出APIを試してみた。
いろいろな言語でのライブラリが公式から出ているようだが、
今回はRubyのものを使って試してみた。

準備

・Google Cloud Platformでプロジェクトを作成

Google Cloud Vision APIのクイックスタート
までをやる。

これでAPIで利用する、プロジェクトIDを取得できる。

実行

公式のText detection samplesの通り。

gemをインストール

gemがあるのでinstall。
(これは、テキスト検出に限らず他のcloud-vision APIもまとめて使える)

gem install google-cloud-vision

実行

準備のところで取得したプロジェクトIDを取得し実行。

# project_id = "Your Google Cloud project ID"
# image_path = "Path to local image file, eg. './image.png'"

require "google/cloud/vision"

vision = Google::Cloud::Vision.new project: project_id
image  = vision.image image_path

puts image.text

いろいろな画像で試してみる

・かなりわかりやすい画像

test.png

結果

image.text
=> #<Text text: "あいうえお\nABC\n", locale: "ja", bounds: 4, words: 2, pages: 1>

これは完璧

・文字を少し汚す

test3.png

結果

=> #<Text text: "あい うえお\nAB\n", locale: "ja", bounds: 4, words: 3, pages: 1>

微妙に読めなくなった。ノイズの色が文字と違っても、文字へのノイズ率?的なのが多いとだめな雰囲気。

・文字を汚す

結果

test2.png

image.text
=> nil

認識まったくされなくなった。
文字色は全く違うんだから、そのあたりうまくやってくれそうな気がしてたが甘かった。

・文字を回転1

test4.png

結果

image.text
=> #<Text text: "あいうえお\n", locale: "ja", bounds: 4, words: 1, pages: 1>

回転された「あいうえお」は認識されたが、回転しなかった「ABC」が認識されなくなった(wordsも2->1に)

・文字を回転2

test5.png

結果

image.text
=> #<Text text: "あいうえお\n", locale: "ja", bounds: 4, words: 1, pages: 1>

「文字を回転1」を同じ結果。
回転された「あいうえお」は認識されたが、回転しなかった「ABC」が認識されなくなった。
これ、画像内の文字がみんな同じ(ような?)角度じゃなければだめなのだろうか。

・文字を回転3

test5.png

結果

image.text
=> #<Text text: "ABC\nあいうえお\n", locale: "ja", bounds: 4, words: 2, pages: 1>

同じ角度の回転ならOKのようだ。ABCの認識が先になったので、たぶん画像を逆さのものとして最初っから検出しているようだ

・文字を回転4

test7.png

結果

image.text
=> #<Text text: "ABC\nあいうえお\n", locale: "ja", bounds: 4, words: 2, pages: 1>

この角度の回転までならOKのようだ

・文字を回転5

test8.png

結果

image.text
=> #<Text text: "あいうえお\n", locale: "ja", bounds: 4, words: 1, pages: 1>

この角度はだめ

所感

公式gemがあり、導入までのハードルはすごく高いもののちょっとした角度やノイズで認識されないのでどういう画像を読み込ませるかはかなり考慮しないといけなそう。

4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6