Use Raspberry Pi Camera Module for Time-Lapse Video

On Pi day, I bought a Raspberry Pi Camera Module V2 from Amazon so I could try out Amazon RekognitionGoogle Video Intelligence API, and many other fun projects that utilize the camera. I finally opened the camera package yesterday and started playing with it. The first project was a simple one: take many still photos and create a time-lapse video. Here are the steps:

  1. Install the camera module.
  2. Set up the Pi and put the camera facing the window.
  3. Write the following Python program:
from picamera import PiCamera
import time

# Configs
SUNRISE = 6
SUNSET = 20
LOCAL_TIME_UTC_OFFSET = -7

SAVE_PATH = "/home/pi/Projects/PiCam/photos/"
TIME_GAP_IN_SECONDS = 300

def isDayTime():
  hour = int(time.strftime("%H")) + LOCAL_TIME_UTC_OFFSET
  hour_local = hour
  if hour < 0:
hour_local = hour + 24
return (hour_local >= SUNRISE and hour_local <= SUNSET)

def start():
  camera = PiCamera()
  camera.start_preview()

  while True:
    time.sleep(TIME_GAP_IN_SECONDS)
    if isDayTime():
      now = time.strftime("%m%d%Y-%H%M")
      file_path = SAVE_PATH + now + ".jpg"
      camera.capture(file_path)
      print("Captured: " + file_path)
    else:
      print("Do not capture")
      camera.stop_preview()

print("Program starts...")
start()

Then just let it run in the background. Today, after sunset, I stopped the program and copied all the photos over to my Macbook, and used iMovie to create a time-lapse video.

That’s it! More camera projects to come!

Advertisements