image = vision_client.image(AttributeError:'ImageAnnotatorClient'对象没有属性'image'

问题描述 投票:0回答:2
import io,os

# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client (Change the line below******)
vision_client = vision.ImageAnnotatorClient('my-key.json')   

# The name of the image file to annotate (Change the line below 'image_path.jpg' ******)
file_name = os.path.join(
    os.path.dirname(__file__),
    'image_path.jpg') 

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
    image = vision_client.image(
        content=content)

# Performs label detection on the image file
labels = image.detect_labels()

print('Labels:')
for label in labels:
    print(label.description)


python 3.6.5 on windows

这个代码示例给出了标题中提到的错误,是否有人知道如何解决这个问题?

python google-api google-vision
2个回答
0
投票

您的代码有些问题,但我想我找到了所有这些。

import os, io
from google.cloud import vision

vision_client = vision.ImageAnnotatorClient('my-key.json')   

file_name = os.path.join(os.path.dirname(__file__),'image_path.jpg') 

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

labels = vision_client.label_detection({'content': content})
labels = labes.label_annotations()

print('Labels:')
for label in labels:
    print(label.description)

0
投票

这对我有用:

import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources/wakeupcat.jpg')

# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
    content = image_file.read()
image = types.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
    print(label.description)
© www.soinside.com 2019 - 2024. All rights reserved.