在Flask中处理用户上传的图像 [英] Working with user uploaded image in Flask

查看:91
本文介绍了在Flask中处理用户上传的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个可以摄取图像的模型,可以在像素级别读取它,然后确定图像中是猫还是狗.出于教育目的,我现在正尝试将其放入Flask应用程序中(以前从未使用过Flask).到目前为止,我的工作:用户上传图像,Flask将图像保存到磁盘,图像被操纵并发送到模型,模型预测,然后从模板生成输出页面.现在,我想显示用户上传的图像,但是遇到了问题.适用的代码在这里:

I've built a model that can take in an image, read it at the pixel level, then decide if the image contains a cat or a dog. For educational purposes, I am now trying to put it into a Flask app (never used Flask before). What I have working so far: User uploads image, image is saved to disk by Flask, image is manipulated and sent to the model, model predicts and I generate an output page from a template. Now I'd like to display the image the user uploaded and I'm running into issues. The applicable code is here:

import flask
from flask import request, render_template
import numpy as np
import pandas as pd
from copy import deepcopy
from werkzeug import secure_filename
from PIL import Image
import catdog

# Initialize the app
app = flask.Flask(__name__)

@app.route("/")
def viz_page():
    with open("frontend.html", 'r') as viz_file:
        return viz_file.read()

@app.route("/uploader", methods=["GET","POST"])
def get_image():
    if request.method == 'POST':
        f = request.files['file']
        sfname = 'images/'+str(secure_filename(f.filename))
        f.save(sfname)

        clf = catdog.classifier()
        #clf.save_image(f.filename)

        return render_template('result.html', pred = clf.predict(sfname), imgpath = sfname)


#--------- RUN WEB APP SERVER ------------#

# Start the app server on port 80
# (The default website port)
app.run(host='0.0.0.0', port=5000, debug=True)

和模板:

  <!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Dog vs Cat</title>
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
      <script src="http://d3js.org/d3.v3.min.js"></script>
    </head>
    <body>
      <h3> Prediction: {{ pred }} </h3>
      <br><br>
      <img src={{imgpath}}>
      <script>

      </script>

    </body>
  </html>

我要返回的是预测:yada yada"部分,它运行良好.但是,图像始终返回空白.当我查看它时,它正在查找 http://localhost:5000/images/cat.4.jpg ,这意味着它不在正确的位置,但是我不确定如何动态抓取我想要的图像.我在这里找到了该指南:如何将上传的图像传递到Flask中的template.html ,我觉得这应该足以为我提供帮助,但是我只是没有得到想要让我看到的答案.帮助将不胜感激.

What I'm getting back out is the "Prediction: yada yada" part, which is working well. However, the image always comes back blank. When I look it's looking in http://localhost:5000/images/cat.4.jpg which I know means it's not looking in the right place, but I'm not sure how I can dynamically grab the image I want. I found this guide here: How to pass uploaded image to template.html in Flask and I feel like that should be enough to help me, but I'm just not getting what the answers are trying to get me to see. Help would be greatly appreciated.

奖金问题:是否可能永远不保存图像,而只是将其不断保存在Flask中的内存中?理想情况下,我永远不会保存图像,只使用它,然后在做出预测后将其扔掉.

Bonus question: is it possible to never save the image and just constantly keep it in memory in Flask? Ideally, I'd never save the image, I'd just work with it, then toss it after a prediction was made.

推荐答案

图像应保存在/static文件夹中,因此只需将保存路径更改为static/images

Image should be saved in /static folder, so just change saving path to static/images

sfname = 'static/images/'+str(secure_filename(f.filename))

因此HTML模板将准备就绪,看起来像这样

so than HTML template will be ready it will look like this

<img src="/static/images/your_image_here.jpg">

这篇关于在Flask中处理用户上传的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆