21
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2016-02-20

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

API KEY を Google Developers Consoleで作成する

Google Developers Console

プロジェクト作成する

1.png

お支払い方法を設定する

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

3.png

Cloud Vision API を有効

2.png

API KEY を取得

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

4.png

検証

検証する画像

basket

サンプルプログラム

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
              }
            ]
          }
        }
      ]
    }
  ]
}
21
24
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
21
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?