Thursday, 28 March 2013

Time Lapse Videos of the Garden (1 of 3)

I've gotten quite into hillwalking this year. In fact I even have another blog dedicated purely to that which I will shameless plug here: it is munrochallenge.wordpress.com and it details me and my friend Martyn's attempt to climb 52 munros this year. (We've managed 3 so far.) As part of this new hobby I signed up to a few on-line forums to meet other people with similar interests and one of the forums I go on frequently is the one at walkhighlands.co.uk.

It was on here that a user known as Alastair S (probably his real name!) posted up a thread talking about a little computer program he had made. What it does is connects to public internet cameras anywhere in the world you point it to and downloads an image from them once every minute. When it has a large collection of these images it creates a video out of them which is played back at 20 frames per second. This hence lets you watch an entire days, weeks, months or even years worth of footage in just a few minutes. It's a technique known as 'time lapse video' and it can make for some very interesting videos. Here are a couple he has produced so far:

video

video

and here is the original post on the forum in case you are interested.

This got me thinking about my own internet camera that I had sitting idly in the spare room. I bought it as part of a project I was working on and once the project was finished I set it up to look out over the garden. It's been hooked up for about 2 years now but I very rarely log into it and don't bother recording any of the footage either so really it is just sitting there wastefully.

With Alastair's time lapse idea this may no longer be the case.

So I got stuck straight into it and am glad to say it is already set up and running nicely! It was actually a pretty easy system to set up since a lot of the work I had already done. The camera was already installed and connected to the network properly and I already have a server running 24 / 7 in my house (I am a nerd after all) so all I had to do was get the camera to take a photo every minute and save it to the server then run a separate program to join them all together.

Most internet cameras come with the ability to take a photo at predefined intervals and save them onto a networked computer and mine was thankfully no different. The only issue was that the camera used standard FTP to save the images to a computer and my server was running SFTP. Not a huge problem though, a quick Google turned up vsftpd which can run a standard FTP server. I installed that with the command:

sudo apt-get install vsftpd

Then followed the instructions on Ubuntu.com to get it set up which in brief are:
  1. edit /etc/vsftpd.conf and uncomment these two lines:
    • local_enable=YES
    • write_enable=YES
  2. Restart vsftpd:
    • sudo service vsftpd restart
And that was it, pretty straight forward. I tested the connection by going onto FileZilla and connecting using my details on FTP mode and all worked nicely and I made a new directory called Garden whilst I was there to keep all the photos in.

Then it was over to the camera to get things set up there. All internet camera have a pretty similar range of functions even if they are displayed differently on the camera's web interface. Here are the settings for mine now that it is all set up to save images to the server:
As you can see the images are going to my server at 192.168.0.2 using the standard FTP port of 21 and my username and password. I'm putting them in the directory /home/ryan/Garden and I've set it to do this every 60 seconds. There is also a scheduler at the bottom that I'm not currently using but thought I'd show anyway as I will probably use it in the near future to save taking images at night when you can't see anything!

A quick test and I found that this all worked nicely and I had a new image appearing in the Garden directory every minute on the dot.

I left this running for about an hour and then came back to see about the next bit of the task, putting them all together to form a video. I knew this was possible with FFMPEG as I had used it before for a similar task so I gave it a quick Google and came up with 2 good results: stackoverflow.com question on creating video from images and FFMPEG.org on creating video from images. Through a combination of these I came up with this script:

x=0; for i in *jpg; do counter=$(printf %04d $x); cp "$i" /tmp/img"$counter".jpg; x=$(($x+1)); done
ffmpeg -f image2 -r 4 -y -i /tmp/img%04d.jpg garden.avi
rm /tmp/img*

What this does is the first line copies all the images from the current directory into the /tmp/ directory and renames them img0000.jpg, img0001.jpg, img0002.jpg and so on. I had to use cp rather than creating a symlink as the answer on StackOverFlow suggests as the next line didn't work for me with them.

The next line is the call to ffmpeg (which I installed with sudo apt-get install ffmpeg ) and it tells ffmpeg to create a video from images (-f image2) at 4 frames per second (-r 4) automatically overwriting the previous video if there is one (-y) using the input images in the /tmp/ folder that have the img0000.jpg name pattern (-i /tmp/img%04d.jpg) using the avi format and the filename garden.avi (garden.avi).

The final line deletes all the images that were created during the copy process.

I had to do this copy as the pattern the camera uses for file names was suitable for processing by FFMPEG and couldn't be changed on the camera itself. Using cp is obviously a lot slower and more memory consuming than creating symbolic links but speed and space aren't an issue here so I don't mind. I could also move the files and rename them as I do but I want to keep the originals so went with cp then rm.

And that is all there is to it! A quick FTP of the output file across to my computer and a short upload to YouTube and here is the result of that 1 hour worth of photos:

Garden Time Lapse Test

Not bad. Here is the result after leaving it running the rest of the day (450 images so about 7.5 hours):
Garden Time Lapse Test 2

It shows that from around 1:20 onwards there isn't much point in capturing images as you can't see anything but darkness. The camera is an infra-red one so I may see about putting some infra-red lights outside (the ones built into the camera reflect off the glass of the window) but I'll see how I got on with the current set up first.

I'm going to play around with the settings the next few weeks seeing what like the videos are when I take more or less photos per minute, play them back at a faster or slower frame rate and also create videos over a longer length of time. I'm really looking forward to what sort of video I'll have come this time next year if I keep it going. I'll most likely look into adding some scripts to cron as well so I can get most of this completely automated!

I'll post up more about this in a few weeks. Hope you've enjoyed this and thanks again to Alastair for the inspiration behind the idea.

Happy gardening and computing!

No comments:

Post a Comment