forked from miguelgrinberg/flask-video-streaming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
80 lines (66 loc) · 2.19 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
#!/usr/bin/env python3
from importlib import import_module
import os
from flask import Flask, render_template, Response, request
import RPi.GPIO as GPIO
# import camera driver
if os.environ.get('CAMERA'):
Camera = import_module('camera_' + os.environ['CAMERA']).Camera
else:
raise RuntimeError('No environment variable CAMERA defined!')
Led = import_module('led').Led
# Raspberry Pi camera module (requires picamera package)
app = Flask(__name__)
led = Led(21)
cam = Camera(led)
@app.route('/')
def index():
"""Video streaming home page."""
return render_template('index.html')
def gen(camera):
"""Video streaming generator function."""
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
def gen_snapshot(camera):
frame = camera.get_frame(True)
return frame
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
if(not led.leds_on()):
led.switch_leds(True)
return Response(gen(cam),
mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/snapshot')
def snapshot():
"""Video streaming route. Put this in the src attribute of an img tag."""
# if(not cam.leds_on):
# led.switch_leds(True)
return Response(gen_snapshot(cam),
mimetype='image/jpeg')
@app.route('/control_leds', methods=['POST'])
def control_leds():
"""Video streaming route. Put this in the src attribute of an img tag."""
json = request.json
led_state = json["state"]
if(led_state == 'on'):
led.switch_leds(True)
resp = 'true'
elif(led_state == 'off'):
led.switch_leds(False)
resp = 'false'
else:
return ('', 504)
return Response(f'{{"led_status": {resp}}}', mimetype="application/json")
@app.route('/leds_on')
def led_status():
status = led.status()
if(status == 1):
resp = 'true'
else:
resp = 'false'
return Response(f'{{"led_status": {resp}}}', mimetype="application/json")
# if __name__ == '__main__':
# app.run(host='0.0.0.0', threaded=True, port=3000)