-
Notifications
You must be signed in to change notification settings - Fork 0
/
babymonitor.py
75 lines (59 loc) · 1.97 KB
/
babymonitor.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
from flask import Flask, render_template, Response, request
from camera import VideoCamera
from temperature import TemperatureProbe
from audio import Microphone
import pyaudio
from datetime import datetime
# create the camera, temp and microphone
camera = VideoCamera()
temp_probe = TemperatureProbe()
microphone = Microphone()
app = Flask(__name__)
# return index.html from templates
@app.route('/')
def index():
return render_template('index.html')
# get the video frames for index.html
@app.route('/video_feed')
def video_feed():
return Response(gen_cam(camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
# get the time for index.html
@app.route('/time_feed')
def time_feed():
return Response(gen_time(), mimetype='text')
# get a temperature for index.html
@app.route('/temp_feed')
def temp_feed():
return Response(gen_temp(temp_probe), mimetype='text')
# get an audio chunk for index.html
@app.route('/audio_feed')
def audio_feed():
return Response(gen_audio(microphone), mimetype='audio/x-wav')
# always get the next frame from the camera
def gen_cam(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
# get the time when requested. set to 1000ms in html.index
def gen_time():
yield datetime.now().strftime("%H:%M:%S<br>%d.%m.%Y")
# get the temperature when requested. set to 1000ms in html.index
def gen_temp(temp_probe):
yield temp_probe.read_temp()
# always get the next chunk of audio
def gen_audio(microphone):
data = microphone.get_wav_header()
first_chunk = True
# if it is the first chunk add a header to it.
while True:
if first_chunk:
data += microphone.get_audio()
first_chunk = False
else:
data = microphone.get_audio()
yield (data)
# run the flask app automatically
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False)