Created
June 16, 2016 04:09
-
-
Save ckuwanoe/23b3fcd8e2c4512f98d530d4d19de13f to your computer and use it in GitHub Desktop.
OCR an image using the google vision API
This file contains hidden or 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 'net/http' | |
require 'json' | |
# Base 64 the input image | |
b64_data = Base64.encode64(File.open(ARGV[0], "rb").read) | |
# Stuff we need | |
api_key = "<YOUR_GOOGLE_API_KEY>" | |
content_type = "Content-Type: application/json" | |
url = "https://vision.googleapis.com/v1/images:annotate?key=#{api_key}" | |
data = { | |
"requests": [ | |
{ | |
"image": { | |
"content": b64_data | |
}, | |
"features": [ | |
{ | |
"type": "TEXT_DETECTION", | |
"maxResults": 1 | |
} | |
] | |
} | |
] | |
}.to_json | |
# Make the request | |
url = URI(url) | |
req = Net::HTTP::Post.new(url, initheader = {'Content-Type' =>'application/json'}) | |
req.body = data | |
res = Net::HTTP.new(url.host, url.port) | |
res.use_ssl = true | |
# res.set_debug_output $stderr | |
detected_text = "" | |
res.start do |http| | |
puts "Querying Google for image: #{ARGV[0]}" | |
resp = http.request(req) | |
# puts resp | |
json = JSON.parse(resp.body) | |
# puts json | |
if json && json["responses"] && json["responses"][0]["textAnnotations"] && json["responses"][0]["textAnnotations"][0]["description"] | |
detected_text = json["responses"][0]["textAnnotations"][0]["description"] | |
end | |
end | |
puts "Google says the image is: #{detected_text}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment