gpt4 book ai didi

python - 谷歌 Api 视觉, "before_request"错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:06:37 24 4
gpt4 key购买 nike

所以我尝试使用 Google 的视觉 API,它可以识别标签、面部和文本检测......等

但不幸的是,我无法修复导致我们落后的错误。

源代码

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
from google.cloud import storage

def getCredentials():
storage_client = storage.Client.from_service_account_json(
'eyeHear-cad56858f9be.json')
return storage_client
# buckets = list(storage_client.list_buckets())
# print(buckets)

def instantiateClient():
# Instantiates a client
client = vision.ImageAnnotatorClient(
credentials=getCredentials())

# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'images/demo-image.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)

它说“Client”对象没有属性“before_request”

这就是我陷入困境的地方,我不知道该做什么。

错误

ERROR:root:AuthMetadataPluginCallback "<google.auth.transport.grpc.AuthMetadataPlugin object at 0x104746ef0>" raised exception!
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_plugin_wrapping.py", line 77, in _call_
callback_state, callback))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/auth/transport/grpc.py", line 77, in _call_
callback(self._get_authorization_headers(context), None)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/auth/transport/grpc.py", line 61, in _get_authorization_headers
self._credentials.before_request(
AttributeError: 'Client' object has no attribute 'before_request'
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 54, in error_remapped_callable
return callable_(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 487, in _call_
return _end_unary_response_blocking(state, call, False, deadline)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/grpc/_channel.py", line 437, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, Getting metadata from plugin failed with error: 'Client' object has no attribute 'before_request')>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "testAPI.py", line 43, in <module>
run_quickstart()
File "testAPI.py", line 33, in run_quickstart
response = client.text_detection(image=image)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/vision_helpers/decorators.py", line 110, in inner
return self.annotate_image(request, retry=retry, timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/vision_helpers/__init__.py", line 67, in annotate_image
r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/cloud/vision_v1/gapic/image_annotator_client.py", line 158, in batch_annotate_images
request, retry=retry, timeout=timeout)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/gapic_v1/method.py", line 139, in _call_
return wrapped_func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 56, in error_remapped_callable
six.raise_from(exceptions.from_grpc_error(exc), exc)
File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'Client' object has no attribute 'before_request'

最佳答案

奇怪,我已经运行了。我必须直接在 os.environ 调用中声明我的凭据的路径。也许尝试一下而不是使用 from storage 方法?

import io
import os

# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "/Users/jeff/Downloads/test_client.json"

# Instantiates a client
client = vision.ImageAnnotatorClient()

# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'collie.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)

输出为:

dog
dog breed
dog like mammal
dog breed group
border collie
karelian bear dog
snout
native american indian dog
icelandic sheepdog
canadian eskimo dog

输入为

wget https://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Border_Collie_liver_portrait.jpg/220px-Border_Collie_liver_portrait.jpg

然后重命名为collie.jpg

关于python - 谷歌 Api 视觉, "before_request"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48846121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com