1

So I've been trying to figure this out for a while now, without success. I am using a RPi Zero Rev 1.3 model and have connected it to the 8MP RPi camera along with a PIR motion sensor. What I'm trying to do is capture images at 2 second intervals (the first capture taking place 1 second after the PIR sensor has been triggered, and then capturing images every 2 seconds afterwards until no more motion has been detected. I can capture the first image (1 second after the PIR sensor has been triggered, and the video records successfully until motion stops, but I can't get it to capture any more still images after the first one.

My code is longer, but this is the relevant part for the sequence I'm trying to figure out.

#!/usr/bin/env python

from gpiozero import MotionSensor
from picamera import PiCamera
from datetime import datetime
import subprocess
import time
import os

camera = PiCamera()
pir = MotionSensor(4)
camera.resolution = (1920,1080)
camera.hflip = True
camera.vflip = True
camera.iso = 1600
camera.awb_mode = 'auto'
camera.exposure_mode = 'night'
vid_directory = '/home/pi/camsecurity_vids/'
pic_filepath = "/home/pi/camsecurity_stills/"

try:
    print 'PIR Motion Triggered Video Capture'
    time.sleep(10)
    print 'Ready'

    while True:
        pir.wait_for_motion()
        h264_filename = vid_directory + datetime.now().strftime("%m%d%Y_%H%M%S.h264")
        only_filename = datetime.now().strftime("%m%d%Y_%H%M%S.mp4")
        mp4_filename = vid_directory + only_filename
        pic_filename = datetime.now().strftime("%m%d%Y_%H%M%S.jpg")
        pic_path = pic_filepath + pic_filename
        camera.start_recording(h264_filename, quality=10)
        camera.wait_recording(1)
        camera.capture(pic_path, format='jpeg', quality=100, use_video_port=True)
        print "Captured %s" % pic_filename
        pir.wait_for_no_motion()
        camera.stop_recording()
        subprocess.call("MP4Box -fps 30 -add %s %s" % (h264_filename, mp4_filename), shell=True); os.remove(h264_filename)
        print "Recorded %s" % only_filename
except KeyboardInterrupt:
    print 'Quit'

I've tried various things like putting another camera.wait_recording after the camera.capture command, but nothing I try seems to work. I've successfully created a program that solely does interval photo capture triggered by the sensor, but I can't seem to figure out how to make it work when combining photo capture while capturing video. Do I need to create a nested WHILE loop in my code to make this work, or is there a simpler way to accomplish what I want?

Thanks in advance!

1 Answer 1

0

I think you're on the right track - perhaps just using the timeout parameter in the wait_for_no_motion function you can get what you're looking for

I would suggest changing this code:

pic_filename = datetime.now().strftime("%m%d%Y_%H%M%S.jpg")
pic_path = pic_filepath + pic_filename
camera.start_recording(h264_filename, quality=10)
camera.wait_recording(1)    
camera.capture(pic_path, format='jpeg', quality=100, use_video_port=True)
print "Captured %s" % pic_filename
pir.wait_for_no_motion()

To this - adding your new loop - and using a 2 second interval waiting for motion before taking the next picture

camera.start_recording(h264_filename, quality=10)
camera.wait_recording(1)    
while pir.motion_detected:
    pic_filename = datetime.now().strftime("%m%d%Y_%H%M%S.jpg")
    pic_path = pic_filepath + pic_filename
    camera.capture(pic_path, format='jpeg', quality=100, use_video_port=True)
    print "Captured %s" % pic_filename
    pir.wait_for_no_motion(2)        

Note how the file naming line is put into the loop

1
  • Good question including code and description of the problem, symptoms and ideal solution. Have a good day
    – dbmitch
    Nov 20, 2016 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.