Use a webcam from the terminal

Aug 18, '02 12:59:41PM

Contributed by: victory

The goal was to figure out a way to capture images from a USB-webcam from the terminal. If all you want a simple point-and-click webcam app that runs on a host machine all the time, there are several very nice free and commercial apps out there (listed in the "References" section at the end of this article). In my case, I just wanted to grab an occasional image of our server room while logged in remotely to an OSX box located there.

There doesn't appear to be any purely command-line driven image capture apps for OSX at this time. At first I'd considered a quick AppleScript hack to launch and control one of the many GUI-based webcam apps from the terminal, but it bothered me that there wasn't a cleaner way to do this. Another possibility was to modify an existing webcam app, removing all the GUI-specific code and turning it into a command-line tool -- not pleasant a prospect, particularly if it meant mutilating someone else's code. Also, the 'just for fun' aspect of the project suspiciously began to sound like real work.

The approach I eventually took involves using a webcam-aware image processing package that happens to command-line component. Read the rest of the article for the how-to...

INGREDIENTS

There are a few problems using this specific camera with the macam driver; see below for details.

CAMERA & DRIVER

It seems that there are very few cheap, off-the-shelf webcams for OSX at the moment. There are several excellent FireWire-based models that work great under OSX, but they were all outside of my price range for this project. Also, since OSX-native driver support is still lacking for many devices, I decided to first find a working OSX webcam driver and see what cameras it supported, rather than the other way around.

Matthias Krauss' macam is an excellent, free USB webcam driver that currently supports about a dozen different cameras which he lists on his site. Armed with the list, I went to a local computer superstore to see what I could find. By now, it should be apparent that this project was more of the 'weekend' variety than one that was methodically planned and researched!

As it happens, the ONLY camera that I could find on the list was the Logitech QuickCam Express. It's a small, ball-shaped camera that sits on a
triangular base. (It resembles the original Connectix QuickCam that early Mac users may remember, except that it now has a USB connection) The macam author warns that Logitech apparently makes several different versions of this product, not all of which work properly with his driver. To further complicate things, there doesn't appear to be a definite way to tell the difference.

I bought the QuickCam Express anyway and it worked fine. If you decide to take the same gamble, you might want to check the store's return policy ahead of time. Here's some info about the unit I got:

Logitech QuickCam Express - UPC code: QC-XPRS US 961183-0403 / 9785501198

A few comments about the macam driver: The current version of macam is 0.6 (as of July 2002), but I'm using a beta version 0.71 which supposedly has better support for the QuickCam Express. Both versions are available for download at the macam site. What I find really neat is that most hardware device drivers traditionally affect an OS at a fairly deep level. The macam driver is written as a QuickTime component. What this means is that the driver installs easily (just copy it to /Library/QuickTime), much like how you'd install an a 3rd party software codec. Equally important, this implies that the driver runs entirely in userspace and doesn't offer the potential risk of crashing your system the way traditional device drivers might.

Once the macam driver is installed, it will work with any app that looks for a standard QuickTime VDIG source. The macam package thoughtfully includes an app that allows you to quickly determine if the camera and driver are working properly. If you only want to use the webcam with GUI-type apps (see References for a list), this is as far as you need to go. However, the goal was to figure out a way to capture an image from the terminal, so we'll next look at VideoScript.

VIDEOSCRIPT
The second important discovery was a package called VideoScript, which is actually a complete language for doing automated image processing. It reminds me of ImageAlchemy on the PC or NIHImage for Classic. The good news is that VideoScript runs under OSX and is able to use the VDIG input from a webcam. The main VS package runs as a GUI-based app that opens a text editor much like other development environments. Yet the real trick is a client version of VideoScript that will run scripts files from the command-line. The client version is still in beta and can be downloaded here. There are actually several versions of VideoScript offered: A free 'lite' version which I'm using, and a full-featured commercial 'pro' version. Actually, VideoScript does a lot much more than the simple image captures described here. I encourage anyone interested to take a look at the company's site which has downloadable documentation and an online language reference. There's an interesting example of how to use VideoScript to write a motion-detection routine for surveillance cameras.

Here are the VideoScript commands to take a picture from the webcam and save it somewhere:
set v to videosource;
set i to frame of v;
set window "view" to i;
set file "OSXRoot:Users:myname:Sites:webcam.jpeg" to i;
One odd feature is that even under OSX, VideoScript still uses traditional Mac-style pathnames like 'MyHD:subdir1:subdir2:file' instead of '/Volumes/MyHD/subdir1/subdir2/file'.

You can enter and run these instructions right from the main window of the VideoScript app or you can save them text file that and tell the command-line client to run it from the terminal. Thus, if you saved the preceding code snippet to a file called watchit.vs, from the
terminal you could:
videoscript -f watchit.vs
which would grab a picture and save it to /Users/myname/Sites/webcam.jpeg. If you had webserving enabled on this machine, you could even view the captured image elsewhere from a browser (i.e 'http://my-webcam-address/~myname/webcam.jpeg'). Naturally, this works equally well from a local terminal window or from a remote session via SSH.

OTHER NOTES:REFERENCES AND LINKS

Comments (5)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2002081812594192