google vision API可以接受外部图片网址吗?

时间:2016-03-04 19:04:06

标签: google-cloud-vision

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

5 个答案:

答案 0 :(得分:9)

是的,这完全没问题:

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

答案 1 :(得分:4)

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

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

答案 2 :(得分:1)

是的,只要小于4Mb,您就可以对任何图像执行此操作。 必须在Google云端存储上。

以下是使用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中对象的图片   云存储(当uri的格式为#34; gs:// BUCKET / OBJECT")或者   公共网址。

答案 3 :(得分:1)

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]

答案 4 :(得分:-4)

是的,Google Cloud Vision API DOES 接受外部网址图片。我刚用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_file
opener = urllib.urlopen(url)

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