-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
61 lines (48 loc) · 1.63 KB
/
server.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
import threading, time, cv2, threading, logging, sys, traceback, os, json, colorama
from flask import Response, Flask, render_template
colorama.init()
logger = logging.getLogger('logger')
fh = logging.FileHandler(os.path.join(os.path.dirname(__file__), "logs\w_server.log"))
logger.addHandler(fh)
def exc_handler(exctype, value, tb):
logger.exception(''.join(traceback.format_exception(exctype, value, tb)))
sys.excepthook = exc_handler
with open(os.path.join(os.path.dirname(__file__), "config.json"), "r") as conf_file:
config = json.load(conf_file)
cam_n = config["camera"]["v_cam"]
outputFrame = None
lock = threading.Lock()
cap = cv2.VideoCapture(cam_n)
app = Flask(__name__)
time.sleep(2.0)
@app.route("/")
def index():
return render_template( "index.html")
def getframe():
global outputFrame, lock
while True:
_, img = cap.read()
with lock:
outputFrame = img
def generate():
global outputFrame, lock
while True:
with lock:
if outputFrame is None:
continue
(flag, encodedImage) = cv2.imencode(".jpg", outputFrame)
if not flag:
continue
yield(b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' +
bytearray(encodedImage) + b'\r\n')
@app.route("/video_feed")
def video_feed():
return Response(generate(),
mimetype = "multipart/x-mixed-replace; boundary=frame")
if __name__ == '__main__':
t = threading.Thread(target=getframe)
t.daemon = True
t.start()
app.run(host="0.0.0.0", port=8040, debug=True,
threaded=True, use_reloader=False)
cv2.destroyAllWindows()