Ruby on rails 如何在Google Vision API-Ruby的url上传图像 如何为Google Vision正确上传S3 URL图像?

Ruby on rails 如何在Google Vision API-Ruby的url上传图像 如何为Google Vision正确上传S3 URL图像?,ruby-on-rails,ruby,google-app-engine,ruby-on-rails-4,google-cloud-vision,Ruby On Rails,Ruby,Google App Engine,Ruby On Rails 4,Google Cloud Vision,我正在尝试将图像(保存在AWS S3 URL上)发送到Google Vision,按照下面列出的第2个选项使用Base64编码: 发送到Google Cloud Vision API的图像可以通过两种方式提供: 使用谷歌云存储URI,格式为gs://bucketname/path/to/image\u文件名 作为JSON请求中发送的图像数据。因为图像数据必须作为ASCII文本提供,所以所有图像数据都应该使用base64编码进行转义 我在用电脑 我尝试过,只是做了一点小小的修改: requir

我正在尝试将图像(保存在AWS S3 URL上)发送到Google Vision,按照下面列出的第2个选项使用Base64编码:

发送到Google Cloud Vision API的图像可以通过两种方式提供:

  • 使用谷歌云存储URI,格式为gs://bucketname/path/to/image\u文件名

  • 作为JSON请求中发送的图像数据。因为图像数据必须作为ASCII文本提供,所以所有图像数据都应该使用base64编码进行转义

  • 我在用电脑

    我尝试过,只是做了一点小小的修改:

    require 'google/cloud/vision'
    require 'base64'
    require 'googleauth'
    require 'open-uri'
    
    encoded_image = Base64.strict_encode64(open(image_url, &:read))
    
    @vision = Google::Cloud::Vision.new
    image = @vision.image(encoded_image)
    annotation = @vision.annotate(image, labels: true, text: true)
    
    我尝试了AWS URL上的图像和其他URL上的图像

    每次我从Google Cloud Vision gem收到此错误时:
    ArgumentError:无法将(my_base_64_encoded_图像)转换为图像

    更新-仅在ruby中成功编码和解码图像 我已确认此代码:
    encoded\u image=Base64.strict\u encode64(open(image\u url,&:read))
    通过以下方式工作:

    # Using a random image from the interwebs
    image_url = "https://storage.googleapis.com/gweb-uniblog-publish-prod/static/blog/images/google-200x200.7714256da16f.png"
    encoded_image = Base64.strict_encode64(open(image_url, &:read))
    
    ### now try to decode the encoded_image
    File.open("my-new-image.jpg", "wb") do |file|
      file.write(Base64.strict_decode64(encoded_image))
    end
    ### great success
    

    那么谷歌在这方面有什么问题?我的编码是正确的。

    如果你打算使用Google Cloud Vision gem,你需要遵循gem文档(使用非编码图像),可能是他在幕后做的

    基于gem的文档,您可以像下面的代码一样转换图像

    open image_url do |img|
      @vision = Google::Cloud::Vision.new
    
      image = @vision.image(img)
      # you can also use the class method from_io
      # image = Google::Cloud::Vision::Image.from_io(img, @vision)
    
      annotation = @vision.annotate(image, labels: true, text: true)
    end
    

    啊,所以根本不要转换成Base64。只需使用一个tmpfile。。。美好的