您好,我正在用 Ruby 构建一个程序来为网页上的图像生成 alt 属性。我正在抓取图像的页面,然后将它们的 src,换句话说,一个 URL,发送到 google-cloud-vision 以进行标签检测和其他 Cloud Vision 方法。每张图像大约需要 2-6 秒。我想知道是否有任何方法可以减少响应时间。我首先使用 TinyPNG 来压缩图像。 Cloud Vision 稍快一些,但压缩所花费的时间超过了改进。我怎样才能提高响应时间?我将列出一些想法。
1) 由于我们向 Google Cloud 发送了一个 URL,因此 Google Cloud 在分析图像之前需要时间来接收来自 img_src 的响应。发送 base64 编码的图像是否更快?发送(或者实际上,Google 接收)图像的最快方式是什么?
cloud_vision = Google::Cloud::Vision.new project: PROJECT_ID
@vision = cloud_vision.image(@file_name)
@vision.labels #or @vision.web, etc.
2) 我当前的标签检测代码。第一个问题:发送 JSON 请求比在 Google Cloud 项目上调用 Ruby(标签或网络)方法更快吗?如果是这样,我应该限制回复吗?置信度分数低于 0.6 的标签似乎没有多大帮助。这会加快图像录制/处理时间吗?
欢迎任何有关如何加快 Cloud Vision 响应时间的建议。
最佳答案
TL;DR - 您可以利用 Cloud Vision 注释 API 中的批处理支持。
更长的版本
Google Cloud Vision API 支持在对 images:annotate
API 的一次调用中批处理多个请求.还有these limits which are enforced for Cloud Vision :
- 每个请求最多 16 张图片
- 每张图片最多 4 MB
- 最大总请求大小为 8 MB。
您可以通过一次批处理 16 个来减少请求的数量(假设您没有超过请求中的任何图像大小限制):
#!/usr/bin/env ruby
require "google/cloud/vision"
image_paths = [
...
"./wakeupcat.jpg",
"./cat_meme_1.jpg",
"./cat_meme_2.jpg",
...
]
vision = Google::Cloud::Vision.new
length = image_paths.length
start = 0
request_count = 0
while start < length do
last = [start + 15, length - 1].min
current_image_paths = image_paths[start..last]
printf "Sending %d images in the request. start: %d last: %d\n", current_image_paths.length, start, last
result = vision.annotate *current_image_paths, labels: 1
printf "Result: %s\n", result
start += 16
request_count += 1
end
printf "Made %d requests\n", request_count
关于ruby - 如何将 Base64 图像发送到 Ruby 中的 Google Cloud Vision API 标签检测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45048382/