-
Notifications
You must be signed in to change notification settings - Fork 2
/
flaskapp.py
92 lines (73 loc) · 3.63 KB
/
flaskapp.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
83
84
85
86
87
88
89
90
91
92
from flask import Flask, render_template, Response,jsonify,request,session
#FlaskForm--> it is required to receive input from the user
# Whether uploading a video file to our object detection model
from flask_wtf import FlaskForm
from wtforms import FileField, SubmitField,StringField,DecimalRangeField,IntegerRangeField
from werkzeug.utils import secure_filename
from wtforms.validators import InputRequired,NumberRange
import os
# Required to run the YOLOv8 model
import cv2
# YOLO_Video is the python file which contains the code for our object detection model
#Video Detection is the Function which performs Object Detection on Input Video
from YOLO_Video import video_detection
app = Flask(__name__)
app.config['SECRET_KEY'] = 'ayush'
app.config['UPLOAD_FOLDER'] = 'static/files'
#Use FlaskForm to get input video file from user
class UploadFileForm(FlaskForm):
#We store the uploaded video file path in the FileField in the variable file
#We have added validators to make sure the user inputs the video in the valid format and user does upload the
#video when prompted to do so
file = FileField("File",validators=[InputRequired()])
submit = SubmitField("Run")
def generate_frames(path_x = ''):
yolo_output = video_detection(path_x)
for detection_ in yolo_output:
ref,buffer=cv2.imencode('.jpg',detection_)
frame=buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame +b'\r\n')
def generate_frames_web(path_x):
yolo_output = video_detection(path_x)
for detection_ in yolo_output:
ref,buffer=cv2.imencode('.jpg',detection_)
frame=buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame +b'\r\n')
@app.route('/', methods=['GET','POST'])
@app.route('/home', methods=['GET','POST'])
def home():
session.clear()
return render_template('indexproject.html')
# Rendering the Webcam Rage
#Now lets make a Webcam page for the application
#Use 'app.route()' method, to render the Webcam page at "/webcam"
@app.route("/webcam", methods=['GET','POST'])
def webcam():
session.clear()
return render_template('ui.html')
@app.route('/FrontPage', methods=['GET','POST'])
def front():
# Upload File Form: Create an instance for the Upload File Form
form = UploadFileForm()
if form.validate_on_submit():
# Our uploaded video file path is saved here
file = form.file.data
file.save(os.path.join(os.path.abspath(os.path.dirname(__file__)), app.config['UPLOAD_FOLDER'],
secure_filename(file.filename))) # Then save the file
# Use session storage to save video file path
session['video_path'] = os.path.join(os.path.abspath(os.path.dirname(__file__)), app.config['UPLOAD_FOLDER'],
secure_filename(file.filename))
return render_template('videoprojectnew.html', form=form)
@app.route('/video')
def video():
#return Response(generate_frames(path_x='static/files/bikes.mp4'), mimetype='multipart/x-mixed-replace; boundary=frame')
return Response(generate_frames(path_x = session.get('video_path', None)),mimetype='multipart/x-mixed-replace; boundary=frame')
# To display the Output Video on Webcam page
@app.route('/webapp')
def webapp():
#return Response(generate_frames(path_x = session.get('video_path', None),conf_=round(float(session.get('conf_', None))/100,2)),mimetype='multipart/x-mixed-replace; boundary=frame')
return Response(generate_frames_web(path_x=0), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
app.run(debug=True)