烧瓶显示用户上传的图像

时间:2018-08-14 00:35:48

标签: image flask upload python-imaging-library

我正在制作烧瓶应用程序,并且已经完成了大部分工作,但是仍然有问题。目前,我允许用户上传图像文件,并对其进行Keras转换,以预测它是猫还是狗。我可以正确显示预测结果,但我也看到许多其他模型也显示图像(我在网上搜索过)。我希望这样做而不必将图像保存在本地。渲染模板时,我尝试传递PIL图像,但是PIL只是传递了Image对象,但是似乎没有任何方法可以获取图像本身进行显示。这是我正在使用的代码。如果有人可以提供帮助,将不胜感激。

@app.route('/')
@app.route('/Predict_home', methods = ['GET','POST'])
def predict():
    if request.method == 'POST':
        encoded_original = request.files['picture']
        encoded = base64.b64encode(encoded_original.read())
        decoded = base64.b64decode(encoded)
        image = Image.open(io.BytesIO(decoded))
        preprocessed_image = preprocess_image(image, target_size=(150, 150))
        prediction = make_predictions(preprocessed_image)
        response = {
            'prediction': {
                'dog': prediction[0][1],
                'cat': prediction[0][0]
            }}

        return render_template('Predict_home.html', encoded = response, 
file = send_file(image, mimetype='image/*'))

    return render_template('base.html')

0 个答案:

没有答案
相关问题