Monday, December 17, 2012

Webcam Auto-Exposure Problems Under Linux - Solved

Too much light on bright days
I have struggled to find a webcam program which could grab decent still images from a webcam running under Linux. I have tried the common suggestions found online, used gvucview, cheese, webcam and webcamd.

All of these programs worked for snapping still images but the auto-exposure never worked properly and the images were poor. Most of the day the picture was either dark or obliterated with glare.

It seems the problem is that most webcam software is designed for streaming video. They all do a decent job of this, but even those programs that build in a delay never worked consistently for single jpg images. Searching the web, it is obvious I am not the only one with this problem and I could find no real solution.

Too dark in low light
After fighting this problem for days, it occurred to
me that perhaps I was using the wrong tool for the
job. I needed to look for a program which was designed specifically to snap still images from a webcam, not capture streaming video.

After some searching, I found a program called uvccapture which does just that. It is a simple, light program  that accepts arguments for resolution, timing, save location, even brightness, saturation, contrast and other basic settings. It is available in the Ubuntu repositories or you can grab it from here: https://github.com/csete/uvccapture

Happily, the auto-exposure is now working and my webcam is finally producing clear and properly exposed images. Here's how I managed it:

Auto-Exposure working properly
After installing uvccapture, I created a folder called 'webcam' in my home directory to store image.jpg and scripts. I set up the program to load on the boot using the Startup Applications tool by entering this into the command field:
uvccapture -m -t600 -q75 -x640 -y480 -o/home/user/webcam/image.jpg
This string of commands loads the program and instructs it to refresh the image every 10 minutes (600 seconds), to use compression on the JPG file at 75% quality. It states the webcam resolution, which must be supported by your device. The last argument includes the location to save the image file.

More info and commands for uvccapture are located here: http://manpages.ubuntu.com/manpages/natty/man1/uvccapture.1.html

The next problem was finding a new program to send the files via ftp to a remote server. Most of the programs I had used included the ftp service with the image grabber. I wanted something light and stable which could run in the background.

I looked at several programs and scripts and finally decided on this simple script which runs every 10 minutes via cron. Copy and paste the following into a text editor. Edit lines 4, 5 & 6 in the top section and line 5 in the bottom section:
# vi ftp-run.sh
#! /bin/sh
#run this file with ./ftp-run.sh
REMOTE='your-remote-server.com'
USER='username'
PASSWORD='password'
FTPLOG='/tmp/ftplog'
date >> $FTPLOG

ftp -n $REMOTE <<_FTP>>$FTPLOG
quote USER $USER
quote PASS $PASSWORD
bin
/home/user/webcam
mput image.jpg
quit
_FTP
Save the file as ftp-run.sh in the webcam folder. Right click on the icon and under Permissions, click to 'Run this file as a program'. I set up a cron job to run this script every 10 minutes with this command:
crontab -e
    arrow down to bottom

Paste:
*/10 * * * * cd webcam && ./ftp-run.sh

Ctrl + x to exit
Y to save
Enter
I rebooted to load the program and settings. I finally have a working webcam and my images are clear, bright and upload reliably to my server. I hope this how-to will help someone else who is struggling with this problem.

Shameless Plug:
More fun with your webcam: PortalView Live Desktop Wallpaper for Linux

No comments:

Post a Comment