Taking Pictures

First, we need some pictures in order to create our video.
To do that, we will use Open CV and a web cam connected to Raspberry Pi.

Photo Taker

After settup the Rapsberry Pi and the camera, we should create a script to take pictures at some interval of time.

import cv2
import time

# Initiate the VideoCapture with the number of the camera
# In our case: /dev/video0 -> camera = 0
camera = cv2.VideoCapture(0)

# Get an image from the camera
# the Read function return a tuple
# First value says with the image was successfully read
# Second value is the image we want
ret,frame = camera.read()

# Lets make sure that the image is good
while ret == False:
	ret,frame = camera.read()

# Save the image into a file
filePath = "./photos/%d.png" % int(time.time())
cv2.imwrite(filePath,frame)

# Realease the camera
del(camera)

Add the script to crontab. You can decide the interval of your photos.

# To edit the contrab
crontab -e

# The script will be called every 5 minutes
*/5 * * * * /usr/bin/python /path/to/script/photoTaker.py

Video Generator

Now it is time to put all photos together in a video.

import numpy
from PIL import Image
import cv2
import os

# Define the size of the video
# Must be the same size of the photos
width,height = (640, 480)

# Define the codec
fourcc = cv2.cv.CV_FOURCC('P','I','M','1')
# Create the video pointer
# Params  (file_name,code,frames_per_second,size)
video = cv2.VideoWriter("timelapse.avi",fourcc,20,(width,height))

# List all photos 
for i in os.listdir("photos"):
	# Open the file
	im1 = Image.open("photos/"+i)
	# Convert to NumPY array
	frame = numpy.array(im1)

	# Add into the video
	video.write(frame)

# Release the video
video.release()

Result

A timelapse of a autumn day on Arlington, VA.

Downloads and more

Links to raw files:

References