Flask 应用程序中的第二个 POST 请求引发错误

时间:2021-05-07 17:42:12

标签: flask

我正在制作一个用于面部检测的 Flask 应用程序。为此,我创建了一个用于提交图像的表单。然后在将图像发送到路由 through_imagess 后,代码检测人脸并告诉人脸数量。当我第一次在表单中提交图像时,它会显示计数。但是在第二次提交图像后立即生成以下错误:

"Debugging middleware caught exception in streamed response at a point where response headers were already sent"

你能帮我解决这个错误吗?

以下是我使用的代码:

import numpy as np
import cv2 as cv
from werkzeug.utils import secure_filename
from flask import Flask,render_template,request,url_for,Response,stream_with_context,session,flash,make_response
app = Flask(__name__, instance_relative_config=True, template_folder='template')
app.secret_key="hello"
filenames=""
message=""
capacity=0
capacity1=0
@app.route('/hellos')
def hellos():
    return render_template('home.html')
@app.route('/through_image')
def through_image():
    return render_template('image.html')
def gen1(image,capacity):
    #img=cv.resize(image,(0,0),fx=0.5,fy=0.5)
    img=cv.cvtColor(image,cv.COLOR_RGB2GRAY)
    face_cascade=cv.CascadeClassifier(cv.data.haarcascades+'haarcascade_frontalface_alt2.xml')
    detect=face_cascade.detectMultiScale(img,scaleFactor=1.1,minNeighbors=5)
    i=0
    for x,y,w,h in detect:
        rectangle=cv.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
        i=i+1
        if i<=capacity:
            rectangle=cv.putText(rectangle,'face num'+str(i),(x-20,y- 
            10),cv.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),1)
        else:
            rectangle=cv.putText(rectangle,'exceeded',(50,50),cv.FONT_HERSHEY_SIMPLEX,0.7, 
            (0,0,255),1)
    imjpeg=cv.imencode('.jpg',rectangle)[1].tobytes()
    yield(b'--frame\r\n'+b'Content-Type: image/jpeg\r\n\r\n' + imjpeg + b'\r\n\r\n')
@app.route('/through_images',methods=['GET','POST'])
def through_images():
    if request.method=='POST':
        f=request.files['image[]']
        global filenames
        global capacity1
        capacity1=0
        f1=request.form['Capacity']
        capacity1=int(f1)
        l=0
        filenames+=secure_filename(f.filename)
        l=len(filenames)
        if l>0:
            print(1)
    f.save('path+secure_filename(f.filename))
    return render_template('imageoutput.html')
@app.route('/through_imagess')
def through_imagess():
    img=cv.imread('/Users/saikscbs/Documents/project2/proj3/Upload_Folder/'+filenames)  
    return Response(stream_with_context(gen1(img,capacity1)),mimetype='multipart/x-mixed- 
    replace; boundary=frame')

0 个答案:

没有答案
相关问题