-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
148 lines (91 loc) · 2.96 KB
/
main.py
File metadata and controls
148 lines (91 loc) · 2.96 KB
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
from flask import Flask, render_template, request, Response
import pir_motion_camera
from picamera import PiCamera
from gpiozero import MotionSensor
from threading import Condition
import io
camera = PiCamera()
PATH_IMAGES = "/home/pi/MotionCamera/images/"
app = Flask(__name__)
pir_motion_camera = pir_motion_camera.pir_motion_camera(camera, PATH_IMAGES)
pir = MotionSensor(4)
camera_state = "Off"
def camera_status(status):
global camera_state
camera_state = status
if status == "On":
pir.when_motion = pir_motion_camera.capture_image
elif status == 'Off':
pir.when_motion = None
elif status == 'Image':
pir.when_motion = pir_motion_camera.capture_image
elif status == 'Video':
try:
camera.stop_recording()
except:
pass
pir.when_motion = pir_motion_camera.capture_video
@app.route("/")
def index():
templateData = {
'camerastatus' : camera_state,
'videolength': pir_motion_camera.video_lenght,
'sleeptimer' : pir_motion_camera.sleeptimer
}
##ei toimi template
return render_template('index.html', **templateData)
@app.route("/<controll>/<action>")
def serverControll(controll, action):
if controll == 'camera' or controll == 'mode':
camera_status(action)
return index()
@app.route("/videolength/")
def video_lenght_controll():
length = request.args.get('length')
print(length)
pir_motion_camera.video_lenght = (int)(length)
return index()
@app.route("/sleeptimer/")
def sleeptime_controll():
length = request.args.get('length')
pir_motion_camera.sleeptimer = (int)(length)
return index()
def gen(camera):
#get camera frame
try:
camera.stop_recording()
except:
pass
camera.start_recording(output, format='mjpeg')
while True:
with output.condition:
output.condition.wait()
frame = output.frame
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/liveView')
def video_feed():
camera_status("Off")
return Response(gen(camera),
mimetype='multipart/x-mixed-replace; boundary=frame')
class StreamingOutput(object):
def __init__(self):
self.frame = None
self.buffer = io.BytesIO()
self.condition = Condition()
def write(self, buf):
if buf.startswith(b'\xff\xd8'):
# New frame, copy the existing buffer's content and notify all
# clients it's available
self.buffer.truncate()
with self.condition:
self.frame = self.buffer.getvalue()
self.condition.notify_all()
self.buffer.seek(0)
return self.buffer.write(buf)
if __name__ == "__main__":
output = StreamingOutput()
try:
app.run(host="0.0.0.0")
finally:
camera.close()