-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
67 lines (54 loc) · 1.98 KB
/
main.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
from flask import Flask, render_template, Response
from gevent.pywsgi import WSGIServer
from threading import Thread
from camera import VideoCamera
from camera_streamer import CameraStream
from utils import delete_files
app = Flask(__name__)
app.debug = False
app.threaded = False
def security_system_app():
video_camera = None
streamer = None
def reset_camera(video_camera, streamer):
delete_files()
if video_camera:
video_camera.release()
video_camera = VideoCamera()
video_camera.initialize()
streamer = CameraStream(camera_source=video_camera)
return (video_camera, streamer,)
video_camera, streamer = reset_camera(video_camera, streamer)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/v1/record', methods=['GET'])
def record():
video_camera.record(True)
return Response('recording', status=200, mimetype='application/json')
@app.route('/api/v1/stop_recording', methods=['GET'])
def stop_recording():
video_camera.record(False)
return Response('not recording', status=200, mimetype='application/json')
@app.route('/api/v1/reset', methods=['GET'])
def reset():
reset_camera(video_camera, streamer)
return Response('reset', status=200, mimetype='application/json')
def gen(stream_handle):
while True:
response = (
b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' +
stream_handle.request_stream() +
b'\r\n\r\n'
)
yield response
@app.route('/video_feed')
def video_feed():
return Response(gen(streamer) or None,
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
"""Start a server that can handle async streaming"""
security_system_app()
server = WSGIServer(('192.168.1.184', 5000), app)
server.serve_forever()