Rails Active Storage + AWS Rekognition: как получить файл IO - PullRequest
0 голосов
/ 11 марта 2019

Я пытаюсь интегрировать AWS Rekognition в мое приложение Rails.После того, как пользователь загрузит свой аватар через Active Storage, Rekognition должен показать некоторую информацию об этом.

def update
  respond_to do |format|
    if @user.update(user_params)
      if @user.images.attached?
        Aws.config.update({
            region: 'us-west-2',
            credentials: Aws::Credentials.new('ACCESS_KEY', 'SECRET_KEY')
          })
          rekognition = Aws::Rekognition::Client.new(region: Aws.config[:region], credentials: Aws.config[:credentials])
          img = @user.images.first. # original was: File.read(ARGV.first)

          #detect faces  
          resp = rekognition.detect_faces({
            image: { bytes: img },
            attributes: ["ALL"], # What attributes to return
          })
          resp.face_details[0].emotions.each do |emo|
            puts emo.type + " " + emo.confidence.to_i.to_s #=> Strings "HAPPY", "SAD", "ANGRY"
          end
      end
   end
end

Однако я получаю ошибку

expected params[:image][:bytes] to be a String or IO object, got value #<ActiveStorage::Attachment id: 4, name: "images", record_type: "User", record_id: 47, blob_id: 9, created_at: ""> (class: ActiveStorage::Attachment) instead.

Как можноЯ получаю свойство файла изображения в AWS Rekognition?

1 Ответ

1 голос
/ 11 марта 2019

Существует два способа передачи изображения в Aws::Rekognition.

  1. URL-адрес изображения AWS S3 и имя
resp = rekognition.detect_faces(
        {image:
          {s3_object:
            {bucket: `bucket name`,
              name: `pull path of file`,
            },
          }, attributes: ['ALL'],
        }
      )
Через объект Image
rekognition.detect_faces({
         image: { bytes: File.read(`path of file`) }
       })

в вашем случае вы передаете ActiveStorage объект, который не может быть проанализирован AWS.вот почему выдает ошибку.

...