Raspberry Pi Camera Setup for Time-Lapse

From Wurst-Wasser.net
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This article describes the setup of the Raspberry Pi Camera to create a time-lapse movie. If you're looking for a way to set up motion detection, see Raspberry Pi Camera Setup - Motion detected movie creation

Setup

Get package list

apt-get update

Update all packages

apt-get upgrade

Enable the camera

raspi-config

Raspicam01.png
Then answer the following stupid questions ("really enable?" and "reboot now?"), and you're done.

Configuration

Since using motion was some hazzle I decied to try a KISS-approach, using only the raspistill and convert[1] to implement.

Make the Pi take pictures

Make PiCam Device accessible

Since /usr/lib/cgi-bin/ is the default CGI-directory...

  • make sure, everyone can access the camera device[2]:
chmod o+rwx /dev/vchiq 

Setup script for taking a picture

  • create this script named takePicture.sh:
#!/bin/bash
#
# Globals
#
GFOLDERTARGET="/var/www/webcam-archive"
GFILELOCK="${GFOLDERTARGET}/takePicture.lck"
GFILELOCKAGEMAX="60"
GTIMESTAMP="`date +'%Y-%m-%d_%H-%M-%S'`"
GFILETARGET="${GFOLDERTARGET}/${GTIMESTAMP}.jpg"
GFILELASTSNAP="/var/www/webcam/lastsnap.jpg"
#
#
#
# Main
#
# Check locking
if [ -f "${GFILELOCK}" ]; then
  LOCKFILEAGE="`stat --format=\"%Z\" \"${GFILELOCK}\"`"
  NOW="`date +'%s'`"
  AGE="`expr ${NOW} - ${LOCKFILEAGE}`"
  if [ "${AGE}" -gt "${GFILELOCKAGEMAX}" ]; then
     echo "Lockfile is ${AGE} seconds old, removing it."
     rm "${GFILELOCK}"
  else
     echo "Lockfile is ${AGE} seconds old, exiting."
     exit 1
  fi
fi
#
#
# Lock
echo $$ > "${GFILELOCK}"
#
# timeout should not be 0, since images will be to dark and are noisy
# Since my cam is mounted wrong, I prefer to flip it :) (and flop it)
raspistill --nopreview -w 1024 -h 768 -o - | convert - -flip -flop - > "${GFOLDERTARGET}/lastsnap.tmp"
mv "${GFOLDERTARGET}/lastsnap.tmp" "${GFILETARGET}"
cp "${GFILETARGET}" "${GFILELASTSNAP}"
#
# Unlock
rm "${GFILELOCK}"
#
# Exit
exit 0
  • create a folder named webcam and webcam-archive:
cd /var/www
mkdir webcam
chown pi webcam-
mkdir webcam-archive
chown pi webcam-archive

Setup script for taking pictures

  • create this script named takePictures.sh[3]:
#!/bin/bash
#
# Globals
GSNAPSHOTSPERMINUTE=4
GSNAPSHOTINTERVAL="`expr 60 / 4`"
#
# Main
for ((i=1; i<=${GSNAPSHOTSPERMINUTE}; i++)) 
do
  TIMEBEFORESNAPSHOT="`date +'%s'`"
  echo "Taking snapshot at `date`"
  /home/pi/Development/motion_webcam/takePicture.sh
  TIMEAFTERSNAPSHOT="`date +'%s'`"
  DURATIONOFSNAPSHOT="`expr ${TIMEAFTERSNAPSHOT} - ${TIMEBEFORESNAPSHOT}`"
  if [ "${DURATIONOFSNAPSHOT}" -lt "${GSNAPSHOTINTERVAL}" ]; then
     DELAY="`expr ${GSNAPSHOTINTERVAL} - ${DURATIONOFSNAPSHOT}`"
     echo "Sleeping for ${DELAY} seconds."
     sleep ${DELAY}
  fi
done
  • Add this to pi's crontab:
# PiCam - Take Snapshots
* * * * *       /home/pi/Development/motion_webcam/takePictures.sh > /dev/null 2>&1

Create a time-lapse movie

  • Create a new script mergeAllPicturesInOneAnimatedGIF.sh:
#!/bin/bash
# 
# Script: mergeAllPicturesInOneAnimatedGIF.sh
# Author: Heiko Kretschmer
# Purpose: Merge all .jpg into one animated .gif
#
# History:
# 2014-03-30,	09:52, h:	Creation. Note. default ticks in -delay is n/100 of a second
#

# Globals
#
GFOLDERSOURCE="/var/www/webcam-archive"
GSUFFIXSOURCE="jpg"
GTIMESTAMP="`date +'%Y-%m-%d'`"
GFILETARGET="/var/www/webcam-archive/${GTIMESTAMP}.gif"

# (Un)Main
#
convert -limit area 0 -delay 25 -loop 1 "${GFOLDERSOURCE}/*.${GSUFFIXSOURCE}" "${GFILETARGET}"
  • Add a crontab entry:
# PiCam - Merge Images to time-lapse-movie
0 0 * * *       /home/pi/Development/motion_webcam/mergeAllPicturesInOneAnimatedGIF.sh > /dev/null 2>&1

Check if it works

How do the scripts work?

  • The takePictures.sh script will be fired every minute and will take, launching takePicture.sh a picture avery 15 seconds (4 per minute)
  • The mergeAllPicturesInOneAnimatedGIF.sh merges once a day all snapshots into one animated .gif

Safety precautions

  • Carefully watch your disk, this process could easily eat up all your free space, especially check /tmp, where imagemagick will create temporary files!
  • Keep in mind, creating so much pictures (if you follow above instructions, you will create 1440*4=5760!) will fill your disk really fast! Taken with full resolution, a picture weighs around 600K, adding up to 3.5GB a day! (24*60*4*0.6M=3.456GB)!
    You can reduce the storage used by...:
    • …changing GSNAPSHOTINTERVAL in takePictures.sh
    • …changing the quality and resolution in takePicture.sh
    • …changing your crontab to take only one image per minute:
      * * * * * /home/pi/Development/webcam/takePicture.sh > /dev/null 2>&1
      This will result in 1440*0.6MB=864MB per day (yes, our 8GB SD Card is full after one week...)
    • …executing mergeAllPicturesInOneAnimatedGIF.sh on your Mac instead of on your Pi, since it draws a LOT of CPU-power and eats up a LOT of free disk space or try How to create time-lapse movies from JPEG stills.



  1. Which is part of imagemagick
  2. including web users
  3. Execution of /home/pi/Development/motion_webcam/takePicture.sh takes approx. 7 seconds!