Automating Picture Capture Using Webcams on Linux/Ubuntu

Ever want to turn a laptop into a webcam surveillance monitoring tool, or use a USB webcam to take pictures every 5 minutes to record a timelapse video? Or maybe you just want to monitor your room remotely and on demand? Here's a good weekend (or day) project:

Installing Linux/Ubuntu on a laptop computer

Setting up SSH so I could remotely connect to the computer

Make sure Linux detects the webcam

Configuring crontab (creating a cronjob) to run every 5 minutes or hourly

Viewing the pictures in a web browser

Installing Linux (Ubuntu) on my laptop

Over the weekend I decided to turn one of my old laptops into a Linux server. My old netbook, the ASUS 1201N was a perfect candidate since netbooks are designed to run with extremely low power consumption. Though the AC adapter for the ASUS 1201N is rated to output a maximum of 40 watts, I used my handy dandy Kill-O-Watt meter to measure the real power usage. Turns out it's typically only 20W at idle, and around 25W under load. While this isn't as ideal as a Raspberry Pi (which can run on only 5W) the difference in cost isn't too crazy.

So I am happily paying $1.28 per month in electricity to have my very own Linux server running 24/7, complete with a 1.60 GHz dual core Intel Atom N330 CPU and 2 GB of memory. Quite a bit more than the Raspberry Pi, which can be helpful for doing video and image encoding.

How do you install Ubuntu? You just go to the Ubuntu website and download it. I actually used the Windows bootable USB utility which made the process pretty simple. Note that the desktop version of Ubuntu does not come (by default) with an SSH server (more on that below), but its pretty easy to set up. The server version of Ubuntu can come with an SSH server if you select it during the install process.

Installing SSH and Configuring SSH Open Server

If you want to be able to connect to your new Ubuntu machine remotely, you'll need to install and have an SSH server running. You can do this if you have the desktop version of Ubuntu too. (I downloaded the desktop version). It's pretty simple to get an SSH server:

I changed the default port number from 22 to a random higher number to make it a little harder for random people out on the internet to try to log in. (They would still have to guess the username and password anyways, but this helps too). Speaking of which, if you're behind a router (like if you live at home), you'll need to set up port forwarding so you can log in remotely. Some more information about port forwarding can be found here.

How does Linux detect your webcam?

Well if it's a USB webcam, plug it in. If it's an integrated webcam built into the laptop, there's nothing to plug in. Ubuntu should automatically detect and install drivers for the webcam. To see if it's detected, let's run some commands:

More information on lsusb can be found by reading the lsusb 'man page' (otherwise known as the manual). You can also use the lsusb command to learn more about the resolution of the webcams. Just change the Bus and Device numbers that you found above.

If for some reason you plug in the webcam but it doesn't show up (in /dev/video* or lsusb), try looking at the recent driver messages for clues:

stephen@ubuntu:~$ dmesg|tail

fswebcam, ffmpeg, MPlayer, and VLC

There are a few command line tools that will let you take a picture using your webcam. I've tried three different tools and found that I liked fswebcam the most, but I've listed all of the options here:

Note that you might need to change your device from /dev/video0 to perhaps /dev/video1! Check the above section to see what webcam is detected.

VLC

Some notes about the Logitech Quickcam Pro 9000

If you are using the [Logitech Quickcam Pro 9000](http://www.amazon.com/dp/B000RZQZM0/ref=as_li_ss_til?tag=netinstr-20") it has an advertised maximum resolution of 1600x1200. Let's try to run that with fswebcam.

Configure crontab (make a cronjob) to take a picture every minute or hour

Crontab is a popular *nix utility that executes a command on a user defined interval. Maybe you just want to take a picture every minute, or maybe you want to shutdown your computer Monday through Friday at 10pm. Or maybe you want to run some scripts that backup your data once every 3 months. If you want to run multiple commands you can do so by chaining them with the && keyword, but it's also sometimes worth making a bash script (or maybe a simple Python/Perl/Ruby script) that gets executed as part of the cronjob.

To view the current cron jobs for the current user, type crontab -e. To view the current cron jobs for the super user, type sudo crontab -e.

If you want to view any logs for the the cron job you can view the logs by typing:

$ grep CRON /var/log/syslog

Viewing/Transferring the Pictures

Okay, so you found a command line utility that takes pictures, and perhaps a cronjob that runs that command every 5 minutes or 10 minutes or every hour or once a day, but how do you look at the picture?

There are a couple of ways of doing this. If you're using the desktop version of Ubuntu (with a nice graphical user interface) you just double click on the photo. For the rest of us who are SSH'ing in to a remote machine or are using the server version of Ubuntu or some other Linx distro, we have a few options:

FileZilla to grab the files and transfer them to our local machines

If you have a web server (Apache, ngnix, or something else) on the server, move the file to the web directory

SCP the file to a remote web server. For example, I have a few websites (such as this one) hosted by Dreamhost, and they provide shell access

The command to securely transfer a file on one machine to another looks like this: