-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
94 lines (81 loc) · 4.07 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
from flask import Flask,session,render_template,request,redirect,g,url_for,Response
import os
import time
import cv2
app= Flask(__name__)
sub = cv2.createBackgroundSubtractorMOG2() # create background subtractor
app.secret_key = os.urandom(24)
@app.route('/',methods=['GET','POST'])
def index():
if request.method == 'POST':
session.pop('user',None)
if request.form['password'] =='password':
session['user'] = request.form['username']
return redirect(url_for('portected'))
return render_template('index.html')
@app.route('/profile')
def portected():
if g.user == 'fireman':
return render_template('protected.html',user=session['user'])
return redirect(url_for('index'))
def gen():
"""Video streaming generator function."""
cap = cv2.VideoCapture(0)
# Read until video is completed
while(cap.isOpened()):
ret, frame = cap.read() # import image
if not ret: #if vid finish repeat
frame = cv2.VideoCapture(0)
continue
if ret: # if there is a frame continue with code
image = cv2.resize(frame, (0, 0), None, 1, 1) # resize image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # converts image to gray
fgmask = sub.apply(gray) # uses the background subtraction
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5)) # kernel to apply to the morphology
closing = cv2.morphologyEx(fgmask, cv2.MORPH_CLOSE, kernel)
opening = cv2.morphologyEx(closing, cv2.MORPH_OPEN, kernel)
dilation = cv2.dilate(opening, kernel)
retvalbin, bins = cv2.threshold(dilation, 220, 255, cv2.THRESH_BINARY) # removes the shadows
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
minarea = 1000
maxarea = 50000
for i in range(len(contours)): # cycles through all contours in current frame
if hierarchy[0, i, 3] == -1: # using hierarchy to only count parent contours (contours not within others)
area = cv2.contourArea(contours[i]) # area of contour
if minarea < area < maxarea: # area threshold for contour
# calculating centroids of contours
cnt = contours[i]
M = cv2.moments(cnt)
cx = int(M['m10'] / M['m00'])
cy = int(M['m01'] / M['m00'])
# gets bounding points of contour to create rectangle
# x,y is top left corner and w,h is width and height
x, y, w, h = cv2.boundingRect(cnt)
# creates a rectangle around contour
cv2.rectangle(image, (x, y), (x + w, y + h), (255, 0, 0), 2)
# Prints centroid text in order to double check later on
cv2.putText(image, str(cx) + "," + str(cy), (cx + 10, cy + 10), cv2.FONT_HERSHEY_SIMPLEX,.3, (0, 0, 255), 1)
cv2.drawMarker(image, (cx, cy), (0, 255, 255), cv2.MARKER_CROSS, markerSize=8, thickness=3,line_type=cv2.LINE_8)
#cv2.imshow("countours", image)
frame = cv2.imencode('.jpg', image)[1].tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
#time.sleep(0.1)
key = cv2.waitKey(20)
if key == 27:
break
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.before_request
def before_request():
g.user = None
if 'user' in session:
g.user = session['user']
@app.route('/dropsession')
def dropsession():
session.pop('user',None)
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')