简体   繁体   English

为什么 Flask 应用程序没有第二次运行?

[英]Why Flask Application not running 2nd time?

I created a Flask application of object detection on video.我创建了一个 Flask 视频对象检测应用程序。 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.这里我提到了 index.html 文件的代码。

<!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问候, 托马斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Turtle 窗口每运行 2 次就会崩溃一次 - Python Turtle window crashes every 2nd time running 为什么第二个线程没有启动? - Why is the 2nd thread not starting? 在第二个SSH终端中运行第二个pygame程序(用于第二个显示)会暂停,直到按下^ C为止 - running 2nd pygame program (for 2nd display) in 2nd SSH terminal pauses until ^C is pressed Instabot API for Python 在第二次运行代码后引发错误 - Instabot API for Python raises error after running code for the 2nd time 运行第二个zmq.eventloop.ioloop - running a 2nd zmq.eventloop.ioloop 奇怪的行为:使用tkfiledialog的python应用程序,对话框没有第二次打开 - weird behaviour: python application using tkfiledialog, dialog doesn't open a 2nd time 无法第二次执行 zip 项目 - cant execute zip item for 2nd time 为什么我的缩略图PIL功能第二次不起作用? - Why is it that my thumbnail PIL function won't work the 2nd time? 在运行在 gunicorn 上的 Flask 应用程序中使用日志轮换时,为什么要同时在多个文件上写入日志? - Why writing logs on multiple files at the same time when using log rotation in my flask application running on gunicorn? 为什么我的循环第二次运行时出现错误? TypeError:“ int”对象没有属性“ __getitem__” - Why do I get and error when my loop runs the 2nd time? TypeError: 'int' object has no attribute '__getitem__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM