烧瓶图像变量“粘滞”

时间:2019-05-06 17:26:51

标签: python flask

我有一个Flask应用程序,第一次运行,此后html始终显示第一个预测图像,即使新的预测图像已写入文件夹中

我曾尝试从上传文件夹中删除图片,但原始预测仍然存在。

下面是我的烧瓶应用程序

UPLOAD_FOLDER = ''
ALLOWED_EXTENSIONS = set(['jpg', 'png'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in 
    ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_file():

if request.method == 'POST':
    # check if the post request has the file part
    if 'file' not in request.files:
        flash('No file part')
        return redirect(request.url)

    file = request.files['file']

    # if user does not select file, browser also
    # submit an empty part without filename
    if file.filename == '':
        flash('No selected file')
        return redirect(request.url)

    if file and allowed_file(file.filename):
        filename = secure_filename(file.filename)
        #filename = file.filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
        image = open_image(filename)
        image_url = url_for('uploaded_file', filename=filename)
        think = learn.predict(image)
        think_np = np.array(think[1])
        think_np.shape = (256,256)
        think_np = think_np.astype(int)
        think_np[think_np > 0] = 255
        think_im = Image.fromarray((think_np).astype('uint8'), mode='L')
        think_im.save(os.path.join(app.config['UPLOAD_FOLDER'], 'think2_im.png'))
        think_im_url = url_for('uploaded_file', filename='think2_im.png')
        print(think_im_url)
        #image.show(y=learn.predict(image)[0])
        return '''<h1>The cell image is:</h1>
        <img src= "{}" height = "85" width="200"/>
        <h1>The cell segmentation is:</h1>
        <img src= "{}" height = "85" width="200"/>'''.format(image_url, think_im_url)


return '''
<!doctype html>
<title>Upload new File</title>
<h1>Upload an image of Cells</h1>
<form method=post enctype=multipart/form-data>
  <input type=file name=file>
  <input type=submit value=Upload>
</form>
'''


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'], filename)

我第一次运行此程序,一切正常。但是,如果我再次运行,think_im_url总是显示第一个预测的图像。即使我重新启动内核。即使我进入上载文件夹并删除图像。而且,尽管如此,写入上载文件夹的预测图像是正确的(新的)预测图像。 HTML中显示的内容始终保持不变。

如果我进入代码并更改名称,即将“ think2_img.png”更改为“ think3_img.png”,则该过程将再次开始。第一次效果很好,但是即使新的预测会覆盖上载文件夹中的“ think”预测,HTML也只会显示原始预测。

image_URL每次都应该更改。

0 个答案:

没有答案