Google Cloud Vision API で画像から文字列を取得してみる

画像から文字列を取得するAPIは他にもいろいろやったことあるが、どれもイマイチだった記憶。Google先生ということで期待して[Google Cloud Vision API](https://cloud.google.com/vision/)試してみた。

# API KEY を Google Developers Consoleで作成する

## Google Developers Console

+ [Google Developers Console](https://console.cloud.google.com/)

### プロジェクト作成する

![1.png](https://qiita-image-store.s3.amazonaws.com/0/7086/f163dfa3-5726-6cea-3be9-7ebf1cb09b53.png)

### お支払い方法を設定する

これ設定しないとAPI叩けない

![3.png](https://qiita-image-store.s3.amazonaws.com/0/7086/d78a7093-a834-cccc-e036-d4bb8d878366.png)


### Cloud Vision API を有効

![2.png](https://qiita-image-store.s3.amazonaws.com/0/7086/025a3d48-170b-5f49-d33f-8370fdc0f96a.png)


### API KEY を取得

「認証情報」⇒「Create Credentials」⇒「APIキー」⇒「ブラウザキー」でAPI KEY を作成する

![4.png](https://qiita-image-store.s3.amazonaws.com/0/7086/287ee395-df48-4333-a0cb-1333595724e1.png)


# 検証

## 検証する画像

![basket](https://i.gyazo.com/7073f1ea36715476ace6928a8ecf7389.png)

## サンプルプログラム

+ [ドキュメント](https://cloud.google.com/vision/docs/concepts)を参考にパラメータ設定

```rb
require 'base64'
require 'json'
require 'net/http'
require 'uri'
require 'open-uri'

VISION_API_URL = "https://vision.googleapis.com/v1/images:annotate"
API_KEY        = "Google Developers Consoleで作成したキー"
URL            = "#{VISION_API_URL}?key=#{API_KEY}"
INPUT_IMG_FILE = 'http://blog-imgs-42-origin.fc2.com/k/o/t/kotomona/20101125183059615.jpg'

begin
  uri           = URI.parse(URL)
  https         = Net::HTTP.new(uri.host, uri.port)
  https.use_ssl = true

  req                 = Net::HTTP::Post.new(uri.request_uri)
  req["Content-Type"] = "application/json"
  param               = {
    "requests" =>
    [
      {
        "image" =>
        {
          "content" => Base64.strict_encode64(open(INPUT_IMG_FILE).read)
        },
        "features" =>
        [
          {
            "type"       => "LABEL_DETECTION",
            "maxResults" => 10
          },
          {
            "type"       => "TEXT_DETECTION",
            "maxResults" => 10
          }
        ]
      }
    ]
  }
  req.body = param.to_json
  res      = https.request(req)

  case res
  when Net::HTTPSuccess
    puts res.body
  else
    res.error!
  end
rescue => e
  puts "error = #{e.message}"
end
```

## レスポンス

```
{
  "responses": [
    {
      "labelAnnotations": [
        {
          "mid": "/m/0215n",
          "description": "cartoon",
          "score": 0.93569827
        },
        {
          "mid": "/m/05qdh",
          "description": "painting",
          "score": 0.75862324
        },
        {
          "mid": "/m/0dgsmq8",
          "description": "artwork",
          "score": 0.71173751
        },
        {
          "mid": "/m/012h24",
          "description": "comics",
          "score": 0.63047028
        },
        {
          "mid": "/m/01kr8f",
          "description": "illustration",
          "score": 0.62665105
        }
      ],
      "textAnnotations": [
        {
          "locale": "ja",
          "description": "た\nC&as\nパスケが\nしたいです……\n",
          "boundingPoly": {
            "vertices": [
              {
                "x": 236,
                "y": 143
              },
              {
                "x": 395,
                "y": 143
              },
              {
                "x": 395,
                "y": 502
              },
              {
                "x": 236,
                "y": 502
              }
            ]
          }
        }
      ]
    }
  ]
}
```