Yet Another Stupid Webcam

(Edit: This has been abandoned for now since I never really got it working as well as I’d liked.)

What am I doing in the office on a Sunday?

Not working, actually. Instead I’ve been setting up a webcam in my office. The view is not of the office itself, which would be extremely vain and boring, but of the Kensington area across the river. You’d be able to see where I live were it not for the apartment towers being in the way.

I still have to prettify the interface, but for now you can just select the last file listed in the folder. There is no specific refresh time on it; it’s set up to update whenever the image changes by a certain amount. I still have to experiment with what that threshold should be a bit, so it might not update often enough or too often for your liking for now.

Read on only if you’re curious about the technical details.

There’s plenty of webcam software out there, but I had a couple special requirements. First, I wanted to be able to capture the images and analyze them before serving them, to apply the ‘is-different-enough’ algorithm to determine whether it’s worth serving or not. Also, the images could not be served directly, since the machine the camera is attached to is behind the company firewall and they’re not about to open ports just to allow this webcam through.

So, first I needed to be able to capture the images. Again, there’s plenty of software, but the vast majority of it was GUI-driven interfaces where you push buttons to take a snapshot, start capturing video, etc. I needed something a bit more automated and I would have preferred a simple command-line program that would just take a single picture and exit. I’ve been unable to find something like that yet, and looking at the Windows video capture APIs made my head hurt, so eventually I settled on WebCam2000. It has a GUI, but runs pretty much automatically and acts as a mini-web-server, so anytime I want to take a snapshot I can just ‘wget’ one off its server.

Next I have to analyze the images. This is actually pretty simple: I keep a ‘reference’ image, grab a new image, and compare the two pixel by pixel. For each colour component I take the difference between the new image and the old image and square it, to increase the importance of big differences and reduce the effect of noise. All of these get added up into one total sum, which is then compared against a threshold. If the sum exceeds the threshold, the picture is considered ‘different enough’ and it gets uploaded to the server and becomes the new reference image.

The new image also gets fixed up a bit; the capture program generates BMP files, but with an incorrect length in one of the header fields, so I have to run it through a separate program to fix that, and then convert it to JPEG.

And finally, since I can’t serve them off the webcam machine directly, any new images are copied to this web server via ‘scp’.

All of this is handled by a shell script that just runs in a loop repeating all these steps. Unfortunately the whole process is a bit slow and can only sample 1 or 2 frames per second. Good enough for general usage, but a bit lacking if I want to capture something like lightning.

Leave a Reply

Your email address will not be published. Required fields are marked *