question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'

See original GitHub issue

I’m trying to iterate through images in a directory and get their labels through goodle_api_vision. This is my code:

def run_quickstart():
import io
import os
import cv2
import numpy as np
from google.cloud import vision
from google.cloud.vision import types

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)
    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)
    
if __name__ == '__main__':
    run_quickstart()

the error that I get is at this part of code image = types.Image(content=content). The full error being

<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'

I put the line content = content.tobytes() because otherwise I get this error has type <class 'numpy.ndarray'>, but expected one of: ((<class 'bytes'>,),) (always at this method call image = types.Image(content=content))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

34reactions
theacodescommented, May 21, 2018

@stefano9-4 unfortunately you can’t just pass a string directly as the credentials argument (although perhaps we should make it where you can), you should do this instead:

from google.oauth2 import service_account

credentials = service_account.Credentials. from_service_account_file('service_acc_key.json')

client = vision.ImageAnnotatorClient(credentials=credentials)
10reactions
tseavercommented, Feb 20, 2019

@thyatt67 Your issue is different than this one: it is really the same as #7381: you are creating once client instance per image, which is causing you to be rate-limited on the authentication service. As I suggested on that issue, create a single Client instance at the top-level of your script, and pass it through to your detect_safe_search_uri function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

503 Getting metadata from plugin failed with error: 'str' object ...
google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request ...
Read more >
RE: 503 Getting metadata from plugin failed with error
RE: 503 Getting metadata from plugin failed with error: 'GoogleRefreshTokenClient' object has no attribute 'before_request'. 123 views.
Read more >
google.api_core.exceptions.ServiceUnavailable: 503 Getting ...
ServiceUnavailable : 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'before_request'
Read more >
How To Fix A "Google.Apicore.Exceptions.Serviceunavailable
google.apicore.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: 'str' object has no attribute 'beforerequest' Solution 1:.
Read more >
Resolved: 503 Getting metadata from plugin failed with error ...
Resolved: 503 Getting metadata from plugin failed with error: 'GoogleRefreshTokenClient' object has no attribute 'before_request' when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found