在 PythonAnywhere Flask 路由中调用 ImageAI classifyImage 调用超时,但不是在本地或服务器初始化中

问题描述 投票:0回答:0

我在使用 ImageAI、PythonAnywhere 和 Flask 时遇到了一些非常奇怪的问题。我正在使用 ImageAI 库(版本 3.0.3)对托管在 PythonAnywhere(付费帐户)上的 Flask(版本 2.1.2,Python 3.10.5)网站上的图像进行分类。

当我在本地运行网站时,ImageAI 库没有问题,一切都按预期工作。

当我尝试在 PythonAnywhere 上运行

prediction.classifyImage(imagepath)
时,该方法永远不会返回并且站点永远加载,最终超时。两种情况下的代码完全相同。

from imageai.Classification import ImageClassification
import os

execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-19c8e357.pth"))
prediction.loadModel()

def classify(image):
  print("Classifying...")
  predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, image), result_count=5 )
  print("Classified")
  for eachPrediction, eachProbability in zip(predictions, probabilities):
    print(eachPrediction , " : " , eachProbability)
  return (predictions, probabilities)

我从我的烧瓶应用程序中的路线调用

classify
方法,

@app.route('/about')
def about():
    predictions, probabilities = classify("car.jpg")
    return render_template('about.html')

在 PythonAnywhere 服务器中,

Classifying...
被打印但
Classified
从未被打印。

当我从 PythonAnywhere bash 控制台中的

python
控制台运行完全相同的代码时,该方法成功返回并提供预期的输出。如果我从路由外部调用
classify
函数,即在我设置我的烧瓶应用程序 (
app = Flask(__name__)
) 之后,该方法成功返回并给出预期的输出。

所以问题一定是函数在路由中被调用,在 PythonAnywhere 上而不是在开发中。我很难过,有没有人知道是什么原因造成的?谢谢。

python flask pythonanywhere imageai
© www.soinside.com 2019 - 2024. All rights reserved.