LoginSignup
20
11

More than 1 year has passed since last update.

Amazon RekognitionをRubyから叩いてみる

Last updated at Posted at 2016-12-04

僕のような迷えるAWS初心者のために。
圧倒的に成長したのでちゃんと導入から書いて編集しました。

aws-sdk導入

bundle init
# frozen_string_literal: true
source "https://rubygems.org"
gem "aws-sdk"
bundle install

リクエスト

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

require "aws-sdk"
Aws.config[:region] = "ap-northeast-1"
Aws.config[:access_key_id] = "****************"
Aws.config[:secret_access_key] = "***************"

rekog = Aws::Rekognition::Client.new(region: "us-east-1") # AP対応まだです?
# https://s3.amazonaws.com/hoge/dir/1.jpg
result = rekog.detect_labels({
 image: {
   s3_object: {
     bucket: "hoge",
     name: "dir/1.jpg"
   },
 }
})
result.labels.each do |label|
 puts("#{label.name} #{label.confidence}")
end

max_labelsmin_confidenceのオプションが指定できます。
S3ではなくてバイナリでリクエストすることもできそうです。

ドキュメントはこの辺にあります。

結果

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

1.jpg

Human 99.27359771728516
People 99.27857208251953
Person 99.27857971191406
Nurse 50.79678726196289
Female 50.725425720214844

やったぜ!

2.jpg

People 99.2396011352539
Person 99.2396011352539
Human 99.23065185546875
Head 55.16327667236328
Portrait 55.16327667236328
Selfie 54.765235900878906
Face 54.30482482910156
Female 53.51632308959961
Hair 51.61334228515625
Haircut 51.61334228515625

a.jpg

Desk 98.12632751464844                                                                                         
Furniture 98.12632751464844
Table 98.12632751464844
Collage 83.7958984375
Poster 83.7958984375
Electronics 73.93887329101562
Hardware 73.93887329101562
Flyer 58.507293701171875
Brochure 54.80351257324219
Paper 54.80351257324219
Computer 54.42918395996094
LCD Screen 54.42918395996094
Laptop 54.42918395996094
Pc 54.42918395996094
Apparel 51.038368225097656
Clothing 51.038368225097656
Photo Booth 50.5786018371582
Console 50.56346130371094

注意事項

S3も同一リージョンのものを使うのが望ましいようです。今後エラーになる可能性がありそうです。

20
11
2

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
20
11