Star in Your Own Reality TV Show

Install and use common USB webcams under Linux.

Webcams have a variety of uses, from video conferencing to surveillance to just showing your friends what you are doing at the moment. Linux support for webcams, particularly USB webcams, is pretty good, provided you know where to look. Once the webcam is installed, there are a lot of programs available to take quick snapshots, automatically upload images to a server periodically, and even turn your webcam into a motion detector. In this hack, I cover the general steps you need to install a webcam under Linux, and highlight some of the popular Linux webcam applications.

Install the Webcam

The first and most difficult step to installing a webcam is figuring out which Linux module it uses. There are a number of webcam modules built into the standard kernel series, but there are also quite a few that aren’t, so the sort of detective work you need to do will vary depending upon your webcam. Before you do too much detective work though, just plug in your webcam and check the output of dmesg to see if your system automatically recognizes your webcam. Here is some sample dmesg output showing a webcam being recognized.

	$ dmesg
	…
	usb 1-1: new full speed USB device using uhci_hcd and address 3
	Linux video capture interface: v1.00 
	/home/greenfly/src/spca5xx-20050701/drivers/usb/spca5xx.c: USB SPCA5xx
	camera found. Type Labtec Webcam (unknown CCD)
	/home/greenfly/src/spca5xx-20050701/drivers/usb/spca5xx.c: [spca5xx_probe: 
	8652] Camera type GBGR 
	usbcore: registered new driver spca5xx
	/home/greenfly/src/spca5xx-20050701/drivers/usb/spca5xx.c: spca5xx driver
	00.57.00 registered
	/home/greenfly/src/spca5xx-20050701/drivers/usb/spca5xx.c: VIDIOCMCAPTURE:
	invalid format (7)

In my case, the above message didn’t appear automatically because I wasn’t lucky enough to have support for my webcam directly in the kernel. If your system doesn’t automatically find your webcam, you will have some work to do.

The steps vary depending on the driver, but the basic steps are to locate your style of webcam, download the module for your device, extract the source code, and build the module according to the instructions. The Webcam HOWTO at http://www.tldp.org/HOWTO/Webcam-HOWTO provides a pretty thorough listing of webcam drivers and the devices they support, so go there first and find your device in the list. You will need kernel headers to build this module, so if you didn’t build your own kernel you will need to install the kernel headers package for your distribution’s kernel.

After the webcam module is installed, plug in your webcam if it wasn’t plugged in already, and check dmesg again to see if it was recognized. You might need to manually load your webcam module as root before it gets recognized. In the case of my spca5xx webcam module I would type:

	# modprobe spca5xx

Webcams use the video4linux support in the kernel and will register with a /dev/videoX device starting with /dev/video0, so if you plug in more than one webcam or have more than one video4linux device installed either look through dmesg for information on which device was assigned to which webcam or use a tool like xawtv to scan for the device:

	$ xawtv -hwscan
	This is xawtv-3.94, running on Linux/i686 (2.6.10)
	looking for available devices
	port 56-56
		type : Xvideo, image scaler
		name : Intel(R) 830M/845G/852GM/855GM/865G Video Overlay
		
	/dev/video0: OK				 [ -device /dev/video0 ]
	type : v4l
		name : Labtec Webcam
		flags: capture

Tip

Because webcams use /dev/video, just as with TV Tuners and general frame grabbers, you can also use webcam software with a camcorder that is attached to your hardware frame grabber.

Use Your Webcam

With the webcam installed, it’s time to actually test and use it. One of the easiest GUI webcam tools to use is gqcam, which is probably already packaged by your Linux distribution, so look for it in your package installation tool. If not, download it from the official page at http://cse.unl.edu/~cluening/gqcam and compile and install it according to the installation instructions.

Start Gqcam from your desktop menu or type gqcam in a terminal. Gqcam opens and displays video it gets from the first video device. The interface is pretty basic and provides you with the general controls that you need to adjust brightness, contrast, hue, and other video settings. You can also control whether the video is full sized or a fraction of the highest resolution. To grab a frame from the webcam, click Snap Picture, and enter a filename to save the image. Click Freeze Picture to stop at the current video frame.

A popular application for webcams is to periodically snap pictures and upload them to a site. Although you could do this manually, the appropriately named webcam program seeks to automate the task. webcam is part of the xawtv package [Hack #74] discusses. webcam’s design makes it easy to run from a cron job. It takes no arguments on the command line other than an optional configuration file to load, otherwise all configuration is done in the ~/.webcamrc file.

The .webcamrc file is split into two sections, grab and ftp. The grab section has all of the settings for image capture. Here is a sample grab section from a .webcamrc file:

	[grab]
	device = /dev/video0
	text = "webcam %Y-%m-%d %H:%M:%S"
	fg_red = 255
	fg_green = 255
	fg_blue = 255
	width = 320
	height = 240
	delay = 3
	wait = 0
	rotate = 0
	top = 0
	left = 0
	bottom = -1
	right = -1
	quality = 75
	trigger = 0
	once = 0

The configuration file options are pretty self-explanatory and are documented completely in the man page, but here are a few interesting options. The text parameter defines what text will be overlaid onto the image. In this case each image would say the word “webcam” followed by the timestamp. The fg_red, fg_green, and fg_blue options control the colors for the text overlay (in this example, white). The delay option specifies the delay in seconds between image grabs, so adjust this value depending on how often you want to take pictures. If the trigger option is set to something other than zero, webcam will compare the current image with the last uploaded image and only upload the new image if the difference is greater than the value of trigger.

The ftp section of .webcamrc contains all of the settings for the upload server. Here is a sample ftp section:

	[ftp]
	host = www.example.com
	user = username
	pass = password
	dir = public_html/images
	file = webcam.jpeg
	tmp = uploading.jpeg
	passive = 1
	debug = 0
	auto = 0
	local = 0
	ssh = 0

These options are also pretty self-explanatory. Enter the account information for the ftp account on your server. webcam also supports SSHuploads as an alternative, provided you already have passwordless SSH keys set up, just set ssh equal to 1.

To test webcam locally, set the local variable to 1. You might want to modify the dir variable as well, so that it will copy the image to an existing local directory. Then start webcam. Here’s example output from webcam set to put files under /tmp for testing:

	$ webcam
	reading config file: /home/greenfly/.webcamrc
	video4linux webcam v1.5 - (c) 1998-2002 Gerd Knorr
	grabber config:

		size 320x240 [24 bit TrueColor (LE: bgr)]
		input (null), norm (null), jpeg quality 75
		rotate=0, top=0, left=0, bottom=240, right=320

	write config [ftp]:
		local transfer /tmp//uploading.jpeg => /tmp//webcam.jpeg

Once you have confirmed webcam is grabbing images correctly, change the local and dir variables back to their original settings and restart webcam so it will start uploading images to the remote server.

Tip

One easy way to constantly take webcam images is through cron. Since you only want webcam to be run once, set the once variable in .webcamrc to 1 and then edit your user’s crontab with:

	$ crontab–e

Here’s an example crontab entry to run webcam every two minutes:

	*/2 * * * * webcam > /dev/null
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset