A Megapixel Time-lapse Camera System

Introduction
The NSLU2 running OpenWRT and the QuickCam Pro 9000 combine to make a very versatile device. In this post I’m going to describe how to use them as a time-lapse camera which takes photos every ten seconds at 1600×1200 resolution and stores them on a flash drive. The photos occupy anywhere from 80K to 400K each depending on the complexity of the scene. This means that around 10,000 to 15,000 will fit on a 4GByte flash drive, corresponding to 20-40 hours of coverage. All that is required is an NSLU2, a UVC-compatible webcam, a flash drive, and an OpenWRT firmware build, which is available here. A brief sample movie (30 seconds, 8.5Mbytes, DivX) is available here.

A simple hardware mod

The first step in building this arrangement is to bypass a hardware limitation of the NSLU2. As provided, only one of the two USB ports has power. If we want to attach both a webcam and a flash drive, we need both ports to supply power. The solution is a very simple hardware mod (a single jumper) which is described here. A good description of how to open the case can be found here.

Installing the firmware

This process is described in “A High-Resolution IP Camera”, but for completeness here it is again. You will need:

  1. An NSLU2
  2. A host computer, best running Linux
  3. The firmware image, openwrt-nslu2-uvc-webcam2.bin

You will need to run a tool called UpSlug2 to install the firmware. UpSlug2 is described here. I would recommend running it from Linux. I used my Ubuntu machine, but booting a Knoppix Live CD on a Windows machine should work fine. Under Ubuntu, getting UpSlug2 is as simple as:

apt-get install upslug2

The next step is to power-up the NSLU2 in “upgrade mode”. It should be connected to the same LAN as your Linux machine, but do not plug in the camera or flash drive at this stage. Use a straightened paper clip or other thin probe inserted in the Reset hole at the back of the NSLU2. Apply gentle pressure and the reset switch will click in. Hold it down and press the power button at the front. The Ready/Status LED will come on orange, but after nine or ten seconds will turn red. Release the reset switch as soon as this happens. The Ready/Status LED should start alternating red and green, indicating that the device is ready to upgrade. Now issue the command:

sudo upslug2 --image="openwrt-nslu2-uvc-webcam2.bin"

If all is well and UpSlug2 can find the NSLU2 you should see a nice animated summary of the upgrade process, which will take about one minute twenty seconds. When it finishes you should wait for about two more minutes for it to perform initial setup – eventually the Disk 1 and Disk 2 LEDs should go out and the Ready/Status come on red. You can then disconnect the power, plug in the webcam and the flash drive, and power it up again. After about 60 seconds the red ring light on the camera, indicating that it is recording, should come on.

Recording images

Once the firmware is installed, the NSLU2 will start recording images as soon as it has started up. The recording is done with Tom Stöveken’s versatile MJPG-streamer software. MJPG-streamer is capable of writing images to external storage at regular intervals. It names each image with a rather long but unique name of the form:

yearmonthdayhourminutesecond-picture-serialnum.jpg

e.g 2008_05_11_08_38_50_picture_000000046.jpg.

The serialnum increments by 1 for each successive image. The firmware has been set up to write these files to a normal flash drive filesystem; that is, in MS-DOS or more properly FAT32 format. Its not the worlds best filesystem, but it is ubiquitous and works with almost everything. Most flash drives come formatted this way, so should work immediately. Note that the firmware does not make subdirectories; all the files are written to the top directory of the drive. There are limits to how many files can be placed in one directory like this – officially 65536 is the limit for FAT32, but I haven’t tested this and would advise caution.

The firmware makes use of the LEDs on the NSLU2 to describe its status. It will look at what resources are available when it starts up, and select one of the following four modes:

– No-camera mode. If no UVC-compatible webcam is found, the Ready/Status LED will turn red and nothing interesting will happen.

– IP-camera mode, where it streams the camera images to a web page. This is pretty much the same as the system described in my earlier post “A High-Resolution IP Camera”. The Ready/Status LED will be green and the Disk 1 LED will be on.

– Dual IP-camera mode. If it finds two cameras, it will try to stream both of them to two separate web pages. The second web page is at the same address as the first, but on port 8080. The Ready/Status LED will be green and both the Disk 1 and Disk 2 LED will come on. I haven’t actually tested this as I only have one camera.

– Time-lapse camera mode, which is the main subject of this post. In this case no web page will be available and the NSLU2 will start storing images on the flash drive every ten seconds. The Ready/Status LED will be green, the Disk 2 LED will come on, and you should see activity every ten seconds on the flash drive’s LED (if it has one).

The Ethernet LED will, in all modes, as usual, indicate whether or not the NSLU2 is connected to a LAN. In time-lapse camera mode the NSLU2 does not need to be connected to a LAN and can operate independently.

Time-lapse camera mode runs at the full 1600×1200 resolution of the camera, which is possible because we are only processing an image every ten seconds. The NSLU2 can’t cope with these images at much speed (see this post for a discussion of why). The disadvantage of the high resolution is that it can’t do a web page as well. MJPG-streamer is quite capable of doing both, but the resolution has to be reduced to 960 x 720 in JPEG mode, and as I expect the time-lapse mode to be used for unattended long-term recording, I have adopted the higher resolution.

Trouble at mill; filling up the disk

It would be nice to be able to delete the oldest images once the flash drive filled up, so that the camera could run continuously and keep only the new images. This turns out to be difficult to do. With 10,000 files on the drive, the ‘ls’ command that would find the oldest becomes glacially slow, taking over twenty minutes to complete. It also risks crashing the NSLU2. Instead, I have installed a script that checks for a nearly-full drive and issues the ‘poweroff’ command when there is only 15Mbytes free.

Viewing the images

A flash drive with 13,859 JPEGs on it is not actually a very usable thing. Even though the images are labelled with dates and times, looking through them is tedious at best. A much better way of reviewing them is to turn them into a movie. I spent some time looking for the best way to do this, and in the end discovered that Apple’s QuickTime Pro software has an “Open Image Sequence…” command that does a very good job of it indeed. Its also a good tool for viewing, single-stepping, rewinding, editing and generally working with time-lapse movies, and I can recommend it as worth the money (US$30). The only piece of trickery required is that QuickTime Pro expects to be pointed at the first file of a series of files with numerical sequence numbers for names, and the date prefixed onto the file names by MJPG-streamer confuses it. This is best dealt with by applying a shell script to strip off the date prefix. Writing one that runs at a reasonable speed and can cope with 20,000 files in one directory turned out to be a little tricky, but eventually I found the powerful incantation:

find . -name '*.jpg' -print | sed 's/.*_picture_\(.*\).jpg$/mv "&" "\1.jpg"/' | sh

This can strip paint off bargeboards at twenty metres and will convert about 12,000 filenames a minute on my Mac Mini. For ease of use under Mac OS X I have packaged it up into an AppleScript application that will convert folders of files that are dropped onto it. It is available here. Be warned that if there are multiple sequences in the folder (i.e more than one file that ends with 0000001.jpg) the earlier ones will be overwritten. Quicktime Pro also seems to object to the zero-size .jpg files that the recording process sometimes leaves at the end – you should remove these manually. To strip date prefixes under Windows, I am advised that the Cygwin environment will probably support the incantation above.

Internal details and making changes

openwrt-nslu2-uvc-webcam2.bin is a normal build of a selection of packages from OpenWRT Kamikaze taken from the current ‘trunk’, with a couple of simple shell scripts added. The main script can be found in /etc/init.d/done. Checking for “disk full” is done by a second script which can be found in /root/stopwhenfull, and the cron entry that runs it every three minutes is added to /etc/crontabs/root by the ‘done’ script. A copy of the two scripts and a list of the packages in use can be found here. You can telnet directly into the OpenWRT shell, which has no password. You should set a password and use ssh thereafter.

Many things can be changed if you log in and edit the scripts. The time between images is controlled by the -d parameter to mjpg_streamer (in milliseconds). I wouldn’t advise setting it to less than five seconds at 1600×1200 on the Pro 9000, as the NSLU2 is likely to crash. Image size is controlled by the -r parameter and image type by -y. The stopwhenfull script assumes that you won’t generate more than 5Mbytes/minute and should be adjusted if this is not the case.

The firmware does not have to write to a flash drive, but is not likely to work with a USB hard drive as they are usually formatted with NTFS or somesuch, which it does not understand. It does understand the Linux ext3 filesystem, though, which might be more suitable for larger applications anyway, as it doesn’t seem to have a limit to the number of files that can be in one directory.

The firmware includes an NTP client which will try to keep accurate time if the NSLU2 can find Internet access. As provided it will keep UTC time. If you aren’t in the Greenwich time zone you will need to add an appropriate /etc/TZ file for your timezone taken from here.

I have experimented with Samba and vsftpd with the idea of being able to look at the images on the flash drive as they are recorded over the LAN connection. Performance in both cases turns out to be somewhere between glacial and completely fossilised if there are very many files on the disk. I have left vsftpd in as it can be useful for checking that the images are OK just after startup, but I can’t recommend using it once there are more than a few hundred images. To use vsftpd you will need to login over telnet and set a root password, then use that password and the username ‘root’ to gain access over ftp. The firmware uses DHCP to obtain an IP address, as described in “A High-Resolution IP Camera”.

Limitations and improvements

The firmware as it stands has a few deficiencies which I would like to address.

– There is no way of turning the NSLU2 off, which would be a good idea, as its not really very nice to just unplug the flash drive as it runs. With any luck OpenWRT will someday get the power button working as an off switch.

– The focus controls on the QuickCam aren’t accessible in time-lapse mode. The camera can actually focus down to a few centimetres and it would be useful for studying very small slow things if the focus was available.

– A simple web page that at least gave a static image would be handy in time-lapse mode for checking the field of view.

– A better way of storing files than the present single directory would be nice, and the best way to do it would be to modify MJPG-streamer.

– Battery-driven operation is possible but a bit demanding; the combination of the NSLU2, the Quickcam Pro 9000, and a 4GByte flash drive draws 900-1100 mA at 5V while recording, which is rather a lot for batteries. I want to look into reducing this for remote recording applications.

, , , , ,

  1. #1 by Tom on June 5, 2008 - 8:05 am

    Great to see such a nice example! If I find the spare time, I wwant to add a ringbuffer-mode to the file output-plugin.

  2. #2 by Roland on June 10, 2010 - 3:54 pm

    Hi, I just bought a Lifecam Cinema HD webcam and tried the openwrt-nslu2-uvc-webcam2.bin image on my NSLU2 and it works great with my Logitech 9000 webcam, the Lifecam is on port 8080 while the Logitech is on port 80… I tried running the two side by side using two browsers and it works great (the Lifecam has better quality and fps though) and I’m thinking, is there anyway for me to have the two outputs in one web page instead of two separate ones? I looked at the html code for the javascript and they are both the same, so dunno what I need to show both outputs in one page…

    • #3 by johnarthur on June 12, 2010 - 9:14 am

      Don’t know either, but it should be possible. I guess the trick would be to find a way to get one page to include the image from the other…

  3. #4 by Ben on August 10, 2010 - 12:35 pm

    Just wanted to drop you a quick note to say thank you for this and the other NSLU2 posts – very useful info!

    Thanks again!

  1. A Battery-powered Time-Lapse Camera « The Machine in Shaft Ten

Leave a comment