Python Google Vision API标签检测不工作错误:';str';对象在请求之前没有属性

Python Google Vision API标签检测不工作错误:';str';对象在请求之前没有属性,python,google-cloud-platform,google-cloud-vision,Python,Google Cloud Platform,Google Cloud Vision,我正在尝试使用来读取图像的标签 我在一个谷歌计算引擎实例上执行此操作,可以访问所有云API。我正在使用服务帐户进行身份验证 我一直得到以下错误 这就是我正在执行的代码 import io #from google.cloud import storage #from google.cloud.vision_v1 import ImageAnnotatorClient from google.oauth2 import service_account # using old version of

我正在尝试使用来读取图像的标签

我在一个谷歌计算引擎实例上执行此操作,可以访问所有云API。我正在使用服务帐户进行身份验证

我一直得到以下错误

这就是我正在执行的代码

import io
#from google.cloud import storage
#from google.cloud.vision_v1 import ImageAnnotatorClient
from google.oauth2 import service_account
# using old version of API

from google.cloud import vision
from google.cloud.vision import types


image_client = vision.ImageAnnotatorClient(credentials='credentials.json')


with io.open('/home/username/instagram-ml/userbucket/images/test_image.jpg','rb') as image_file:
    content = image_file.read()

image = types.Image(content=content)
#
image_response = image_client.label_detection(image =image)

labels = image_response.label_annotations
直排

image_response = image_client.label_detection(image =image)
一切正常,我没有身份验证问题。但是当我执行上面这一行时,我突然得到了这个错误

基本上是按照上面的说明来做的


不太确定出现了什么问题

您向客户端提供了一个字符串(文件名?)作为凭据,但如中所述,凭据参数(如果传递)应该是其子类的一个实例或其子类之一。

要确定错误,请在设置凭据之前添加以下代码行:

print('Credendtials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')))
如果输出为来自environ:None的
Credendtials
,则脚本未在设置了GOOGLE\u APPLICATION\u CREDENTIALS环境变量的上下文中运行

要解决此问题,请首先确保创建服务帐户私钥。在链接和中可以找到创建服务帐户的步骤

创建服务帐户和密钥时,会自动下载一个json文件。复制此文件的位置,例如:“C:/awesome credentials.json”

接下来安装以下库:

  • pip安装googlecloudvision
  • 安装谷歌云
  • pip安装--升级google api python客户端
  • pip安装--升级google auth
  • 然后,使用以下代码在脚本中设置凭据:

    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_file('C:/awesome-credentials.json')
    client = vision.ImageAnnotatorClient(credentials=credentials)
    
    这是整个代码的一个示例:

    from google.cloud import vision
    import io
    import os
    from google.oauth2 import service_account
    
    
    def detect_text(path):
        """Detects text in the file."""
        credentials = service_account.Credentials.from_service_account_file('C:/awesome-credential.json')
    
        print('Credendtials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')))
        client = vision.ImageAnnotatorClient(credentials=credentials)
    
        with io.open(path, 'rb') as image_file:
            content = image_file.read()
    
        image = vision.types.Image(content=content)
    
        response = client.text_detection(image=image)
        texts = response.text_annotations
        print('Texts:')
    
        for text in texts:
            print('\n"{}"'.format(text.description))
    
            vertices = (['({},{})'.format(vertex.x, vertex.y)
                        for vertex in text.bounding_poly.vertices])
    
            print('bounds: {}'.format(','.join(vertices)))