【问题标题】:Can google vision API accept external image URL?谷歌视觉 API 可以接受外部图像 URL 吗?
【发布时间】:2016-06-18 15:53:41
【问题描述】:

我正在阅读有关视觉 API 请求架构的文档。在图像源中,我只看到使用 GCS 图像路径的 url 的选项。是否可以使用 http://example.com/images/image01.jpg 之类的外部图片网址?

【问题讨论】:

标签: google-cloud-vision


【解决方案1】:

是的,一切正常:

{
  "requests": [
    {
      "features": [
        {
          "type": "WEB_DETECTION"
        }
      ],
      "image": {
        "source": {
          "imageUri": "http://i3.kym-cdn.com/photos/images/facebook/000/242/592/1c8.jpg"
        }
      }
    }
  ]
}

【讨论】:

  • 如果可行,它真的不应该(否则人们可以使用 Vision API 到 DOS 图像主机)! imageUri 字段应始终以 gs:// 开头(指向 Google Cloud Storage)。
  • 我在上个月试用 API 时运行良好
  • @JJGeewax Google 在文档页面上有一个警告:警告:从 HTTP/HTTPS URL 获取图像时,Google 不能保证请求会完成。如果指定的主机拒绝请求(例如,由于请求限制或 DOS 防护),或者如果 Google 限制对站点的请求以防止滥用,您的请求可能会失败。对于生产应用程序,您不应依赖外部托管的映像。 cloud.google.com/vision/docs/ocr#vision_text_detection-cli
  • 简而言之,image.source.imageUri 有效,但受上述条件限制
  • 感谢大家的反馈。自从我没有使用 API 以来已经有一段时间了,因此如果您想更新/改进,请随时编辑我的答案。
【解决方案2】:

可以,但只能使用谷歌云存储网址。试试这个:

{
  "requests": [
    {
      "image": {
        "source": {
          gcsImageUri: 'gs://something.com/image',
        },
      },
      "features": ...
      "imageContext": ...
    },
  ]
}

【讨论】:

  • 谢谢。这是我试图解决的问题,这样我就不必每次都先上传到 GS。我想,这几乎是一样的努力,直接上传到图像服务或先上传到GS然后处理它。只是另一个额外的 API 调用,用于在处理之前上传到 GS,并在处理之后从 GS 中删除。
  • 根据所使用的语言,可能有一些有用的方法可以通过 http 获取图像并获取图像的 base64 表示,您可以将其传递给 google api。
【解决方案3】:

是的,您可以对任何小于 4Mb 的图像执行此操作。它不必必须在 Google Cloud Storage 上。

这是一个使用 Golang 客户端库的示例:

// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

// [START vision_quickstart]
// Sample vision-quickstart uses the Google Cloud Vision API to label an image.
package main

import (
    "fmt"
    "log"

    // Imports the Google Cloud Vision API client package.
    vision "cloud.google.com/go/vision/apiv1"
    "golang.org/x/net/context"
)

func main() {
    ctx := context.Background()

    // Creates a client.
    client, err := vision.NewImageAnnotatorClient(ctx)
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }

    image := vision.NewImageFromURI("https://www.denaligrizzlybear.com/assets/images/scenic-denali.jpg")

    labels, err := client.DetectLabels(ctx, image, nil, 10)
    if err != nil {
        log.Fatalf("Failed to detect labels: %v", err)
    }

    fmt.Println("Labels:")
    for _, label := range labels {
        fmt.Println(label.Description)
    }
}

这里是Godoc上的函数:https://godoc.org/cloud.google.com/go/vision/apiv1#NewImageFromURI

文档状态:

NewImageFromURI 返回一个引用 Google 中的对象的图像 Cloud Storage(当 uri 的格式为“gs://BUCKET/OBJECT”时)或在 公共网址。

【讨论】:

    【解决方案4】:

    python 用户的答案。

    def detect_labels_uri(uri):
        """Detects labels in the file located in Google Cloud Storage or on the
        Web."""
        from google.cloud import vision
        client = vision.ImageAnnotatorClient()
        image = vision.types.Image()
        image.source.image_uri = uri
    
        response = client.label_detection(image=image)
        labels = response.label_annotations
        print('Labels:')
    
        for label in labels:
            print(label.description)
    # [END vision_label_detection_gcs]
    

    【讨论】:

    • 这不是一个答案,这是一个稍微相关的代码 sn-p 没有任何合理的解释。请回答OP的问题。
    【解决方案5】:

    是的,Google Cloud Vision API确实接受外部网址图片。我只是用this image 来获取标签:

    python label_sample.py -u "https://upload.wikimedia.org/wikipedia/commons/e/e6/Bleeding_finger.jpg"
    
    Found label: red with 96.47 percent probability!!!!!!!!!!!
    Found label: color with 95.46 percent probability!!!!!!!!!!!
    Found label: pink with 92.15 percent probability!!!!!!!!!!!
    Found label: finger with 91.06 percent probability!!!!!!!!!!!
    Found label: hand with 90.45 percent probability!!!!!!!!!!!
    Found label: nail with 73.23 percent probability!!!!!!!!!!!
    Found label: lip with 73.09 percent probability!!!!!!!!!!!
    Found label: jewellery with 68.84 percent probability!!!!!!!!!!!
    Found label: produce with 68.39 percent probability!!!!!!!!!!!
    Found label: macro photography with 67.86 percent probability!!!!!!!!!!!
    

    您必须使用 urllib 库以正确格式为其提供 url 并注释打开图像的部分,如下所示:

    url = url_file
    opener = urllib.urlopen(url)
    
    #    with open(photo_file, 'rb') as image:
    image_content = base64.b64encode(opener.read())
    

    【讨论】:

    • 我不明白对我的回答投反对票的原因。
    • 问题是“谷歌视觉 API 可以接受外部图像 URL 吗?”。换句话说:Google 的 API 能否接受来自任何外部服务器(例如 imgur、flickr、s3)的 URL 并在该图像上返回响应。如果问题是“我如何在互联网上下载任何图像资源并使用 Google Vision 进行处理,那么您的答案将是有效的。
    • 您误解了我的答案以及我对问题的解决方案。我的解决方案完全符合问题的要求,即接受来自任何外部服务器的 URL 并在该图像上返回响应。
    • @muglikar 您的示例在本地下载文件,然后将其内联发送到 Vision API。问题是您是否可以仅将 URL(而不是图像本身)发送到 Vision API 进行处理(可以,但仅适用于以 gs:// 开头的 URL)。
    猜你喜欢
    • 2020-09-09
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多