简体   繁体   中英

Why Flask Application not running 2nd time?

I created a Flask application of object detection on video. In this application, I take video input from a user. This video is processed by our application, shows detection on screen and download the video after completion of the process. But when I pass the same video or another video on the same application with the same session it doesn't work. It doesn't show me any error. Here I attached the sample code of my project.

How can I pass another video after completing the process for the first video in this application?

from flask import Flask, render_template, request,  flash, redirect, url_for, send_from_directory
from werkzeug.utils import secure_filename
from PIL import Image
from os import listdir
import numpy as np
import pandas as pd
import cv2
import os
import matplotlib.pyplot as plt
import re
from pathlib import Path
app = Flask(__name__)
UPLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/uploads/'
DOWNLOAD_FOLDER = os.path.dirname(os.path.abspath(__file__)) + '/downloads/'
DIR_PATH = os.path.dirname(os.path.realpath(__file__))
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
ALLOWED_EXTENSIONS = {'mp4'}
app.secret_key = 'random string'
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def my_func(filename):
    name = filename.split('/')[-1].split(".")[0]
    cap= cv2.VideoCapture(filename)
    i=0
    images = []
    while(cap.isOpened()):
        ret, frame = cap.read()
        if not ret: break
        font = cv2.FONT_HERSHEY_SIMPLEX
        org = (5,20)
        fontScale = 0.5
        color = (0, 0, 255)
        fontstroke = 2
        image1 = cv2.putText(frame, 'Done', org, font, fontScale, color, fontstroke)
        images.append(image1)
        i+=1
    height, width, channels = images[0].shape
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter('downloads/' + name + '.mp4', fourcc, 20, (width, height))
    for pic in images:
        out.write(pic)
        cv2.imshow('video',pic)
        if (cv2.waitKey(150) & 0xFF) == ord('q'): # Hit `q` to exit
            break
    out.release()
    cv2.destroyAllWindows()
@app.route('/', methods=['POST', 'GET'])
def index():
    if request.method == 'POST':
        if 'file' not in request.files:
           print('No file attached in request')
           return redirect(request.url)
        file = request.files['file']
        if file.filename == '':
            print('No file selected')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            my_func(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            return redirect(url_for('uploaded_file', filename=filename))
    return render_template('index.html')
@app.route('/downloaded/<filename>', methods=['POST', 'GET'])
def uploaded_file(filename):
    return send_from_directory(app.config['DOWNLOAD_FOLDER'], filename, as_attachment=True)
if __name__ == '__main__':
    app.run(debug = True)

Here I mention the code of index.html file.

<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <title>Model</title>
    </head>
    <body>
    <div align="center">
        <h1>Recognition</h1>
        <h2>Upload Video</h2>
        <form method="post" enctype=multipart/form-data>
            <p><input type=file name=file value="REFRESH">
                <input type=submit value=Upload></p>

        </form>
    </div>
    </body>
    </html>

Two possibilities:

  1. Upload more than one file at a time
  2. Redirect the user to the upload form after the first upload has been finished

You won't be able to handle a second file without any request trigger.

Regards, Thomas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM