所以我正在尝试使用谷歌的视觉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)
它说“客户”对象没有属性,“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'
答案 0 :(得分:1)
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