Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
23
Help us understand the problem. What is going on with this article?
@takaheraw@github

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

More than 5 years have passed since last update.

画像から文字列を取得する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
              }
            ]
          }
        }
      ]
    }
  ]
}
23
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
readyfor
想いをつなぎ、叶える未来を、つくる READYFORのOrganizationです

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
23
Help us understand the problem. What is going on with this article?