本文介绍了如何在静态文件夹中使用Flask中的opencv显示新保存的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在显示网页上的输出之前先对图像进行输入并对其进行一些处理。但是,当我尝试不同的图像时,会显示较旧的图像而不是新的图像。



以下是代码片段:



pre $ 从瓶子导入Flask,render_template,请求

导入pro
$ b $ app = Flask(__ name__)


@ app.route('/')
def index():
return render_template(index.html)

@app ()/ $ / $ / $ / $ / $ / $ / $ / $ / $ / $ / $ / $ / uploads / up.jpg'

a = pro.pro(n)

return render_template(out.html)

if __name__ == '__main__':
app.run(debug = True)

进程代码:

  import cv2 


def pro(p):
img = cv2 .imread(p,1)

#在这里的一些处理

path ='/ home / vivek / Desktop / CL3 / d / booth / trial / static /'
cv2.imwrite(str(path)+'out.jpg',img)
cv2.waitKey(0)

np ='/home/vivek/Desktop/CL3/d/booth/trial/static/out.jpg'

return np

最后是HTML文件:

 <!DOCTYPE html> 
< html>
< body>
< h2> OUTPUT< / h2>
< img src ={{url_for('static',filename ='out.jpg')}}alt =OUTPUTstyle =width:128px; height:128px;>
< / body>
< / html>


解决方案

您需要禁用缓存
$ b $ $ p $ def process():
....
resp = make_response(render_template('out.html'))
resp.cache_control.no_cache = True
return resp

更新

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ =='out.jpg':
return 0
返回Flask.get_send_file_max_age(self,name)

app = MyFlask(__ name__)


I'm trying to take image input and do some processing on it before displaying the output on the webpage. However, when I try a different image, the older image is displayed instead of the new one.

Here are the code snippets:

from flask import Flask, render_template, request

import pro

app = Flask(__name__)


@app.route('/')
def index():
    return render_template("index.html")

@app.route('/process', methods=['GET', 'POST'])
def process():
    n = '/home/vivek/Desktop/CL3/d/booth/trial/uploads/up.jpg'

    a = pro.pro(n)

    return render_template("out.html")

if __name__ == '__main__':
    app.run(debug=True) 

The process code:

import cv2


def pro(p):
    img = cv2.imread(p, 1)

    # some process here

    path = '/home/vivek/Desktop/CL3/d/booth/trial/static/'
    cv2.imwrite(str(path) + 'out.jpg',img)
    cv2.waitKey(0)

    np = '/home/vivek/Desktop/CL3/d/booth/trial/static/out.jpg'

    return np

And finally the HTML file:

<!DOCTYPE html>
<html>
<body>
<h2>OUTPUT</h2>
<img src="{{url_for('static', filename='out.jpg')}}" alt="OUTPUT" style="width:128px;height:128px;">
</body>
</html>
解决方案

You need to disable cache

def process():
    ....
    resp = make_response(render_template('out.html'))
    resp.cache_control.no_cache = True
    return resp

Updated

class MyFlask(Flask):
    def get_send_file_max_age(self, name):
        if name == 'out.jpg':
            return 0
        return Flask.get_send_file_max_age(self, name)

app = MyFlask(__name__)

这篇关于如何在静态文件夹中使用Flask中的opencv显示新保存的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 22:38