Raspberry Pi Timelapse
This weekend, Byers Imports unveiled the new Jaguar F-Type Coupe and invited people to autocross for free. I set up a Raspberry Pi at the start line to record a picture every 10 seconds. It managed to capture every car that autocrossed and a pretty cool view of the moving clouds.
Connect the camera
The folks over at Raspberry Pi have created a nice tutorial of how to setup the camera and connect it to Pi.
Enable the camera
I will assume that you know how to start up a Raspberry Pi running Raspbian and connect a monitor and keyboard/mouse to it. If not, some quick google searches should help you get there.
Open a LXTerminal in Pi and open raspi-config
.
sudo raspi-config
Select Enable camera
and hit Enter
, then go to Finish
and you will be asked to reboot.
Taking pictures
The raspistill
command is used to take pictures along with various configuration flags.
raspistill -o test.jpg
This will place a test.jpg
file in your home folder. The ‘raspistill’ command takes about six seconds to take a picture.
Timelapse script
I wrote a simple python script to take a picture every ten seconds.
import os
import time
import RPi.GPIO as GPIO
from datetime import datetime
# Determine the current date and time
d = datetime.now()
startYear = "%04d" % (d.year)
startMonth = "%02d" % (d.month)
startDate = "%02d" % (d.day)
startHour = "%02d" % (d.hour)
startMin = "%02d" % (d.minute)
#Define the location where files need to be saved
saveFolder = "/home/pi/timelapse/timelapse_" + str(startYear) + str(startMonth)+str(startDate)+ str(startHour) + str(startMin)
os.mkdir(saveFolder)
#First File
fileIndex = 1
#Infinite Loop
while True:
d = datetime.now()
if True:
#Set file number
fileNumber = "%04d" %(fileIndex)
#Get current time
hour = "%02d" %(d.hour)
mins = "%02d" %(d.minute)
imgWidth = 800
imgHeight = 600
print "===============Saving file at " + hour + ":" + mins
#Capture image
os.system("raspistill -w " + str(imgWidth) + " -h " + str(imgHeight) + " -o " + str(saveFolder) + "/" + str(fileNumber) + "_" + str(hour) + str(mins) + ".jpg -sh 40 -awb auto -mm average -ex auto")
#Increment file number
fileIndex += 1
#Wait ten seconds before the next image
time.sleep(10)
else:
print "==================Nothing is happening"
The code is pretty well commented, but I will delve into the actual capture image part. I have included some flags with the raspistill
command, and they are described below.
Flag | Description | Value |
---|---|---|
-w | width | 800 |
-h | height | 600 |
-o | output file name | /path/to/file.jpg |
-sh | sharpness | 40 |
-awb | auto white balance | auto |
-mm | metering mode | average |
-ex | exposure | auto |
More detailed flag specifications are available here.
Automatic startup on boot
In order to make the Pi start taking pictures when it was powered on, I added a cron job to the crontab.
Navigate to the home directory in Pi using cd
. Then, use crontab -e
and add the following command to the crontab.
@reboot python /path/to/timelapse/file.py
Make sure you shutdown.
sudo shutdown -h "now"
This will ensure that your timelapse.py
script will start running every time you reboot the Pi.
Making the Video
Install the mencoder
package that converts all the pictures into a video.
sudo apt-get install mencoder
Then, navigate to your directory that has all the pictures and execute the following commands
/* This adds all the pictures to a list */
ls *.jpg stills.txt
/* Make the video */
mencoder -nosound -ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vibrate=8000000 -vf scale=1920:1080 -o timelapse.avi -mf type=jpeg:fps=24 mf://@stills.txt
Now you can copy the timelapse.avi
file from the Pi using a FTP client or from the SD card itself. Enjoy your video!
BONUS! - Pi Remote Access
Instead of having to keep connecting the Pi to the monitor and keyboard and switching out the SD card every time I needed to copy files, I bought a USB wireless dongle and added it to a USB port on the Pi. The dongle is very easy to install and connect to WiFi using the Raspbian Desktop environment.
Once it was online, I found out the hostname for the Pi using this command:
hostname -I
This should give you an address that looks something like 192.168.1.199
.
Now, using a Mac/Linux terminal or Putty on Windows, you can create an ssh connection directly to the Pi.
ssh pi@192.168.1.199
If you set up a different username, you can replace ‘pi’ with that username. Now, you should be able to access your Pi remotely and copy files over using an FTP client like FileZilla or a bash command like scp
.