【问题标题】:google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'google.api_core.exceptions.ServiceUnavailable:503 从插件获取元数据失败并出现错误:“str”对象没有属性“before_request”
【发布时间】:2019-05-12 20:08:40
【问题描述】:

我正在尝试遍历目录中的图像并通过 goodle_api_vision 获取它们的标签。这是我的代码: def run_quickstart(): 导入 io 导入操作系统 导入简历2 将 numpy 导入为 np 从 google.cloud 导入视觉 从 google.cloud.vision 导入类型

client = vision.ImageAnnotatorClient(credentials = 'service_acc_key.json')

path = 'E:\wrand\\'
for image_path in os.listdir(path):

    file_name = path + image_path
    content = cv2.imread(file_name)
    # Loads the image into memory
    #with io.open(file_name, 'rb') as image_file:
       # content = image_file.read()
    content = content.tobytes()
    print(type(content))

    image = types.Image(content=content)
    print(type(image))
    response = client.label_detection(image=image)
    labels = response.label_annotations
    print('Labels:')
    for label in labels:
        print(label.description)
        # [END vision_quickstart]

if __name__ == '__main__':
    run_quickstart()

我得到的错误是这部分代码image = types.Image(content=content)。完整的错误是

<class 'bytes'>
<class 'google.cloud.vision_v1.types.Image'>
ERROR:root:AuthMetadataPluginCallback " 
<google.auth.transport.grpc.AuthMetadataPlugin object at 
0x0000028520DCA2E8>" raised exception!
Traceback (most recent call last):
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site- 
packages\grpc\_plugin_wrapping.py", line 77, in __call__
    callback_state, callback))
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site- 
packages\google\auth\transport\grpc.py", line 77, in __call__
    callback(self._get_authorization_headers(context), None)
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site
packages\google\auth\transport\grpc.py", line 61, in 
_get_authorization_headers
    self._credentials.before_request(
AttributeError: 'str' object has no attribute 'before_request'
Traceback (most recent call last):
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site- 
packages\google\api_core\grpc_helpers.py", line 54, in error_remapped_callable
return callable_(*args, **kwargs)
File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site-packages\grpc\_channel.py", line 487, in __call__
return _end_unary_response_blocking(state, call, False, deadline)
File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\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: 'str' object has no attribute 'before_request')>

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

Traceback (most recent call last):
  File "Tutorial.py", line 32, in <module>
run_quickstart()
  File "Tutorial.py", line 24, in run_quickstart
response = client.label_detection(image=image)
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site-packages\google\cloud\vision_helpers\decorators.py", line 117, in inner
response = self.annotate_image(request, retry=retry, timeout=timeout)
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site-packages\google\cloud\vision_helpers\__init__.py", line 67, in annotate_image
r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site-packages\google\cloud\vision_v1\gapic\image_annotator_client.py", line 165, in batch_annotate_images
request, retry=retry, timeout=timeout, metadata=metadata)
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\site-packages\google\api_core\gapic_v1\method.py", line 139, in __call__
return wrapped_func(*args, **kwargs)
  File "C:\Users\hi_pe\AppData\Local\Programs\Python\Python36\lib\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: 'str' object has no attribute 'before_request'

我把线放在content = content.tobytes() 因为否则我得到这个错误has type &lt;class 'numpy.ndarray'&gt;, but expected one of: ((&lt;class 'bytes'&gt;,),)(总是在这个方法调用image = types.Image(content=content)

【问题讨论】:

    标签: python google-vision


    【解决方案1】:

    不幸的是,您不能直接将字符串作为凭据参数传递(尽管我们应该尽可能地使用它),您应该这样做:

    from google.oauth2 import service_account
    
    credentials = service_account.Credentials.from_service_account_file('service_acc_key.json')
    
    client = vision.ImageAnnotatorClient(credentials=credentials)
    

    【讨论】:

    • 这太烦人了。这些身份验证流程让 GC 工具难以使用
    【解决方案2】:

    在调用客户端之前添加以下行

    os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = path/to/service-account.json/file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-25
      • 2021-10-12
      • 2021-03-18
      • 2021-08-25
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2016-04-07
      相关资源
      最近更新 更多