9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Cloud VisionをRubyから叩いてみる

Posted at

AWS Rekognition版に引き続き、Cloud Vision版を。
僕のような迷えるGCP初心者のために。

gem導入

bundle init
# frozen_string_literal: true
source "https://rubygems.org"

gem "google-cloud-vision"
bundle install

keyの準備

サービス アカウントからkeyを再作成し、ダウンロードします。既存のkeyはDLできなさそうです。
スクリーンショット 2017-07-18 20.11.16.png

今回は key.json というファイル名にして、rubyスクリプトと同じ場所に起きました。

export VISION_KEYFILE="./key.json"

環境変数でパスを指定してやります。
(注意:今回はAPIを実行することを目的にしているため、雑なことをしています。本番環境で使う場合は置き場所や読み込み方法をちゃんと考えるのがよいでしょう)

リクエスト

labelsをリクエストしてみます。

# Imports the Google Cloud client library
require "google/cloud/vision"

# Your Google Cloud Platform project ID
project_id = "プロジェクトID"

# Instantiates a client
vision = Google::Cloud::Vision.new project: project_id

# The name of the image file to annotate
file_name = "./画像ファイル.jpg"

# Performs label detection on the image file
res = vision.image(file_name).labels
# res = vision.image(file_name).landmark # landmark APIはこんな感じ

puts res.inspect

res.each do |label|
  puts label.inspect
end

ほぼサンプルのまんまです。
簡単ですね。

結果

やったぜ!
1%のマニアックなお客様の需要に応えた看護師のフリー写真素材サイトの素材をお借りしました。
http://nurse-web.jp/photo/

1.jpg

patient 0.9032019376754761
shoulder 0.8479053378105164
profession 0.8370321393013
nursing 0.8111205101013184
professional 0.8093409538269043
hospital 0.7906113266944885
health care 0.7641667127609253
service 0.7637583613395691
healthcare science 0.7570233345031738
joint 0.7511107325553894
nurse 0.6967776417732239
clinic 0.6100589036941528
product 0.5855953693389893
trunk 0.5836150050163269
health & beauty 0.5701348185539246
general practitioner 0.5610331892967224
physician 0.5485168099403381
nursing home 0.5445511341094971
health care provider 0.5364732146263123
registered nurse 0.5301538109779358
therapy 0.5099424123764038
medical 0.509649395942688

2.jpg

shoulder 0.8151744604110718
service 0.7516267895698547
health care 0.7391470670700073
healthcare science 0.6632533669471741
professional 0.6384928226470947
clinic 0.6338927745819092
profession 0.6037552952766418
health & beauty 0.5690610408782959
hair coloring 0.5502387881278992
hospital 0.5419809818267822
beauty salon 0.5337088704109192
patient 0.5122528076171875
research 0.5070715546607971

a.jpg

furniture 0.8865644931793213
desk 0.8032957315444946
technology 0.7598952651023865
table 0.7279538512229919
electronics 0.7005680799484253
product design 0.6304239630699158
9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?