-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
82 lines (66 loc) · 2.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from flask import Flask, render_template, request, Response, flash, send_file
from werkzeug.utils import secure_filename
from openCv import VideoCamera
import pandas as pd
import os
app = Flask(__name__)
app.secret_key = 'omen'
app.config['UPLOAD_FOLDER'] = 'static/uploads/'
auth = False
def generate(video):
try:
while True:
frame = video.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
except Exception as e:
print(f'[generate ERROR]: {e}')
@app.route('/', methods=['GET'])
def index():
try:
file_name = [f for f in os.listdir(
'static/uploads/') if os.path.isfile(os.path.join('static/uploads/', f))]
for i in file_name:
os.remove('static/uploads/' + i)
with open('output.csv', 'w') as f:
pass
except:
pass
return render_template('index.html')
@app.route('/csv', methods=['GET', 'POST'])
def csv():
table = pd.read_csv('output.csv', encoding='unicode_escape')
return render_template('count.html', data=table.to_html())
@app.route('/', methods=['POST'])
def upload_video():
video = request.files['video']
video.save(os.path.join(
app.config['UPLOAD_FOLDER'], secure_filename(video.filename)))
file_name = [f for f in os.listdir(
'static/uploads/') if os.path.isfile(os.path.join('static/uploads/', f))]
return render_template('upload.html', file_name=file_name[0])
@app.route('/download')
def download():
global auth
if auth == True:
auth = False
return send_file('output.csv', as_attachment=True)
else:
flash('First detect the vehicle')
return render_template('error.html')
@app.route('/cv', methods=['GET'])
def cv():
global auth
auth = True
try:
return Response(generate(VideoCamera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
except Exception as e:
auth = False
if str(e) == 'list index out of range':
flash(f'Please upload a video file')
else:
flash(f'[ERROR]: {e}')
return render_template('error.html')
if __name__ == '__main__':
app.run(debug=True)