The Sysadmin's Daily Grind: Surveillance with Motion

Fish Spotting

Charly Kühnast

Commercial surveillance systems are expensive and typically involve maintenance agreements. I thought there must be a low-budget alternative.

Sysadmin
PHP for Sysadmins56
PHP isn't just for the web. We'll show you how you can integrate command line tools into PHP system administration scripts.
Admin Workshop: Locks60
A lock denies a process access to a resource.

Olli and Melanie, two good friends of mine, have three stores. One of the stores has been broken into many times. Of course, they are insured against break ins, but they were concerned by the fact that nobody had been able to nail the culprits. They wondered if a surveillance system might help.

Of course, turnkey surveillance systems are expensive, so my friends asked me if an old PC and a webcam would do the trick. Now, I do own a webcam, but up to that point I had only used it to point at the aquarium in my living room. I had no experience whatsoever with surveillance applications. But hey, that's what search engines are for. After a short search, I had soon found a useful application called Motion [1].

Motion is easy to install. On my Debian test machine, which I chose simply because it already had the webcam attached, I just typed

apt-get install motion

Users with other distributions can use the RPM or tar archives. Motion accesses an arbitrary V4L or V4L2 device, in this case my webcam, and compares the current image with the previous image. It stores the current image if the current image deviates from the previous image by a configurable number of pixels. The following command is all I needed to use this simple function:

motion -t /home/charly/motion

The -t<$> parameter tells Motion where to store the images. Now, when I walk past the webcam the PC speaker beeps! Motion has detected a change in the image. If you prefer to do without the beep, just add -Q<$> to the command. If Motion is running in daemon mode (parameter -D<$>), sound is disabled by default.

When Pictures Learn to Count

If you need to fine tune Motion, you will definitely need to modify the optional configuration file. The file can reside below /usr/local/etc/ or in your home directory. The configuration file allows you to set the sensitivity of the image detection feature. The following entry

threshold 1500

stipulates that at least 1500 pixels need to have changed from one picture to the next before Motion detects a change. My Siamese fighting fish are brave, but quite small, so I set the option to half this value. Lo and behold, Motion reacts whenever a fish of one and a half inches or more swims past the webcam.

If your Linux distribution has an MPEG encoder, Motion will even generate a film from a series of still images (ffmpeg provides direct support for this, for example.) Motion.cgi [2] makes using Motion even simpler. The add-on package includes a Web interface for starting, stopping, and recording with Motion. It also allows me to tap into the data stream at any time to view a live image like the one in Figure 1, which shows my denizens of the deep.

Figure 1: A Siamese Fighting Fish [3] swimming happily through the picture on Charly's webcam, as captured by Motion.cgi.

Note that the camera you use can affect the usefulness of the results Motion give you. My old webcam was fine as long as the room was light enough, but it was useless in the dark. As I don't expect the burglars who keep breaking into Olli and Melanie's store to put on the lights and say "cheese," it looks like my friends will have to invest in a new webcam.

Info

[1] Motion: http://www.lavrsen.dk/twiki/bin/view/Motion/WebHome

[2] Motion.cgi: http://www.lavrsen.dk/twiki/bin/view/Motion/MotionCGI

[3] A fish: http://www.fishbase.org/Country/CountrySpeciesSummary.cfm?Country=Malaysia&Genus=Betta&Species= splendens

The Author

Charly Kühnast is a Unix System Manager at the data- center in Moers, near Germany's famous River Rhine. His tasks include ensuring firewall security and availability and taking care of the DMZ (demilitarized zone).