我有时会得到错误
google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: ('Connection aborted.', ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
运行时
# server/classifiers/gcloud/identifier.py
import io
import os
import json
from google.cloud import vision
from google.cloud.vision import types
from collections import Counter
client = vision.ImageAnnotatorClient()
with io.open("config/labels.json", "r") as f:
LABELS = json.loads(f.read())
def identify_from_string(blob):
image = types.Image(content=blob)
response = client.object_localization(image=image)
labels = response.localized_object_annotations
objects = set(label.name for label in labels)
c = Counter()
for label, s in LABELS.items():
for ob in objects:
if ob in s:
c[label] += s[ob]
if not c: print("Sorry, the server is currently full.")
return str(c)
在带有response = client.object_localization(image=image)
的行上引发错误。在我的app.py
上,我有
# server/app.py
import os
import json
from server.classifiers.gcloud import identifier
from flask import Flask, Response, render_template, send_file, request
@app.route('/classify', methods=['POST'])
def classify():
app.logger.info("Got image to /classify")
file = request.files['image']
blob = file.read()
results = identifier.identify_from_string(blob)
return Response(response=json.dumps(dict(response)), status=200)
server.classiers.gcloud.identifier指向上面的文件。偶尔,这个问题会突然消失。有办法绕过这个吗?
您似乎很有可能需要下载服务帐户凭据并导出环境变量以及您的GOOGLE_CLOUD_PROJECT,例如在*nix:上
export GOOGLE_CLOUD_PROJECT=blue-jet-123
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/path/to/service.json
我正在尝试重现错误,但我的烧瓶服务器没有启动,你能指出"config/labels.json"的内容吗?你可以树你的文件夹,以确保我们有相同的目录结构