1
0
mirror of https://github.com/danbee/my-images synced 2025-03-04 08:49:05 +00:00
my-images/lib/clarifai.rb
2018-08-25 20:29:07 -04:00

45 lines
890 B
Ruby

require "base64"
require "http"
class Clarifai
KEY = ENV.fetch("CLARIFAI_API_KEY").freeze
API_URL = "https://api.clarifai.com/v2/models/" \
"aaa03c23b3724a16a56b629203edc62c/outputs".freeze
attr_reader :tags
def initialize(image_path)
@image_path = image_path
end
def predict!
headers = {
"Authorization": "Key #{KEY}",
"Content-Type": "application/json",
}
params = {
"inputs": [
{
"data": {
"image": {
"base64": Base64.encode64(File.read(@image_path)),
},
},
},
],
}
resp = HTTP.
headers(headers).
post(API_URL, json: params)
extract_tags(JSON.parse(resp.body))
end
def extract_tags(response_hash)
@tags = response_hash["outputs"][0]["data"]["concepts"].map do |concept|
concept["name"]
end
end
end