forked from log0/video_streaming_with_flask_example
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
165 lines (127 loc) · 4.88 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
"""Flask server that serves frames"""
import os
import time
import logging
import io
from importlib import import_module
import threading
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
from flask import Flask, render_template, Response, jsonify, make_response, send_file, request
from flask_httpauth import HTTPBasicAuth
import cv2
import utils
Camera = import_module(
'camera.camera_' + os.environ.get('CAMERA', 'opencv')).Camera
from camera.rtsp_server import start_rtsp, RTSP_URL
log_formatter = logging.Formatter(
"%(asctime)s [ %(threadName)-12.12s ] [ %(levelname)-5.5s ] %(message)s")
root_logger = logging.getLogger()
file_handler = logging.FileHandler("warn.log")
file_handler.setFormatter(log_formatter)
root_logger.addHandler(file_handler)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(log_formatter)
root_logger.addHandler(console_handler)
root_logger.setLevel(logging.DEBUG)
logging.getLogger('apscheduler').setLevel(logging.DEBUG)
app = Flask(__name__)
app.use_reloader = False
THROTTLE_SECONDS = int(os.environ.get('THROTTLE_SECONDS', 5))
MAX_IO_RETRIES = int(os.environ.get('MAX_IO_RETRIES', 1))
# get this from https://doorman.printdebug.com and keep it safe! it's how reports are verified and sent upstream
UPSTREAM_SECRET_KEY = os.environ['UPSTREAM_SECRET_KEY']
auth = HTTPBasicAuth()
# users allowed access in your system
USER_DATA = {
os.environ['STREAM_ROOT_USERNAME']: os.environ['STREAM_ROOT_PASSWORD'],
os.environ['STREAM_API_USERNAME']: os.environ['STREAM_API_PASSWORD']
}
@auth.verify_password
def verify(username, password):
if not (username and password):
return False
return USER_DATA.get(username) == password
@app.route('/live')
@auth.login_required
def index():
return render_template('index.html')
@app.route('/')
@auth.login_required
def ping():
camera = Camera()
return jsonify({
'camera': camera.get_frame() is not None
})
def gen(camera):
while True:
_, frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame.tostring() + b'\r\n\r\n')
@app.route('/video_feed')
@auth.login_required
def video_feed():
return Response(gen(Camera()),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/frame')
@auth.login_required
def get_frame():
return send_file(io.BytesIO(Camera().get_frame()[1].tostring()), mimetype='image/jpeg')
def read_and_process(camera):
image, jpg = camera.get_frame()
detections = utils.check_detect(jpg)
return detections, image, jpg
@app.route('/process')
@auth.login_required
def process_single_frame():
try:
camera = Camera()
detections, _, _ = read_and_process(camera)
return jsonify(detections)
except IOError as e:
root_logger.error(str(e))
return make_response(jsonify({'status': 'error', 'message': 'failed to read camera', 'error': str(e)}), 500)
@app.route('/stream-detect')
@auth.login_required
def detect():
# http://flask.pocoo.org/docs/0.12/patterns/streaming/
def generate_detections():
root_logger.info('Beginning to read and process frames')
while True:
detections, image, jpg = read_and_process(Camera())
if detections['results']:
root_logger.info('Detected objects, altering frame')
for boxes in detections['results']:
image = utils.draw_boxes(image, boxes)
_, jpg = cv2.imencode('.jpg', image)
else:
root_logger.info(
'No objects recognized, passing original back frame')
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + jpg.tostring() + b'\r\n\r\n')
if os.environ.get('THROTTLE_SERVER', False):
time.sleep(THROTTLE_SECONDS)
return Response(generate_detections(),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/verify-key')
@auth.login_required
def verify_upstream_key():
body = request.get_json()
if body['UPSTREAM_REPORT_KEY'] != UPSTREAM_SECRET_KEY:
return make_response(jsonify({"status": "error", "message": "invalid upstream secret key"}), 401)
else:
return jsonify({"status": "success"})
if __name__ == '__main__':
root_logger.info('Starting Flask server thread')
root_logger.info('Starting RTSP thread, hosted on %s', RTSP_URL)
threading.Thread(target=lambda: app.run(
host='0.0.0.0',
debug=os.environ.get('DEBUG') == 'True',
use_reloader=False)).start()
threading.Thread(target=lambda: start_rtsp(Camera),
daemon=True).start()
threading.Thread(target=utils.start_motion_tracker,
daemon=True).start()