使用 Google 视觉 API 预测用户指定标签的分数

Using Google Vision API to Predict Score of User-Specified Labels

假设我有一张 rainbow 的图像,并想使用 Google Vision API 来预测一组用户指定标签的 score,例如:

0    Rainbow:   0.965621
1    Sky        0.887454
2    Artwork    0.813930
3    Giraffe    0.015654
4    Coffee     0.012483

以下Google Vision API code

def detect_labels(path):
    """Detects labels in the file."""
    from google.cloud import vision
    import io
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.Image(content=content)

    response = client.label_detection(image=image)
    labels = response.label_annotations
    print('Labels:')

    for label in labels:
        print(label.description, label.score, label.mid)

Returns 以下标签和分值:

  description       score          mid
0     Rainbow    0.965621    /m/0b48hv
1  Vertebrate    0.924276     /m/09686
2       White    0.921867     /m/083jv
3     Cartoon    0.918200     /m/0215n
4     Product    0.908071    /m/01jwgf
5       Green    0.907698     /m/038hg
6    Organism    0.875143     /m/05nnm
7     Textile    0.873498     /m/0dnr7
8   Rectangle    0.853343     /m/0j62f
9        Font    0.841818   /m/03gq5hm

因为只有 'top 10' 标签被 return 编辑,我没有 score 标签,例如 'Coffee''Giraffe'

  1. 是否可以return超过10个标签?或者这是 Google 视力的限制 API?

  2. 我可以使用 Google 视觉 API 来预测用户指定标签的可能性,而不是 return 前 10 个标签吗?比如预测'Coffee'的似然为0.012483?

  3. 是否可以访问 所有 标签 descriptionsmid 值?根据EntityAnnotation, mid is an 'opaque' entity ID, however it states that some values are apparently available in Google Knowledge Graph Search API。 'opaque' 在这种情况下是否意味着 Google 不共享他们的完整标签列表?

我知道我可以 train my own AutoML Vision model 并定义我自己的标签,但是这样做似乎很愚蠢,因为我很乐意使用 Google 现有的标签分类。我只是想要更多地访问标签数据。有没有一种方法可以简单地为给定图像请求所选 midscore

注意:如果无法通过 Google API.

访问我需要的数据,我很乐意探索替代方案 API

回答您的问题:

  1. 是的,可以 return 超过 10 个标签。只需调整请求中的 max_results 即可。
  2. 是的,您可以在 API 的响应中交叉检查输入的用户特定标签。
  3. 不,Google 使用的标签在一个数量不断增长的存储库中,可能有数百万。

我在使用 Vision API 时采用了不同的方法,其中我使用 batch_annotate_images() and used a request to define the type of detection that will be used. With this approach I can easily control the features 来处理图像。

def detect_labels(path):
    """Detects labels in the file."""
    from google.cloud import vision
    import io
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read()

    image = vision.Image(content=content)
    features = [{"type_": vision.Feature.Type.LABEL_DETECTION, "max_results": 11}]
    requests = [{"image": image, "features": features}]

    response = client.batch_annotate_images(requests=requests)

    for image_response in response.responses:
        for label in image_response.label_annotations:
            print(u"description : {}".format(label.description))
            print(u"score : {}".format(label.score))
            print(u"mid : {}\n".format(label.mid))

使用这个 image for testing 并将 max_results 的值更改为 3 和 11。

已将 max_results 更改为 3:

已将 max_results 更改为 11: