-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
195 lines (162 loc) · 7.24 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from flask import Flask,render_template,Response, session, request, redirect, url_for
import cv2
import os
from Inference_for_obj_counting import tensor_logic
from utils.connector import mysql_connect as mysql
import winsound
import numpy as np
app = Flask(__name__)
app.secret_key = "super secret keys"
threshold = 0.6
dict_names = {
0:"person",
6:"train",
9:"traffic_light",
16:"dog",
17:"horse",
18:"sheep",
19:"cow"
}
#input_video = 0
input_video = os.path.join("static","videos","old_lady_train_Trim.mp4")
# input_video = os.path.join("data","old_lady_train.mp4")
cap = cv2.VideoCapture(input_video)
# this is because frame rate is changing
area_of_interest = [[(288,566), (288,395), (1900,545), (1900,1068)]]
new_area_of_interest = [[(288//2,566//2), (288//2,395//2), (1900//2,545//2), (1900//2,1068//2)]]
def generate_frames():
count = 0
while True:
count += 1
if count % 7 != 0: # skip frames condition
continue
## read the camera frame
ret, frame = cap.read()
if not ret:
break
# draw the boundaries for railway route
cv2.polylines(frame, np.array(area_of_interest, np.int32), True, (15, 220, 10), 6)
# drop the unwanted part of the frame
# frame = frame[:, 285:cols]
height, width, _ = frame.shape
# resize the frame
frame = cv2.resize(frame, (width // 2, height // 2))
height, width, _ = frame.shape
# out_boxes -> coordinates
# out_scores -> scores
# out_classes -> classes
# num_boxes -> number of predictions
out_boxes, out_scores, out_classes, num_boxes = list(tensor_logic.detect_box(frame))
# "detections" will store the detection coordinates for every object detected in the ongoing frame
detections = []
result=-1
for pred in range(num_boxes[0]):
if int(out_classes[0][pred]) in [0, 6, 9, 16, 17, 18, 19]:
if out_scores[0][pred] > threshold:
xmin = int(out_boxes[0][pred][1] * width)
ymin = int(out_boxes[0][pred][0] * height)
xmax = int(out_boxes[0][pred][3] * width)
ymax = int(out_boxes[0][pred][2] * height)
class_id = int(out_classes[0][pred])
detections.append([xmin, ymin, xmax, ymax, class_id])
# print(class_id, out_scores[0][pred])
center = (int((xmin + xmax) / 2), int((ymax + ymax) / 2))
result = cv2.pointPolygonTest(np.array(new_area_of_interest, np.int32), center,
False) # false as we do not want the distance bet poly and obj
# print("*"*20)
# print(result)
if result >= 0: # draw box in red color
# draw the bounding boxes for all the detections
rect_pt_1 = (xmin, ymin)
rect_pt_2 = (xmax, ymax)
cv2.rectangle(frame,
rect_pt_1, rect_pt_2,
color=(0, 0, 255), thickness=2)
# draw a centroid
cv2.circle(frame, center,
radius=2, color=(255, 0, 0), thickness=5)
# put text
cv2.putText(frame,
dict_names[class_id],
(xmin + 5, ymin - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 0), 1,
lineType=cv2.LINE_AA)
# ALERT THE TRESSPASSING WITH SOUND
winsound.Beep(500, 100)
# winsound.PlaySound('alert.wav', winsound.SND_ASYNC)
else: # draw the box in green color
# draw the bounding boxes for all the detections
rect_pt_1 = (xmin, ymin)
rect_pt_2 = (xmax, ymax)
cv2.rectangle(frame,
rect_pt_1, rect_pt_2,
color=(255, 0, 0), thickness=2)
# draw a centroid
cv2.circle(frame, center,
radius=2, color=(0, 0, 255), thickness=5)
# put text
cv2.putText(frame,
dict_names[class_id],
(xmin + 5, ymin - 5),
cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 0), 1,
lineType=cv2.LINE_AA)
# show image
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
# result
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
@app.route("/")
def index():
return render_template("index.html")
@app.route('/multi_screen', methods=['GET','POST'])
def multi_screen():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
# Checking the above credentials with mysql db
db = mysql.connect()
cursor = db.cursor()
cursor.execute('SELECT * FROM user WHERE username=%s AND password=%s', (username, password))
record = cursor.fetchone()
db.close()
img_1 = os.path.join('static', 'images', '1.jpeg')
# img_2 = this we are taking as video file
img_3 = os.path.join('static', 'images', '2.jpeg')
img_4 = os.path.join('static', 'images', '1.jpeg')
img_5 = os.path.join('static', 'images', '2.jpeg')
img_6 = os.path.join('static', 'images', '1.jpeg')
img = [img_1, img_3, img_4, img_5, img_6]
if record:
session['loggedin'] = True
session['username'] = record[1]
return render_template('multi_screen.html', img = img)
else:
return render_template('index.html')
return render_template('index.html')
@app.route('/video')
def video():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/multi_screen_redirected', methods=['GET','POST'])
def multi_screen_redirected():
img_1 = os.path.join('static', 'images', '1.jpeg')
# img_2 = this we are taking as video file
img_3 = os.path.join('static', 'images', '2.jpeg')
img_4 = os.path.join('static', 'images', '1.jpeg')
img_5 = os.path.join('static', 'images', '2.jpeg')
img_6 = os.path.join('static', 'images', '1.jpeg')
img = [img_1, img_3, img_4, img_5, img_6]
return render_template('multi_screen.html', img = img)
@app.route('/big_screen')
def big_screen():
return render_template('big_screen.html')
@app.route("/logout")
def logout():
session.pop('loggedin',None)
session.pop('username', None)
# On logout clear the session and redirect back to login
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug=True)