python - 为什么 Flask 应用程序没有第二次运行?
问题描述
我在视频上创建了一个对象检测的 Flask 应用程序。在这个应用程序中,我从用户那里获取视频输入。该视频由我们的应用程序处理,在屏幕上显示检测并在完成该过程后下载视频。但是,当我在同一个应用程序上通过同一个会话传递同一个视频或另一个视频时,它就不起作用了。它没有向我显示任何错误。在这里,我附上了我的项目的示例代码。
完成此应用程序中第一个视频的流程后,如何传递另一个视频?
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)
这里我提到 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>
解决方案
两种可能:
- 一次上传多个文件
- 第一次上传完成后将用户重定向到上传表单
如果没有任何请求触发器,您将无法处理第二个文件。
问候,托马斯
推荐阅读
- css - Bootstrap 编译默认样式和自定义样式
- c# - TemplateColumn 按钮命令绑定在 Syncfusion DataGrid 中不起作用
- vue.js - 如何对 vue.js 进行多重过滤
- python - 时间数据“2020-07-09T00:00:00+05:30”与格式“yyyy-MM-dd'T'HH:mm:ssZ”不匹配
- r - R中的可变“类似列表的对象?
- php - 如何从未知数量的数组 PHP 中构建一个数组 [字符串]
- asp.net-mvc - 是否可以在 aspnetboilerplate 框架中通知特定的 connectionId?
- c# - 为每个命令停用语音识别,除了激活语音识别
- ios - 如何在不注册开发者计划的情况下获取 iOS 应用程序的大小?
- python - 将 .h5 (HDF5) 文件转换为 .zarr 格式时出现 TypeError