我想使用 GOOGLE Vision API,但收到错误“google.api_core.exceptions.ServiceUnavailable: 503 Connect Failed”。
我在连接 Google Vision API 时收到 503 错误,我已经按照说明一步一步操作了。链接如下: https://cloud.google.com/vision/docs/quickstart-client-libraries
我下载我的密钥文件(json 格式)并确保启用 API。我设置环境变量并按照其他指令放置 os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r"/Users/a529157492/Desktop/application_default_credentials 2.json ” 直接在代码中。
但是,我仍然收到错误代码。
import io
import os
from google.cloud import vision
from google.cloud.vision import types
from google.oauth2 import service_account
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] =
r"/Users/a529157492/Desktop/application_default_credentials 2.json"
client = vision.ImageAnnotatorClient()
file_name = os.path.abspath(r'/Users/a529157492/Desktop/demo-img.jpg')
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')[enter image description here][1]
for label in labels:
print(label.description)
但是,我收到以下错误:
Traceback (most recent call last):
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/google/api_core/grpc_helpers.py", line 57, in
error_remapped_callable
return callable_(*args, **kwargs)
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/grpc/_channel.py", line 532, in __call__
return _end_unary_response_blocking(state, call, False, None)
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/grpc/_channel.py", line 466, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Connect Failed"
debug_error_string = "
{"created":"@1569469218.801077000","description":"Failed to create
subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc",
"file_line":2636,"referenced_errors":
[{"created":"@1569469218.801045000","description":"Pick
Cancelled","file":"src/core/ext/filters/client_channel/lb_policy/pick_first
/pick_first.cc","file_line":241,"referenced_errors":
[{"created":"@1569469218.800545000","description":"Connect
Failed","file":"src/core/ext/filters/client_channel/subchannel.cc",
"file_line":663,"grpc_status":14,"referenced_errors":
[{"created":"@1569469218.800511000","description":"Failed to connect to
remote host: FD
shutdown","file":"src/core/lib/iomgr/ev_poll_posix.cc",
"file_line":532,"grpc_status":14,"os_error":"Timeout
occurred","referenced_errors":
[{"created":"@1569469218.800460000","description":"connect() timed
out","file":"src/core/lib/iomgr/tcp_client_posix.cc","file_line":114}],
"target_address":"ipv4:172.217.27.138:443"}]}]}]}"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Users/a529157492/PycharmProjects/untitled7/pictureanalysis.py", line
24, in <module>
response = client.label_detection(image=image)
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/google/cloud/vision_helpers/decorators.py", line 101, in inner
response = self.annotate_image(request, retry=retry, timeout=timeout)
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/google/cloud/vision_helpers/__init__.py", line 72,
in annotate_image
r = self.batch_annotate_images([request], retry=retry, timeout=timeout)
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/google/cloud/vision_v1/gapic/image_annotator_client.py", line 274,
in batch_annotate_images
request, retry=retry, timeout=timeout, metadata=metadata
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/google/api_core/gapic_v1/method.py", line 143, in __call__
return wrapped_func(*args, **kwargs)
File "/Users/a529157492/PycharmProjects/untitled7/venv/lib/python3.6/site-
packages/google/api_core/grpc_helpers.py", line 59, 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 Connect Failed
我遇到了类似的问题,我更新了我的 python 版本并修复了错误。 使用Python版本> = 3.9