簡體   English   中英

Google Cloud Vision API:檢測徽標TypeError

[英]Google Cloud Vision API: Detect Logos TypeError

我正在嘗試在Python 2.7的本地計算機上實現detect_logos功能。

我有一個功能,該功能最初是在此處給出的Google的label.py文件。

我最終編輯了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)

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

您可能想嘗試使用Vision API的google.cloud客戶端庫。

查看標簽檢測演示 ,該演示可以完成我認為您要嘗試的操作:

>>> 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:不要忘記驗證步驟 您需要將服務帳戶與Cloud Vision一起使用(舊的gcloud auth login無法使用)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM