如何在 url 为 Google Vision API - Ruby 上传图像
How to upload an image at url for Google Vision API - Ruby
如何为 Google Vision 正确上传 S3 URL 图像?
我正在尝试将图像(保存在 AWS S3 URL)发送到 Google Vision,根据下面列出的 documentation 中的第二个选项使用 Base64 编码:
Images sent to the Google Cloud Vision API can be supplied in two ways:
Using Google Cloud Storage URIs of the form gs://bucketname/path/to/image_filename
As image data sent within the JSON request. Because image data must be supplied as ASCII text, all image data should be escaped using base64 encoding.
我正在使用 Google-Cloud-Vision Gem。
我试过this previous answer about Base64 encoding,稍作修改:
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 URLs 上的图像和其他 url 上的图像。
每次我从 Google-Cloud-Vision gem 收到此错误:
ArgumentError: Unable to convert (my_base_64_encoded_image) to an image
更新 - 仅在 ruby 中成功编码和解码图像
我已确认此代码:encoded_image = Base64.strict_encode64(open(image_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有什么问题呢?我编码正确了。
如果你打算使用 Google-Cloud-Vision gem,你需要遵循 gem 文档(使用非编码图像),也许他是在引擎盖..
根据 gem google-cloud-vision 的文档,您可以像下面的代码那样转换图像
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
如何为 Google Vision 正确上传 S3 URL 图像?
我正在尝试将图像(保存在 AWS S3 URL)发送到 Google Vision,根据下面列出的 documentation 中的第二个选项使用 Base64 编码:
Images sent to the Google Cloud Vision API can be supplied in two ways:
Using Google Cloud Storage URIs of the form gs://bucketname/path/to/image_filename
As image data sent within the JSON request. Because image data must be supplied as ASCII text, all image data should be escaped using base64 encoding.
我正在使用 Google-Cloud-Vision Gem。
我试过this previous answer about Base64 encoding,稍作修改:
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 URLs 上的图像和其他 url 上的图像。
每次我从 Google-Cloud-Vision gem 收到此错误:
ArgumentError: Unable to convert (my_base_64_encoded_image) to an image
更新 - 仅在 ruby 中成功编码和解码图像
我已确认此代码:encoded_image = Base64.strict_encode64(open(image_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有什么问题呢?我编码正确了。
如果你打算使用 Google-Cloud-Vision gem,你需要遵循 gem 文档(使用非编码图像),也许他是在引擎盖..
根据 gem google-cloud-vision 的文档,您可以像下面的代码那样转换图像
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