Hack 64. Motion Capture and Video Conferencing Fun
Keep an eye on the world with your webcam.
Some years back, it was the height of geek cred to have a webcam. At that point in history, the average webcam was a hulking device that looked more like a CCTV camera and cost an inordinate amount of money. Many of these bulky units also needed an expensive video card to squeeze the huge amounts of data through weedy `486 processors. Since those early days, the success of the webcam has catapulted and virtually everyone has picked one up for peanuts.
With this explosion of webcams and the rapid growth in broadband speed, videoconferencing has become something of a reality. [Hack #63] covered how to use the GnomeMeeting application to make phone calls over the Internet. In this hack, I cover the video conferencing side of GnomeMeeting as well as explore how to enable motion capture so that you can use it to form a security system in your home/office.
8.11.1. Setting Up a Webcam
Before you get started using GnomeMeeting and motion capture, the first step is to ensure that you have a working webcam configured. With more and more people using Project Utopia [Hack #93], device configuration is becoming less of an issue, but it probably needs a brief discussion.
First, you should find out which driver your webcam needs. A number of online hardware databases and some sensible Google searching can help you with this. Then you can find out if that driver is included in your kernel version or if you need to upgrade to a later kernel [Hack #89] that does support your webcam. If the driver is not included in the latest kernel version or you need a newer version of the driver than the one that's included in the kernel tree, you will probably need to patch the kernel source to get the driver you need.
In addition to using a driver for your webcam, you should also ensure that you compile Video 4 Linux support into your kernel. Video 4 Linux provides a standardized method for the kernel to handle video devices. Support for this is in the main kernel tree. It is recommended that this be compiled as a module that can be loaded when you access your webcam.
Most webcams are USB-powered, so you need to ensure that your USB system is configured correctly [Hack #93] . When you plug in a camera, it should load the Video 4 Linux module. Check that it does with this command:
In the output you should see videodev listed. If it is not listed, you should insert it with insmod:
foo@bar:~$ insmod videodev
Once Video 4 Linux is loaded, it creates one or more video entries in /dev. Check this with:
foo@bar:~$ ls -al /dev/video*
When you run this command, you should see at least one entry appear. If this is not the case, your camera is not working with Linux. You should double-check your previous work to make sure you did everything necessary.
8.11.2. Using GnomeMeeting
When you first run GnomeMeeting, you are taken through a configuration druid that helps you set up and configure the program. Included in this setup routine are some features for ensuring your webcam is working properly. At the end of this process, you can click the webcam icon and see the video from your camera in the window.
If you see a corrupted picture when viewing video in GnomeMeeting, the webcam driver might have some bugs that might require an update to a newer driver version; this has been a problem with the OV511 chip-based range of devices. You should check your camera with a range of software such as xawtv or Camorama. If you can get video working in other tools, it might be a problem with how GnomeMeeting is accessing the device. If this is the case, you should contact the GnomeMeeting developers at http://www.gnomemeeting.org.
8.11.3. Creating a Motion Capture Camera
The concept of motion capture is fairly simple. You set up a camera in a particular location and the camera registers when a particular threshold of pixels changes. As an example, you could have a camera focused on a room, and if someone walks past the camera the recording software is triggered by the motion.
This hack covers a tool called motion that is incredibly flexible in dealing with a variety of motion capture needs. What is particularly interesting about motion is the range of responses that can be triggered when motion occurs. The software can send you an email, update a database, save a picture, record a video clip, play a sound, and more. motion is also flexible in how it is configured and used.
To get started, first you should install motion using your distribution package manager, or from the official web site at http://www.lavrsen.dk/twiki/bin/view/Motion/DownloadFiles. You also need to download the software dependencies if you want to save images and movies when movement occurs. Details about these requirements are on the motion web site.
Running motion is simple; just run it from the command line:
motion reads a central configuration file called motion.conf, which is normally stored in /etc. This easy-to-configure file contains settings for all features within motion. The first section that you should concentrate on is called Capture Device Options. Here you should set videodevice to the device in /dev that you are using (this is usually /dev/video0). You should also adjust the frame rate, as this affects the accuracy of the webcam. The next important section to complete is Motion Detection Settings, where you should set the threshold setting to something that is suitable. This setting specifies how sensitive the motion capture is. To test this, run motion, move in front of the camera, and see how the software reacts. A good test is to look at the camera, stand still, and move your eyes, mouth, and other parts of your face to see if the movement triggers the camera.
The rest of the file contains settings that can be used to send you an email when motion occurs, store information in a database, and store images and video. If you want to store images and video, you should ensure that you set the target_dir setting to a directory where you want to store the images/video.
motion also includes a comprehensive set of command-line options that negate the need for a configuration file in some cases. These command-line options are useful if you want to use motion in a very specific way, possibly in a script or cronjob. In addition to this flexibility, motion includes a special execute option with which you can specify a script or command that can be executed when motion is detected.