🖥

Ruby + GCP Vision API で画像の種類(ラベル)を取得する

に公開

準備

gem install google/cloud/vision

コード


require "google/cloud/vision"

image_path = 'gs://cloud-samples-data/vision/label/setagaya.jpeg'

image_annotator = Google::Cloud::Vision.image_annotator

response = image_annotator.label_detection(
  image:       image_path,
  max_results: 10 # optional, defaults to 10
)

response.responses.each do |res|
  res.label_annotations.each do |label|
    puts label.description
  end
end

画像

<img width="654" alt="image" src="https://user-images.githubusercontent.com/13635059/196164817-ebc322a4-f0d5-4795-bcc1-960d2cdd107d.png">

実行

GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json" ruby /path/to/script.rb

結果

Bicycle
Tire
Wheel
Automotive lighting
Infrastructure
Bicycle wheel
Building
Mode of transport
Electricity
Vehicle

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2022-10-17

Discussion