ようやく、アーリープレビューのアクセスがきたので、Railsから使ってみたいと思います。
リクエストとレスポンス
まず、APIのリクストとレスポンスは、例えば画像に写っているものを認識するためのリクエストはこんな感じのを送ります。
{
"requests": [
{
"image": {
"content": "jisYIujijJIUJI....";
},
"features": [
{
"type": "LABEL_DETECTION",
"maxResults": 10
}
]
}
]
}
content
にはBase64でエンコードした画像がはいります。
レスポンスは画像を解析した結果がはいります。
{
"responses": [
{
"labelAnnotations": [
{
"mid": "/m/07dm6",
"description": "tiger",
"score": 0.9995195
},
{
"mid": "/m/01280g",
"description": "wildlife",
"score": 0.96001059
},
{
"mid": "/m/09686",
"description": "vertebrate",
"score": 0.85704827
},
{
"mid": "/m/04rky",
"description": "mammal",
"score": 0.85094059
},
{
"mid": "/m/089v3",
"description": "zoo",
"score": 0.788213
},
{
"mid": "/m/0cdnk",
"description": "panthera",
"score": 0.73796427
},
{
"mid": "/m/0307l",
"description": "felidae",
"score": 0.57086021
},
{
"mid": "/m/01lrl",
"description": "carnivora",
"score": 0.56989247
},
{
"mid": "/m/0891tx",
"description": "toyger",
"score": 0.52950406
}
]
}
]
}
適当にAPIを呼ぶクラスを作成して、コンストラクタをこんな感じにします。
def initialize(file_path)
@endpoint_uri = "https://vision.googleapis.com/v1alpha1/images:annotate?key=#{ENV['SERVER_KEY']}"
@file_path = file_path
end
``
`http-client`を使ってAPIをたたきます。
```ruby
def request
http_client = HTTPClient.new
content = Base64.strict_encode64(File.new(file_path, 'rb').read)
response = http_client.post_content(endpoint_uri, request_json(content), 'Content-Type' => 'application/json')
result_parse(response)
end
たたくときのリクエストはJSONに変換して、
def request_json(content)
{
requests: [{
image: {
content: content
},
features: [{
type: "LABEL_DETECTION",
maxResults: 10
}]
}]
}.to_json
end
受信したレスポンスをパースして、一番scoreが高いのだけとりだします。
def result_parse(response)
result = JSON.parse(response)['responses'].first
label = result['labelAnnotations'].first
"これは、#{label['description']}です。"
end
あと、CarrierWaveで画像などをあげれるようにします。そこら辺は以下のソースを参考にしてください。
実行
適当に写真をアップロードします。
ちゃんと虎と認識してくれますね。
まとめ
あと、物体以外にもテキスト、顔、ロゴ、ランドマークなどを認識できるようです。
いままででも技術的にこういったことはできるとは思うのですが、Google Cloudにユーザ登録して数時間でここまでできてしまうのはすごいなと思いました。