我有时会收到以下错误信息
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.classifiers.gcloud.identifier指向上面的文件。偶尔,这个问题会突然消失。有什么办法解决吗?
看来你很有可能需要下载服务账户凭证,并导出环境变量以及你的GOOGLE_CLOUD_PROJECT,例如在*nix上。
export GOOGLE_CLOUD_PROJECT=blue-jet-123
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/path/to/service.json
我想重现这个错误,但我的flask服务器没有启动,你能不能说明configlabels.json的内容,你可以把你的文件夹树化,以确保我们有相同的目录结构。