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!