我有时会收到错误信息
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指向上面的文件。偶尔,这个问题会突然消失。有什么办法可以解决这个问题吗?
发布于 2020-05-14 04:35:35
看起来你很有可能需要下载服务帐户凭据并导出环境变量和GOOGLE_CLOUD_PROJECT,例如在*nix上:
export GOOGLE_CLOUD_PROJECT=blue-jet-123
export GOOGLE_APPLICATION_CREDENTIALS=$HOME/path/to/service.json
我试图重现这个错误,但是我的flask服务器没有启动,你能指出"config/labels.json“的内容吗?你可以把你的文件夹树形结构,以确保我们有相同的目录结构
https://stackoverflow.com/questions/60943745
复制相似问题