[英]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:两种可能:
You won't be able to handle a second file without any request trigger.如果没有任何请求触发器,您将无法处理第二个文件。
Regards, Thomas问候, 托马斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.