Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created February 20, 2016 15:17
Show Gist options
  • Save takaheraw/d6939a27dd276501198d to your computer and use it in GitHub Desktop.
Save takaheraw/d6939a27dd276501198d to your computer and use it in GitHub Desktop.
require 'base64'
require 'json'
require 'net/http'
require 'uri'
require 'open-uri'
VISION_API_URL = "https://vision.googleapis.com/v1/images:annotate"
API_KEY = "foobarbazhogefuga"
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
json = JSON.load(res.body)
else
res.error!
end
rescue => e
puts "error = #{e.message}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment