1

I have a Gallery and Attachment models. A gallery has_many attachments and essentially all attachments are images referenced in the ':content' attribute of Attachment.

The images are uploaded using Carrierwave gem and are stored in Aws S3 via fog-aws gem. This works OK. However, I'd like to conduct image recognition to the uploaded images with Amazon Rekognition.

I've installed aws-sdk gem and I'm able to instantiate Rekognition without a problem until I call the detect_labels method at which point I have been unable to use my attached images as arguments of this method.

So fat I've tried:

@attachement = Attachment.first
client = Aws::Rekognition::Client.new
resp = client.detect_labels(
         image: @attachment
       )
# I GET expected params[:image] to be a hash... and got class 'Attachment' instead

I've tried using:

client.detect_labels( image: { @attachment })
client.detect_labels( image: { @attachment.content.url })
client.detect_labels( image: { @attachment.content })

All with the same error. I wonder how can I fetch the s3 object form @attachment and, even if I could do that, how could I use it as an argument in detect_labels.

I've tried also fetching directly the s3 object to try this last bit:

s3 = AWS:S3:Client.new
s3_object = s3.list_objects(bucket: 'my-bucket-name').contents[0]

# and then

client.detect_labels( image: { s3_object })

Still no success...

Any tips?

alopez02
  • 1,524
  • 2
  • 17
  • 36

1 Answers1

1

I finally figured out what was the problem, helped by the following AWS forum

The 'Image' hash key takes as a value an object that must be named 's3_object' and which subsequently needs only the S3 bucket name and the path of the file to be processed. As a reference see the correct example below:

client = Aws::Rekognition::Client.new
resp = client.detect_labels(
         image:
            { s3_object: {
              bucket: "my-bucket-name",
              name: @attachment.content.path, 
            },
          }
       )

# @attachment.content.path => "uploads/my_picture.jpg"
alopez02
  • 1,524
  • 2
  • 17
  • 36
  • 1
    Thank you so much for your question and commentary. If I would've encountered your SO post earlier, it would've saved me two full days! Additionally, very important - was further problem in my case - your `S3` bucket *and* your `Rekognition` instance should be in the same region. See: ```Q: Can I use Amazon Rekognition with images stored in an Amazon S3 bucket in another region? No. Please ensure that the Amazon S3 bucket you want to use is in the same region as your Amazon Rekognition API endpoint.``` – mohnstrudel Apr 10 '19 at 16:31
  • 1
    @mohnstrudel glad it helped! – alopez02 Apr 11 '19 at 10:44