First lets have a look at what the cam actually displays (good view right?). This is useful to see what the traffic conditions are like on the expressway as this is one of the major routes in/out of the city centre.
I set up this cam to capture an image every 5 minutes, so it's not a live stream. The 5 minute interval is configurable with the use of cron, no messy timing inside the script I wrote!
So down to business now. The cam is a Logitech c270. I went for this cam because it's been documented to work and it was cheap. I did a bit of research and found out that as long as the cam is UVC compatible, it should 'just work' in OS X. That being said my other cam (Microsoft HD-3000) also 'just works' but the picture from it is terrible without optimal lighting conditions. The c270 captures a much better quality picture even in the dark.
I set up the cam in my window like this (I have floor-to-ceiling windows in this particular room):
A little later I added a cable guide and marked out where the cam sits in case it gets moved while dusting, etc.
Now, down to the most important aspect of this mini-project: the software to make this all work. To my surprise (?!) OS X does not come with any command line utilities to capture webcam video, so I had to install a couple of things.
First thing was to get Xcode installed from the Apple Developer site. In my case I had to have version 4.6.3 because my server is still running OS X Lion. The Xcode Command Line tools are also required to be installed, these are found on the site site as Xcode.
Then, it was time to install Home Brew via the following commands. The doctor command needs to be run after installation. Do all this via the Terminal.app.
Next I installed Image Magick because I knew that I will be doing some image manipulation of the webcam images before they are uploaded. This was the whole reason for installing Home Brew in the first place. Image Magick is installed like this:
It MUST be built from source, the pre-compiled binaries just DO NOT work due to some static linking dependency issues. I also noticed that the fonts do not work after installation, luckily there is a script that fixes this. The script is located here: http://www.imagemagi … pts/imagick_type_gen. I saved this script as i.pl and ran it like so:
Now Image Magick will work with fonts. However, we still need more software. I downloaded the following additional applications:
Now we are ready to cook with fire! After a bit of playing around, I created the following script to capture, archive, manipulate and upload the webcam image. (Real server details for scp are not shown of course)
So what does this script do? Lets have a look. First it sets up all the paths and creates the archive directory if necessary. Then I use wacaw to capture an image from the webcam (device 2 in my case and I skip 3 frames before the final frame from the camera is captured - this gives the cam some time to warm up). The output is saved in the PNG format because I found that wacaw JPEG output quality was quite bad.
Next I convert the PNG image into a JPEG using Image Magick Convert utility, archive the original (converted JPEG) and then sharpen the image. Sharpening was necessary for night-time capture especially, without it I could not see the tail lights of the cars on the road.
After the image is sharpened, I add some copyright and date information text to it. The text is added over translucent boxes that are drawn on the image first, this makes it easier to read. The stamped image is then optimised for web use using ImageOptim.
The final step is to upload the image, and that's done via scp. What about passwords? I set up public key authentication between my home server and the web host, so there is no need to enter a password every time.
The script above was saved as '/Users/ikromin/Downloads/wcup.sh', then I set up a cron job via crontab -e. This is what I put in the crontab to run my script every 5 minutes.
This all took a bit of work, but it was fun to set it up and some people actually started to use this now, so it's been a useful project.
The only thing that I'm not quite happy with is the inability to capture images in HD using wacaw. I'm going to look around and see what I can do with respect to addressing that in the future.