hyperchai
Posts: 1
Joined: Fri Apr 17, 2020 1:11 am

Possible to snap photo while video is recording?

Sat Apr 18, 2020 12:30 am

Hi,

I am building a wildlife camera (referenced from this wonderful guide: https://peaknature.co.uk/blog/how-to-bu ... e-hardware) but instead of just recording video when detecting motion, I would like to also snap a photo (maybe 15 seconds in) that would tweet the photo or send it to my phone, for example.

Is it possible to do that while the recording is happening?

Here's an example of my code:

Code: Select all

 
 while True:
	 pir.wait_for_motion()
        logging.info('Motion detected')
        print('Motion detected')
        while pir.motion_detected:
            print('Beginning capture')
            ts = '{:%Y%m%d-%H%M%S}'.format(datetime.now())
            logging.info('Beginning capture: '+ str(ts)+'.h264')
            with picamera.PiCamera() as cam:
                cam.resolution=(1024,768)
                cam.annotate_background = picamera.Color('black')

                cam.start_recording('/home/pi/video.h264')
                start = datetime.now()
                while (datetime.now() - start).seconds < duration:
                    cam.annotate_text = datetime.now().strftime('%d-%m-%y %H:%M:%S')
                    cam.wait_recording(0.2)
                cam.stop_recording()
            time.sleep(1)
            print('Stopped recording')

EDIT: So I did some digging, and I read that it is possible, but I'm not quite sure where to place the code. I have it in the while loop here, right before "cam.stop_recording()":

Code: Select all

cam.start_recording('/home/pi/video.h264')
start = datetime.now()
while (datetime.now() - start).seconds < duration:
	cam.annotate_text = datetime.now().strftime('%m-%d-%Y_(%I-%M-%S_%p)')
	cam.wait_recording(0.2)
	camera.capture('image.jpg', use_video_port=True)
        cam.stop_recording()
                
Further questions: Is there a problem placing the camera.capture line there? I read that adding "use_video_port=True" will keep frames from dropping while its recording. Will this take one photo during this recording session? I was hoping to add in a part where it waits 15 seconds from beginning recording, take one photo, then wait til next recording session to execute the still camera capture again.

Return to “Python”