1
0

More than 1 year has passed since last update.

Ruby + Google Vision API で画像の色合い(RGB)を得る

Posted at

コード

require "google/cloud/vision"

IMAGE_ANNOTATOR = Google::Cloud::Vision.image_annotator

image_path = "https://cloud.google.com/static/vision/docs/images/bali_small.jpeg?hl=ja"

response = IMAGE_ANNOTATOR.image_properties_detection(
  image: image_path,
  max_results: 100
)

response.responses.each do |res|
  res.image_properties_annotation.dominant_colors.colors.each do |color|
    red = color.color.red.round
    blue = color.color.blue.round
    green = color.color.green.round

    puts "Red: #{red} Blue: #{blue} Green: #{green}"
  end
end

結果

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

Red: 241 Blue: 132 Green: 178
Red: 114 Blue: 64 Green: 76
Red: 206 Blue: 212 Green: 205
Red: 228 Blue: 65 Green: 55
Red: 146 Blue: 93 Green: 106
Red: 232 Blue: 159 Green: 185
Red: 222 Blue: 225 Green: 221
Red: 187 Blue: 129 Green: 145
Red: 131 Blue: 91 Green: 115
Red: 109 Blue: 77 Green: 79

チャットメンバー募集

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

Twitter

1
0
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
1
0