Google云视觉API:Detect Logos TypeE

2024-06-01 01:51:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python2.7的本地机器上实现detect_logos功能。在

我有一个最初是谷歌的功能标签.py在here中给定的文件。在

我最终编辑了main函数,它在吐出一些标签后调用了detect_logos()。我现在在检测logos(path)函数的第“logos=image.detect_徽标()。在

TypeError:construct\u settings()获得意外的关键字参数“metrics\u headers”

这个错误显然源于伊曼格注释者_客户端.py在vision api中的文件。我一定错过了什么。在

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““


Tags: imageifresponserequestservicecontentfilenamefile
1条回答
网友
1楼 · 发布于 2024-06-01 01:51:05

您可能想尝试使用google.cloud客户机库来代替visionapi。在

看看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

注意:别忘了the authentication step!您需要使用具有cloudvision的服务帐户(旧的gcloud auth login不起作用)。在

相关问题 更多 >