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

时间:2019-02-26 16:16:36

标签: python google-api google-vision

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)


Windows上的

python 3.6.5

此代码示例在标题中给了我错误提示,有人知道如何解决该问题吗?

2 个答案:

答案 0 :(得分:0)

您的代码有些错误,但是我想我都找到了。

pi@raspberrypi:/tmp $ valgrind ./a.out
==3900== Memcheck, a memory error detector
==3900== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3900== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3900== Command: ./a.out
==3900== 
Enter a multi line string (ended by ';'):
this is a paragraph
  terminated by
a ;!!
Enter a letter to be removed:
i
ths s a paragraph
  termnated by
a 
==3900== 
==3900== HEAP SUMMARY:
==3900==     in use at exit: 0 bytes in 0 blocks
==3900==   total heap usage: 5 allocs, 5 frees, 2,307 bytes allocated
==3900== 
==3900== All heap blocks were freed -- no leaks are possible
==3900== 
==3900== For counts of detected and suppressed errors, rerun with: -v
==3900== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

答案 1 :(得分: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)