This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature

CLI Magic: Getting into Motion

By Mark Alexander Bain on February 27, 2006 (8:00:00 AM)

Share    Print    Comments   

Want to keep an eye on what's going on in your home or office when you're not there? You can turn a Linux box into a motion detector by using an old webcam and Motion -- software for monitoring a Video4Linux device.

User level: Intermediate

The first thing you must do is get your webcam going. Dig out that old one from your spares draw, or buy the cheapest one you can find. (I managed to get a Labtec webcam for £2 (about $3.50) on eBay.) Plug it into your PC's USB port, log on as root, then type lsusb. You should see something like this:

# lsusb
Bus 001 Device 001: ID 0000:0000
Bus 001 Device 002: ID 046d:0929 Logitech, Inc.

Linux recognizes that the camera has been connected, but you still need to install some drivers. Although this is not difficult, this is the part that can put people off. The drivers aren't hard to install, but you need the kernel source in place before you can compile the drivers. This can be a bit daunting if you haven't compiled drivers before. You'll need to check with your own distribution, but I'll show you how to do it with Debian to give you an idea of how easy it is.

First, find your Linux release number:

# uname -r
2.4.27-2-386

Then type in the following commands as root:

# apt-get install kernel-source-2.4.27
# cd /usr/src
# tar -xjvf kernel-source-2.4.27.tar.bz2
# cd kernel-source-2.4.27
# cp /boot/config-2.4.27-2-386 .config
# make-kpkg --append-to-version "-2-386" --revision 2.4.27 --config old configure
# rm -f /lib/modules/2.4.27-2-386/build
# ln -s /usr/src/kernel-source-2.4.27 /lib/modules/2.4.27-2-386/build

With that out of the way you're past the most difficult part of the whole process -- now you can start thinking about installing a driver for the camera.

When looking for a driver, start with Spca5xx. This site features a vast list of cameras. Use the vendor and product IDs to select the best driver package. The lsusb output helps you find that information. You can also try Quickcam Express if you can't find a suitable driver on Spca5xx.

Whichever driver you decide to use, follow the installation instructions to load it. Within a few minutes, your webcam will be connected to your Linux box with a driver that supports it. You can't do anything with it yet, but that's where Motion comes in.

Install Motion either by using your distribution's method (e.g., it's apt-get install motion for Debian) or by downloading the tar file, uncompressing it, and following the installation instructions.

Now you're ready to start doing the interesting bits. Start by typing motion on the command line. To test the software, wave your hand in front of the camera. If you've managed to detect motion, you should see output similar to:

File of type 1 saved to: /tmp/01-20060127105755-03.jpg
26898
New threshold: 1500
File of type 1 saved to: /tmp/01-20060127105755-04.jpg
26571
New threshold: 1500
243
New threshold: 1500

Take a look at the images the application saved; you'll probably see a dark image with a blurry something in the middle of it. At least this proves that the motion detector works. Now you need to halt Motion and start customizing its operation to suit your requirements. To stop Motion, use ps to find the process number and then use kill on it.

You can use a number of command-line flags to change how Motion operates, but you'll find it much easier to use its config file, which by default is /etc/motion/motion.conf. To make your own customizations, create a directory called .motion in your home directory and copy the config file into the new folder.

First, set up Motion so that it works as a webcam viewer, so you can see the effects of your changes to the conf file immediately. Edit ~/.motion/motion.conf and change the web_port and webcam_localhost lines so that they read:

webcam_port 8000
webcam_localhost off

Then add the following lines:

snapshot_interval 1
snapshot_filename snapshot

Restart Motion -- remember to kill the process and then retype motion on the command line. You should see something like this:

$ motion
Processing thread 0 - config file /home/mydir/.motion/motion.conf
Thread0 device: /dev/video0 input: 8
File of type 2 saved to: /tmp/snapshot.jpg
File of type 2 saved to: /tmp/snapshot.jpg
File of type 2 saved to: /tmp/snapshot.jpg

All you have to do now is open up a Web browser (either on the PC where Motion is running or on any other PC on your network) and type in the URL of the machine followed by the port number that you defined in the conf file. This should let you view the output from the webcam (updated once a second).

You'll probably find that you've now got a live but dark image. You can rectify this by changing auto_brightness to on and restarting Motion.

Now you've got a working webcam, but what you really want is a working motion detector. The step from one to the other is very small -- simply edit motion.conf and add the line:

locate on

Restart Motion, wave your hand in front of the camera again, and have a look at the display in your Web browser. This time you'll see a box marking the area where motion was detected.

You're now able to see that the motion detector is working, but what's the point of having a motion detector if you're just going to have to sit and watch a screen? Also, if you do a ls /tmp, you'll find dozens of JPEG files produced by the software. Obviously, Motion isn't of much use as it stands. You can do a number of things to make it useful. First, add the following lines to the conf file:

threshold_tune off
threshold 2000

This allows you to set your own levels at which Motion will trigger, making it much more discriminatory. Next, you can add the line:

ffmpeg_cap_new on

Now the software will save video files as well as JPEG files -- and video files are much more useful. Even so, the files are not much use if you have to come into the office and review the files in order to see if any motion has been detected. However, Motion lets you execute a command to determine when motion has been detected. Add this line:

execute /home/mydir/motion/on_motion_detected

This lets you run the file /home/mydir/motion/on_motion_detected as soon as the motion detection is triggered. All you have to do is write some code to the file to be triggered:

DATE=$(date +"%Y%m%d%H%M%S")
ALARM_EMAIL="/tmp/myalarm.tmp"
echo "Subject: Motion detected - $DATE" > $ALARM_EMAIL
echo "" >> $ALARM_EMAIL

echo "Motion detected - check $DATE.avi" >> $ALARM_EMAIL
cat $ALARM_EMAIL | /usr/sbin/ssmtp author@markbain-writer.co.uk 

This code will run every time Motion does its stuff. (Don't forget to use your email address instead of mine!)

Now, whenever you wave your hand in front of the camera, Motion sends you an email telling you so. You may notice that the motion detector appears to trigger once and then wait awhile before it triggers again. There's nothing wrong -- a 60-second delay is built into the system. You can change the interval by adding the gap parameter. For instance, to create a 10-second delay between triggers, add the line:

gap 10

Have a look at the Motion man entry for the full list of parameters for the conf file. You'll find useful items such as text_left, which allows you to add text to the pictures taken.

There you have it -- a simple and cheap but effective motion detector. One final word of advice though: when setting up your motion detector, don't put it where your cat can get at it. It's amazing how quickly she can fill your email inbox.

Share    Print    Comments   

Comments

on CLI Magic: Getting into Motion

Note: Comments are owned by the poster. We are not responsible for their content.

Very cool!

Posted by: Anonymous Coward on February 27, 2006 10:10 PM
Great article and very cool!
It would also be cool to make "audio detector".<nobr> <wbr></nobr>:)
And maybe use IR!<nobr> <wbr></nobr>;)
One thing that would be really cool would be if you could remotely control the camera in which direction it looks.
Motion detector maybe can be useful for your house at night when you sleep, or in your server room to know if someone enter it.

Too bad you had to compile with kernel source and that, and it wasnt easier than that. I thought devices that used USB didnt need drivers or something.

#

Re:Very cool!

Posted by: Anonymous Coward on February 28, 2006 03:40 AM
There is already (at least) one project which can be used to record audio when a given threshold is reached.

<a href="http://manauton.sourceforge.net/" title="sourceforge.net">http://manauton.sourceforge.net/</a sourceforge.net>

It can also timestamp with a bleep code contained within the output wav file.

Simon

PS Don't you hate it when you can't remember the name of a project you've used in the past. Took a while to find this one's name.

#

Trigger

Posted by: Anonymous Coward on February 28, 2006 01:54 AM
Would be pretty cool to put some of these;
* shred<nobr> <wbr></nobr>/home/alice/alice_key.pgp
* umount<nobr> <wbr></nobr>/mnt/encrypted/
* ogg123 warning_motion_detected.ogg
in the "on_motion_detected" file.

Another cool thing would be to make a 9mm turret track the movement, scary? yes!

#

ZoneMinder

Posted by: Anonymous Coward on February 28, 2006 05:29 AM
Nice article on Motion, thanks!

Someone should do an article on ZoneMinder which takes it to the next level:
<a href="http://www.zoneminder.com/" title="zoneminder.com">http://www.zoneminder.com/</a zoneminder.com>

Screenshots: <a href="http://www.zoneminder.com/screenshots.html" title="zoneminder.com">http://www.zoneminder.com/screenshots.html</a zoneminder.com>

#

Re:I used motion to track a nest of blue tits

Posted by: Anonymous Coward on March 01, 2006 01:02 AM
Very cool little project.

Good use.

Very nice.

Thanks for sharing it.

#

Wrong URL for driver page

Posted by: Anonymous Coward on March 02, 2006 12:23 AM
The URL for the Spca5xx driver page is 404 due to a trailing slash.

#

I used motion to track a nest of blue tits

Posted by: Administrator on February 27, 2006 10:51 PM
I used motion and a cheap ebay IR spycam last year to track a nest of blue tits. Motion recorded all the important steps:

-First visits.
-Nest building.
-Madam Tit laying eggs.
-Tits birth.
-Tits feeding.
-Take off...

Hours and hours of video...
It was wonderfull !
I replay the experience this year...I just go the first visit of the nest yesterday....still using motion.

U want a taste of it ?

<a href="http://tittux.skynetblogs.be/" title="skynetblogs.be">http://tittux.skynetblogs.be/</a skynetblogs.be>

Ciao, ciao,
Luc

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya