Второй запрос на публикацию в приложении 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'--framern' b'Content-Type: image/jpegrnrn'   imjpeg   b'rnrn')
@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')
 

Комментарии:

1. пожалуйста, помогите мне, я не могу понять, почему он показывает ошибку, которую я пытаюсь сделать уже много дней