【问题标题】:Google cloud vision api detects different number of labels谷歌云视觉 api 检测到不同数量的标签
【发布时间】:2017-07-22 20:40:13
【问题描述】:

按照this page,以下代码sn-p返回5个标签:

from google.cloud import vision
url = 'https://farm9.staticflickr.com/8215/8267748261_ea142faf5e.jpg'

client = vision.ImageAnnotatorClient()
client.label_detection({'source': {'image_uri': url}}) # yields 5

当我按照here 的描述进行操作时,我得到 10 个标签:

client = vision.Client()
image = client.image(source_uri=url)
labels = image.detect_labels() # yields 10

当我使用Cloud Vision demo page 时,我会为同一张图片获得 18 个标签。

为什么这些方法都不同?我在这里错过了什么?

【问题讨论】:

    标签: python google-cloud-vision


    【解决方案1】:

    TL;DR - image.detect_labels 采用可选的 limit 参数,其默认值为 10,因此您在第二个版本中仅获得 10 标签。如果您将限制增加到高于18 的值,您将获得与您在 Cloud Vision 演示页面上观察到的结果相同的结果。

    detect_labels() 的文档

    模块google.cloud.vision.image中方法detect_labels的帮助:

    detect_labels(self, limit=10)方法 google.cloud.vision.image.Image实例

    Detect labels that describe objects in an image.
    
    :type limit: int
    :param limit: The maximum number of labels to try and detect.
    
    :rtype: list
    :returns: List of :class:`~google.cloud.vision.entity.EntityAnnotation`
    

    使用Image.detect_labels() 的工作示例

    试试这个:

    from google.cloud import vision
    
    IMAGE_URL = 'https://farm9.staticflickr.com/8215/8267748261_ea142faf5e.jpg'
    
    vision_client = vision.Client()
    
    image = vision_client.image(source_uri=IMAGE_URL)
    labels = image.detect_labels(limit=100)
    print('Label Count: {0}'.format(len(labels))) # Result is 18
    print('Labels:')
    for label in labels:
        print(label.description)
    

    使用ImageAnnotatorClient.annotate_image() 的工作示例

    您也可以在使用ImageAnnotatorClient 时设置最大结果数(此处默认为5),尽管请求会有点冗长:

    from google.cloud import vision
    
    IMAGE_URL = 'https://farm9.staticflickr.com/8215/8267748261_ea142faf5e.jpg'
    
    annot_client = vision.ImageAnnotatorClient()
    request_image = {'source': {'image_uri': IMAGE_URL}}
    label_detection_feature = {
        'type': vision.enums.Feature.Type.LABEL_DETECTION, 'max_results': 100}
    request_features = [label_detection_feature]
    response = annot_client.annotate_image(
        {'image': request_image, 'features': request_features})
    print('Label Count: {0}'.format(len(response.label_annotations))) # Result is 18
    

    使用ImageAnnotatorClient.label_detection() 的示例

    如果你直接使用ImageAnnotatorClient.label_detection(),它总是默认最多5结果,似乎没有办法配置这个限制。

    from google.cloud import vision
    
    IMAGE_URL = 'https://farm9.staticflickr.com/8215/8267748261_ea142faf5e.jpg'
    
    annot_client = vision.ImageAnnotatorClient()
    response = annot_client.label_detection(image={'source': {'image_uri': IMAGE_URL}})
    print('Label Count: {0}'.format(len(response.label_annotations))) # Result is 5
    

    【讨论】:

    • 完美,谢谢!给自己的心理提示:使用 python 时,也不要忘记检查内部文档。作为一个附带问题:这是否也在某处在线记录?确实不容易找到。
    • @Toby - 使用 ImageAnnotatorClient.annotate_image() 的请求中的 max_result 字段记录在 here 中。不幸的是limit argument to Image.detect_labels() is documented in an older version of the documentation
    • 第三个示例的更新:从 0.31.0 (#4817) 开始,ImageAnnotatorClient.label_detection() 有一个 max_results 参数,可通过 Python 的内部 help() 找到。或在线here
    • 关于如何在 PHP 版本中执行此操作的任何想法? (我只得到 10 个结果,但需要更多)我有类似的东西:... = ImageAnnotatorClient()->labelDetection($image);
    猜你喜欢
    • 2017-09-09
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2017-02-04
    相关资源
    最近更新 更多