【问题标题】:image = vision_client.image( AttributeError: 'ImageAnnotatorClient' object has no attribute 'image'image = vision_client.image( AttributeError: 'ImageAnnotatorClient' 对象没有属性 'image'
【发布时间】:2019-02-26 16:16:35
【问题描述】:
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

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

【问题讨论】:

  • 也许你能告诉我我的代码示例有什么问题?
  • 还有其他人吗?我只需要知道如何修复我的代码
  • 就在那里...ImageAnnotatorClient 根本没有名为image 的属性,这就是image = vision_client.image 不起作用的原因
  • 那我该如何解决呢?换什么?

标签: python google-api google-vision


【解决方案1】:

这对我有用:

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)

【讨论】:

    【解决方案2】:

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

    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)
    

    【讨论】:

    • 首先 - 我也使用了 io 模块。其次,您应该写“愿景”而不是“愿景”。在我修复了所有错误之后,我得到了一堆错误,所以你也没有回答我的问题,你也没有帮助我修复它们..
    • 另一个有什么帮助吗?来吧伙计们,以前使用此模块的人一定很容易......
    • 谢谢!在我更改之后,我得到了那个错误 - Traceback(最近一次调用最后一次):文件“my_file.py”,第 11 行,在 标签 = vision_client.label_detection(content).label_annotations() 文件“C:\Users\AppData \Local\Programs\Python\Python36-32\lib\site-
    • 有什么新东西吗?
    猜你喜欢
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2022-11-04
    • 2012-12-01
    相关资源
    最近更新 更多