Python “谷歌Api愿景”;在“请求”之前;错误
所以我尝试使用谷歌的视觉api,它需要识别标签、面部和文本检测……等等 但不幸的是,我无法修复导致我们落后的错误 源代码Python “谷歌Api愿景”;在“请求”之前;错误,python,api,Python,Api,所以我尝试使用谷歌的视觉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():
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'
错误:root:AuthMetadataPluginCallback“”引发异常!
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/grpc/\u plugin\u wrapping.py”,第77行,在调用中_
回调(状态,回调)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/auth/transport/grpc.py”,第77行,在_
回调(self.\u获取\u授权\u头(上下文),无)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/auth/transport/grpc.py”,第61行,在“获取授权”标题中
自我认证。在请求之前(
AttributeError:“客户端”对象没有“在请求之前”属性
回溯(最近一次呼叫最后一次):
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api\u core/grpc\u helpers.py”,第54行,错误为\u重新映射\u可调用
返回可调用函数(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/grpc/_channel.py”,第487行,在调用中_
返回\u结束\u一元\u响应\u阻塞(状态、呼叫、错误、截止日期)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/grpc/_channel.py”,第437行,在一元响应中
提升集合点(状态,无,无,截止日期)
grpc._通道_会合点:
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“testAPI.py”,第43行,在
运行快速启动()
运行快速启动中第33行的文件“testAPI.py”
响应=客户端.text\u检测(图像=图像)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/cloud/vision\u helpers/decorators.py”,第110行,内部
返回self.annotate_映像(请求,重试=重试,超时=超时)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/cloud/vision\u helpers/\uuuuuuu init\uuuuuu.py”,第67行,在annotate\u图像中
r=self.batch\u注释图像([request],retry=retry,timeout=timeout)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/cloud/vision\u v1/gapic/image\u annotator\u client.py”,第158行,在批注释图像中
请求,重试=重试,超时=超时)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api_core/gapic_v1/method.py”,第139行,in_call_
返回包装函数(*args,**kwargs)
文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site packages/google/api\u core/grpc\u helpers.py”,第56行,错误为\u重新映射\u可调用
六、从(例外情况。从grpc错误(exc),exc)
文件“”,第3行,从
google.api_core.exceptions.ServiceUnavailable:503从插件获取元数据失败,错误为:“客户端”对象没有“before_请求”属性
奇怪,我让它运行起来了。我必须在对os.environ的调用中直接声明凭据的路径。也许尝试一下,而不是使用“从存储”方法
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如果在InstanceClient中,您只需返回客户端并注释掉图像加载代码,那么客户端是否实际根据API进行身份验证?如果让我猜的话,这似乎是问题的根源。你什么意思?你能在下面的部分回答中解释更多吗?Wit没有给出错误,但是当我运行它时,什么也没有发生,因为图像没有链接到“InstanceClient方法”更新了我的答案-尝试用os.environ更改你的身份验证策略