-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.py
80 lines (65 loc) · 2.12 KB
/
record.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 python
import json
import os
import RPi.GPIO as GPIO
from time import sleep
from picamera import PiCamera
from datetime import datetime
def get_filename():
global config
return config['save_path'] % datetime.now().strftime('%Y%m%d-%H%M%S')
def pir_triggered(channel):
global config, lastSeen, recording, camera
lastSeen = datetime.now()
if not recording:
print "Starting up the recording"
recording = True
path = get_filename()
camera.start_recording(path)
camera.wait_recording(config['record_seconds'])
with open(os.path.dirname(os.path.realpath(__file__)) + '/config.json', 'r') as f:
config = json.load(f)
GPIO.setmode(GPIO.BCM)
GPIO.setup(config['pir_pin'], GPIO.IN)
camera = PiCamera()
camera.resolution = (1280, 720)
camera.led = False
recording = False
lastSeen = datetime.now()
GPIO.add_event_detect(config['pir_pin'], GPIO.RISING, callback=pir_triggered)
if config['delay_start_seconds']:
if config['debug_output']:
print "Starting in %d seconds" % config['delay_start_seconds']
sleep(config['delay_start_seconds'])
if config['debug_output']:
print "Go!"
try:
while True:
if recording:
if config['debug_output']:
print "Currently recording"
delta = datetime.now() - lastSeen
if delta.seconds > config['record_seconds']:
if config['debug_output']:
print "Stopping the video"
camera.stop_recording()
recording = False
else:
if config['debug_output']:
print "Still movement, waiting some more"
camera.wait_recording(config['record_seconds'])
else:
if config['debug_output']:
print "Not currently recording"
sleep(config['sleep_seconds'])
except KeyboardInterrupt:
if recording:
recording = False
camera.wait_recording(0)
camera.stop_recording()
GPIO.cleanup()
if recording:
recording = False
camera.wait_recording(0)
camera.stop_recording()
GPIO.cleanup()