-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflask_simple.py
72 lines (52 loc) · 1.69 KB
/
flask_simple.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
import threading
import cv2
import imutils
from flask import Flask, render_template, Response
from imutils.video import VideoStream
app = Flask(__name__)
vs = VideoStream(src=0).start()
lock = threading.Lock()
GLOBAL_FRAME = None
def change_frame_():
global GLOBAL_FRAME
while True:
GLOBAL_FRAME = vs.read()
GLOBAL_FRAME = imutils.resize(GLOBAL_FRAME, width=400)
def generate_():
global GLOBAL_FRAME
while True:
with lock:
if GLOBAL_FRAME is None:
continue
(flag, encodedImage) = cv2.imencode(".jpg", GLOBAL_FRAME)
if not flag:
continue
yield b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + bytearray(encodedImage) + b'\r\n'
# noinspection PyUnresolvedReferences
@app.route('/')
def index():
return render_template('index.html')
# noinspection PyUnresolvedReferences
@app.route('/home')
def home():
return render_template('index.html')
# noinspection PyUnresolvedReferences
@app.route('/about')
def about():
return render_template('about-us.html')
# noinspection PyUnresolvedReferences
@app.route('/contact')
def contact():
return render_template('blog.html')
@app.route('/video_feed')
def video_feed():
# return the response generated along with the specific media
# type (mime type)
return Response(generate_(),
mimetype="multipart/x-mixed-replace; boundary=frame")
if __name__ == '__main__':
# new lambda thread to read frames from the video stream
t = threading.Thread(target=change_frame_, args=())
t.daemon = True
t.start()
app.run(host='127.0.0.1', port=8000, debug=True, threaded=True, use_reloader=False)