Google Vision API label detection not working error: 'str' object has no attribute before request
Google Vision API label detection not working error: 'str' object has no attribute before request
我正在尝试使用 Google Vision API 来读取图像的标签。
我正在 Google Compute Engine 实例上执行此操作,可以访问所有 Cloud 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)
一切正常,我没有遇到身份验证问题。但是当我执行上面的行时,我突然得到这个错误。
几乎按照此 page
上的说明进行操作
不太确定哪里出了问题
您向客户端提供一个字符串(文件名?)作为凭据,但在 docs, the credential argument, if passed, should be an instance of Credentials 或其子类之一中指定。
要确定错误,请在设置凭据之前将以下行添加到您的代码中:
print('Credendtials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')))
如果输出是 Credendtials from environ: None
,则脚本在设置了 GOOGLE_APPLICATION_CREDENTIALS 环境变量的上下文中不是 运行。
要解决这个问题,首先确保您创建了服务帐户私钥。可以在链接 https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files and Guideline on Setup
中找到创建服务帐户的步骤
当您创建服务帐户和密钥时,json 文件将自动下载。复制此文件的位置,例如:'C:/awesome-credentials.json'.
接下来安装以下库:
- pip 安装google-cloud-vision
- pip 安装google-云
- pip install --upgrade google-api-python-client
- pip install --upgrade 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)))
我正在尝试使用 Google Vision API 来读取图像的标签。
我正在 Google Compute Engine 实例上执行此操作,可以访问所有 Cloud 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)
一切正常,我没有遇到身份验证问题。但是当我执行上面的行时,我突然得到这个错误。
几乎按照此 page
上的说明进行操作不太确定哪里出了问题
您向客户端提供一个字符串(文件名?)作为凭据,但在 docs, the credential argument, if passed, should be an instance of Credentials 或其子类之一中指定。
要确定错误,请在设置凭据之前将以下行添加到您的代码中:
print('Credendtials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')))
如果输出是 Credendtials from environ: None
,则脚本在设置了 GOOGLE_APPLICATION_CREDENTIALS 环境变量的上下文中不是 运行。
要解决这个问题,首先确保您创建了服务帐户私钥。可以在链接 https://google-auth.readthedocs.io/en/latest/user-guide.html#service-account-private-key-files and Guideline on Setup
中找到创建服务帐户的步骤当您创建服务帐户和密钥时,json 文件将自动下载。复制此文件的位置,例如:'C:/awesome-credentials.json'.
接下来安装以下库:
- pip 安装google-cloud-vision
- pip 安装google-云
- pip install --upgrade google-api-python-client
- pip install --upgrade 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)))