【问题标题】:Google Cloud Vision API: Detect Logos TypeErrorGoogle Cloud Vision API:检测徽标类型错误
【发布时间】:2017-06-16 10:29:09
【问题描述】:

我正在尝试在 Python 2.7 中的本地计算机上实现 detect_logos 功能。

我有一个函数,它最初是 Google 的 label.py 文件,在 here 中给出。

我最终编辑了 main 函数,它在吐出一些标签后调用了 detect_logos()。我现在在“logos = image.detect_logos()”行的 detect_logos(path) 函数中遇到以下错误。

TypeError:construct_settings() 得到了一个意外的关键字参数“metrics_headers”

此错误显然源于视觉 api 中的 immage_annotator_client.py 文件。我一定是错过了什么。

def detectLabelsLogos(photo_file):

    #configurable options: FREE TO CHANGE
    resizedFileName = 'clone.jpeg'  #name of resized files (are deleted at the end)
    labelCount = 5                  #max number of labels 

    #nonconfigurable options: DO NOT TOUCH
    resized = False
    filename = photo_file
    service = googleapiclient.discovery.build('vision', 'v1')

    #initial size check
    imgSizeInBytes = os.stat(photo_file).st_size

    if imgSizeInBytes >= MAX_IMAGE_SIZE_IN_BYTES:
        print "\n[Image too large...resizing...]"
        resized = True
        filename = resizedFileName

        with Image.open(photo_file) as image:
            newimg = resizeimage.resize_thumbnail(image, [1600, 800])
            newimg.save(filename, image.format)
            newimg.close()

    imgSizeInBytes = os.stat(filename).st_size

    #ensure file is not empty
    if imgSizeInBytes > 0:
        # [START construct_request]
        with open(filename, 'rb') as image:
            image_content = base64.b64encode(image.read())
            service_request = service.images().annotate(body={
                'requests': [{
                    'image': {
                        'content': image_content.decode('UTF-8')
                    },
                    'features': [{
                        'type': 'LABEL_DETECTION',
                        'maxResults': labelCount
                    }]
                }]
            })
        # [END construct_request]

            # [START parse_response]
            response = service_request.execute()

            detect_logos(filename);

            for i in range(0, labelCount):
                if i >= len(response['responses'][0]['labelAnnotations']):
                    print "\n[Exhausted Responses]"
                    break
                label = response['responses'][0]['labelAnnotations'][i]['description']
                print('\nFound label: %s' % (label))
            # [END parse_response]

            image.close()

        #delete resized file
        if resized:
            os.remove(filename)   
else:
    print "[Invalid File Input: Empty File]"

print "\n"

def detect_logos(path):
    """Detects logos in the file."""

    vision_client = vision.Client()
    print vision_client

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

    image = vision_client.image(content=content)

    logos = image.detect_logos()
    print('\nLogos:')

    for logo in logos:
        print(logo.description)

我也一直在做“set GOOGLE_APPLICATION_CREDENTIALS=/blah/blah/serviceaccountkey.json”

【问题讨论】:

    标签: python google-cloud-vision


    【解决方案1】:

    您可能想尝试改用 Vision API 的 google.cloud 客户端库。

    查看the label detection demo,它做了我认为你正在尝试做的事情:

    >>> from google.cloud import vision
    >>> client = vision.Client()
    >>> image = client.image(source_uri='gs://my-storage-bucket/image.jpg')
    >>> labels = image.detect_labels(limit=3)
    >>> labels[0].description
    'automobile'
    >>> labels[0].score
    0.9863683
    

    PS:别忘了the authentication step!您需要使用 Cloud Vision 的服务帐号(旧的 gcloud auth login 不起作用)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 1970-01-01
      • 2016-10-01
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多