Last active
April 16, 2016 10:15
-
-
Save nanonanomachine/fa3e6092186b87c6b46e1b48863deb06 to your computer and use it in GitHub Desktop.
Cloud Vision API Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'base64' | |
require 'json' | |
require 'net/https' | |
DIRECTORIES = ['DIR PATH'] | |
API_KEY = 'PASTE YOUR API KEY' | |
API_URL = "https://vision.googleapis.com/v1/images:annotate?key=#{API_KEY}" | |
def detect(image_path) | |
# 画像をbase64にエンコード | |
base64_image = Base64.strict_encode64(File.new(image_path, 'rb').read) | |
# APIリクエスト用のJSONパラメータの組み立て | |
body = { | |
requests: [{ | |
image: { | |
content: base64_image | |
}, | |
features: [ | |
{ | |
type: 'TEXT_DETECTION', | |
maxResults: 5 | |
} | |
] | |
}] | |
}.to_json | |
# Google Cloud Vision APIにリクエスト投げる | |
uri = URI.parse(API_URL) | |
https = Net::HTTP.new(uri.host, uri.port) | |
https.use_ssl = true | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request["Content-Type"] = "application/json" | |
response = https.request(request, body) | |
# APIレスポンス出力 | |
File.write( | |
File.join(File.dirname(image_path), | |
File.basename(image_path, '.*') + ".json",), | |
response.body) | |
end | |
DIRECTORIES.each do |directory| | |
Dir::glob(directory + '/*').each do |f| | |
detect f | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment