-
Notifications
You must be signed in to change notification settings - Fork 0
/
motionvid.py
executable file
·101 lines (93 loc) · 3.31 KB
/
motionvid.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
import ConfigParser
import io
import random
import picamera
import RPi.GPIO as GPIO
import time
import os
print "starting motionvid"
config = ConfigParser.RawConfigParser()
## uncomment this to create the config file
#config.add_section('Paths')
#config.set('Paths','video_directory','/motionimg/')
#config.add_section('Settings')
#config.set('Settings','MotionPin','7')
#config.set('Settings','VideoWidth','1640')
#config.set('Settings','VideoHeight','1232')
#config.set('Settings','VideoFileName','%Y%m%d%H%M%S.h264')
#config.set('Settings','VideoFormat','h264')
#with open('motionvid.conf','wb') as configfile:
# config.write(configfile)
config.read('/home/pi/motioncam/motionvid.conf')
video_directory = config.get('Paths','video_directory')
MotionPin = int(config.get('Settings','MotionPin'))
VideoWidth = int(config.get('Settings','VideoWidth'))
VideoHeight = int(config.get('Settings','VideoHeight'))
VideoFileName = config.get('Settings','VideoFileName')
VideoFormat = config.get('Settings','VideoFormat')
videobufferlength = int(config.get('Settings','videobufferlength'))
VideoLength = int(config.get('Settings','VideoLength'))
AllowRetrigger = (config.get('Settings','AllowRetrigger') == 'yes')
vFlip = (config.get('Settings','vFlip'))
hFlip = (config.get('Settings','hFlip'))
expMode = (config.get('Settings','expMode'))
lightsOn = (config.get('Settings','lightsOn'))
lightsOnCmd = (config.get('Settings','lightsOnCmd'))
print "video_directory = ",video_directory
print "MotionPin = ",MotionPin
print "VideoWidth = ",VideoWidth
print "VideoHeight = ",VideoHeight
print "VideoFileName = ",VideoFileName
print "VideoFormat = ",VideoFormat
print "videobufferlength = ",videobufferlength
print "VideoLength = ",VideoLength
print "AllowRetrigger = ",AllowRetrigger
print "vFlip = ",vFlip
print "hFlip = ",hFlip
print "expMode = ",expMode
print "lightsOn = ",lightsOn
print "lightsOnCmd = ",lightsOnCmd
GPIO.setmode(GPIO.BOARD)
GPIO.setup(MotionPin, GPIO.IN) #Read output from PIR motion sensor
imgWidth = VideoWidth # Max = 2592
imgHeight = VideoHeight
motiondetbef = False # Max = 1944
def motion_detected():
# return random.randint(0, 10) == 0
global motiondetbef
global AllowRetrigger
i=GPIO.input(7)
if i==1:
if motiondetbef == False:
if AllowRetrigger == False:
motiondetbef = True
return True
else:
motiondetbef = False
return False
camera = picamera.PiCamera()
camera.resolution = (1640,1232)
if(vFlip == 'yes'):
camera.vflip = True
if(hFlip == 'yes'):
camera.hflip = True
camera.exposure_mode = expMode
stream = picamera.PiCameraCircularIO(camera, seconds=videobufferlength)
camera.start_recording(stream, format=VideoFormat)
try:
while True:
camera.wait_recording(1)
if motion_detected():
if(lightsOn == 'yes'):
os.system(lightsOnCmd)
print "Capturing"
# Keep recording for 10 seconds and only then write the
# stream to disk
camera.wait_recording(VideoLength)
fname=time.strftime(video_directory + VideoFileName)
print 'Saving'
stream.copy_to(fname)
print "Done saving"
finally:
camera.stop_recording()