-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflaskapp.py
158 lines (126 loc) · 5.17 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from flask import Flask, render_template, Response, session, request
#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
from werkzeug.utils import secure_filename
from wtforms.validators import InputRequired
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
from pymongo import MongoClient
from mongopass import mongopass
app = Flask(__name__)
client = MongoClient(mongopass)
db=client.crud
myCollection = db.myColl
collection = db.licence_plates
helmets = db.helmets
acc = db.accidents
app.config['SECRET_KEY'] = 'hackathon'
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('index.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('video.html', form=form)
@app.route('/video')
def video():
#return Response(generate_frames(path_x='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')
@app.route('/crud')
def insert_val():
return render_template('crud.html')
@app.route('/read')
def read():
cursor = myCollection.find()
'''for record in cursor:
name.append(record['name'])
print(record)'''
return render_template('response.html', res=cursor)
@app.route('/insert')
def insert():
name = request.args.get('name')
address = request.args.get('address')
myVal = {'name': name, 'address': address}
x = myCollection.insert_one(myVal)
return render_template('response.html', res=x)
@app.route('/delete')
def delete():
name = request.args.get('name')
myquery = {'name': name}
myCollection.delete_one(myquery)
x = "Record delete"
return render_template('response.html', res=x)
@app.route('/update')
def update():
name = request.args.get('name')
new_address = request.args.get('new_address')
myquery = {'name': name}
newvalues = {'$set': {'address': new_address}}
myCollection.update_one(myquery, newvalues)
x = "Record updated"
return render_template('response.html', res=x)
@app.route('/litter')
def litter():
cursor = collection.find()
return render_template('litter.html', res=cursor)
@app.route('/helmet')
def helmet():
cursor = helmets.find()
return render_template('no_helmet.html', res=cursor)
@app.route('/accidents')
def accidents():
cursor = acc.find()
return render_template('road_accidents.html', res=cursor)
if __name__ == "__main__":
app.run(debug=True)