Monday, 27 May 2013

Time Lapse Video with Raspberry Pi Camera

The first thing I wanted to try with the camera board for the Raspberry Pi was a time lapse video, they really appeal to me, and because of the size of both the Pi and its camera I thought I could attach it to my car nearly at road level, take a drive at dusk and get a total different perspective.

Time lapse videos are made by joining lots, 2600 in this video, pictures together into one video stream.

This is the second video I made, the first had a quicker frame rate but its all a bit manic, check it out, http://youtu.be/MSkh5jSR1ws.

I got a Raspberry Model A, a Camera board and a freeloader charging pack.


Getting the Pi to take a lot of pictures!

If you haven't already you going to need to setup your camera see this post on raspberrypi.org for info.

I created a simple script which used the time lapse function of the raspistill command.

Create a directory to hold images

mkdir ~/images

Create script to capture images

nano ~/capturetimelapse.sh

filename=$(date -u +"%Y%m%d_%H%M%S")_%04d.jpg
/opt/vc/bin/raspistill -o /home/pi/images/$filename -tl 1000 -t 7200000 > /home/pi/camera.log 2>&1 &

The filename=$(date -u +"%Y%m%d_%H%M%S")_%04d.jpg line is used to format the filename to the date and time the script started and add a rolling number to the images so the images are saved as:

[2013 May 27th _ 9:30:12 _ image number.jpg]

20130527_093012_0001.jpg
20130527_093012_0002.jpg
20130527_093012_0003.jpg
20130527_093012_0004.jpg
...

The /opt/vc/bin/raspistill command is then run with the following options:
  • -o /home/pi/images/$filename - uses the filename we specified as the output file
  • -tl 1000 - sets timelapse mode and tells it to take a picture every 1000 milliseconds (1 second)
  • -t 7200000 - set the timeout and to record for 7200000 milliseconds [2 hours * 60 minutes * 60 seconds * 1000]
  • > /home/pi/camera.log - tells the command to log messasge to camera.log
  • 2>&1 - tells the command to log errors to camera.log 
  • & - tells the command to run in the background
An init.d script is then used to make this run at start-up.  If you want more information about running commands at start-up on the Pi, see Rasberry Pi - Running programs at start-up.

Make script executable

chmod +x ~/capturetimelapse.sh

Run the script at start-up

sudo nano /etc/init.d/runtimelapse

#! /bin/sh
# /etc/init.d/runtimelapse 

### BEGIN INIT INFO
# Provides:          runtimelapse
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Simple script to start a program at boot
# Description:       A simple script from www.stuffaboutcode.com which will start / stop a program a boot / shutdown.
### END INIT INFO

# If you want a command to always run, put it here

# Carry out specific functions when asked to by the system
case "$1" in
  start)
    echo "Starting timelapse"
    # run application you want to start
    /home/pi/capturetimelapse.sh
    ;;
  stop)
    echo "Stopping timelapse"
    # kill application you want to stop
    killall raspistill
    ;;
  *)
    echo "Usage: /etc/init.d/runtimelapse {start|stop}"
    exit 1
    ;;
esac

exit 0

Make script executable

sudo chmod +x /etc/init.d/runtimelapse

Start script to test it

sudo /etc/init.d/runtimelapse start

Stop the script

sudo /etc/init.d/runtimelapse stop

Register script to run at start-up

sudo update-rc.d runtimelapse defaults

Note - you can stop the script running at start-up by using: 

sudo update-rc.d -f runtimelapse remove

Capture video

I gaffer taped the Pi, Camera and Battery to the font of my car, the camera is taped to a small block of wood to make it easier to mount, the Pi and battery pack are in a plastic bag taped behind the license plate.   


Plug it in, the Pi will start up and the time lapse program will run, you will know its working because the light on the camera will turn on, then drive...  You will need a lot of footage, 24 frames a second is about typical for a timelapse, so 24 minutes of footage give you 1 minute.

Make the timelapse video

I copied the files off the SD card onto my Windows PC using LinuxReader and then used Windows Live Movie maker to make the timelapse video, check out this video on youtube for a tutorial.  The software is free and it does a reasonable job.


27 comments:

  1. Nice one, Martin :-)
    Just in case you should want to create the video on the Pi itself, I've blogged about a way to do this with mencoder:
    http://www.recantha.co.uk/blog/?p=3201
    You might have to play with the settings a bit to get decently high quality.

    ReplyDelete
  2. Great tutorial. Just need to add the "sudo chmod 755..." for the init.d startup file as you detail in your other blog to make this work

    ReplyDelete
  3. If the filename was set to overwrite (i.e. static), would this be a far simpler solution than trying to get an image to stream (the issues indicated by the number of threads on the RPi official forum!)? One could simply refer to "filename.jpg" in the HTTP script: does anyone now how to set an automatic refresh in the HTTP script?

    ReplyDelete
    Replies
    1. You can use Javascript to create a html page page which would reload, thats pretty easy; I've not really looked into streaming though, so not sure if this would solve your problem.

      Delete
  4. Having a problem. Images directory remains empty. Followed your tutorial to the letter.

    Can use 'raspistill -o image.jpg' to grab a frame but none of the scripts want to grab an image and save to the images folder.

    I see the messages on starting and stopping the script and the camera light turns on...

    Nit sure what's good wrong.

    ReplyDelete
    Replies
    1. Hi Rob,

      Have you called the directory images and Images (it is case-sensitive)?

      I would check the camera.log file, run cat ~/camera.log, and see if there are any errors being written to it.

      Does the light for the camera come on when you run the script?

      Let me know how you get on.

      Mart

      Delete
    2. Thanks for reply, not had chance to try again yet. Let you know how I get on. Thanks

      Delete
    3. Tried again from scratch. Checked the code 3 times, no spelling mistakes etc.

      On checking the log, there are multiple entries about not being able to create files in the images directory.

      I'm logging in as the default 'pi' user. Could this issue be connected to that?

      When I do a test using 'raspistill -o image.jpg' it always works.

      Baffled.

      Delete
  5. Hey, This is a bit of a strange one, I can't get this script to work, and on further investigation I can't even get the pi to take a picture manually using the "raspistill -o image.jpg" command.

    The script will start up and everything, but it "takes" a empty picture!!

    Any Ideas?

    ReplyDelete
    Replies
    1. Have you followed the guide to setup your camera including enabling it in raspiconfig

      Delete
  6. Hi,
    Is there any benefit to using the Pi Camera to do stuff in preference to a standard USB webcam??

    thanks

    Phil

    ReplyDelete
    Replies
    1. I suppose it depends on the usb cam you using but I would say:
      - smaller
      - cheaper
      - quality, cam board is 1080p
      - lower power usage
      - doesn't tie up a usb port you could use for something else.

      Delete
    2. The CSI camera is connected directly to the GPU I think (I don't know the exact details) and it's extremely fast, capable of 30fps at full 1080 resolution. All USB cameras I know are really slow (no more than 3 or 4 fps) making them unusable for many purposes.

      Delete
  7. I can confirm that this script and Martin's guidance does work. To avoid spelling mistakes I would copy and paste straight into nano text editor on the RPi. You will need to make sure that the RPi cam is set up in raspiconfig. If you want to disable to red light (whilst useful for troubleshooting, it will reflect off glass into the camera), you can add "disable_camera_led=1" to the /boot/config.txt file (sudo nano /boot/config.txt). I prefer to use mencoder to compile the video (search for "DesignSpark timelapse" webpage for advice and look in the comments) as Windows Movie Maker really needs a high power PC to get this done.

    ReplyDelete
    Replies
    1. Stu,

      Are you logging in as root or pi user? I get errors in the camera log when using pi.

      Do I need to use root?

      Delete
    2. Hi Rob

      No you shouldn't need to use root I use the pi user without any problems. Out of interest does it work when you use sudo? They might suggest you problems are permission based.

      Mart

      Delete
  8. This is awesome! I did a little research, and I came across something. It is how to run Terminal commands from Python. (The link: http://ubuntuforums.org/showthread.php?t=729192). Anyway, I thought of something. What if you put all the code for taking pictures, and packed it into a file, like take-picture. Then, code in Minecraft Pi with Python, and make it so when you right-click a certain block, it takes a picture with the camera. Like using if to detect the block hitting, and then do the command ./take-picture. That would be cool! I would do this myself if I had a camera Pi.

    ReplyDelete
    Replies
    1. Easy done, although it doesnt have to be take a picture it could be anything, light an led maybe?

      Delete
    2. That would be awesome too! But how do you make the LED light flash?

      Delete
    3. You should get into gpio programming see this post about getting an led to flash. www.stuffaboutcode.com/2012/11/raspberry-pi-getting-led-to-flash.html?m=1

      Delete
    4. Hmm... Does that require soldering? I'm not that type of person. But maybe I can get my brother to do it if it is required...

      Delete
    5. Not if you dont want to, there are plenty of. Addon boards, or you could get female to male jumper leads which will connect straight to the gpio pins on the pi and plug into a breadboard.

      Delete
    6. Hmm.. Is there anything else physical that you can change with the Raspberry Pi? Like the power lights?

      Delete
    7. How much does a camera for Pi cost?

      Delete
    8. You can control almost anything electrical, including using relays to turn off lights google 'raspberry pi home automation' and have a look at a piface, its a great addon for the pi which has leds, motors and relays. Careful playing with electrics though!

      I think i paid about £15 for the camera, dont know how much theynare in the states though.

      Delete
  9. Thanks allot

    As i was not very succesfull with the timefrime option of raspistill (the brightmess was good on the first image and then got darker and darker for the next 5 images or so) i wrote this little script:

    #!/bin/bash

    numImages=600
    preHeat=2000
    awb=cloud
    quality=85


    for i in $(seq $numImages)
    do
    filename=$(printf "frame_%04d.jpg" $i)
    raspistill -n -w 1920 -h 1080 -t $preHeat -q $quality -o $filename -awb $awb
    sleep 8
    done

    ReplyDelete