Another round of webcam

in technology

Cat meme

Shut up. Shut. Up. I’m happy with the time-lapse solution, and its control mechanism. What I want to do now something slightly different: Single images. On my webserver. Right now I have a system that puts up a new capture every ~10 seconds or so on as webcam1-current.jpg, along with a semi-permanent Dropbox-hosted archive. It feels deficient. I don’t like webcam’s inbuilt handling system for capturing and transferring files-it feels arbitrary and limited-so here I am going to explore a script-friendly alternative/replacement.

Let us…begin!

I have access to three webcams on my system, one internal and two external USB devices.

  1. /dev/video0 – USB-bus internal 300k pixel (640×480).
  2. /dev/video1 – USB-bus external 1.3 megapixel (1280×1024).
  3. /dev/video2- USB-bus external 1.3 megapixel (1280×1024).

In honour of their origins, I treat them as webcam0, webcam1 and webcam2 respectively. My Netbook can handle running two webcams simultaneously; errors out if I try to add a third. That necessarily isn’t a problem unless I am theoretically feeling sneaky and leave my laptop open while I go to Subway so I can capture covert photographs of people looking at my overtly captured stream from 091 Labs.

Theoretically, of course.

Cantrips and invocations.

Currently everything is very simply set through webcam’s configuration file. If I want to do something different, I call a different configuration file. This isn’t ideal because webcam is mercurial. About 40% of the time I get this:

libv4l2: error converting / decoding frame data: v4l-convert: error parsing JPEG header: Not a JPG file?

Example terminal output

At this point I have to unplug the webcam and plug it back in before it’ll play nice again, so at the current moment in time a script/program that is launching and closing webcam repeatedly isn’t going to. There’s a long and involved solution that requires me to research the libv4l2 error and implement any fix I might come across…but I’m researching that in parallel to this story. For the purpose of a Mark blog post I’m going to do the lazy Mark thing and simply capture a stream of full-sized, 1280x1024px images to my Dropbox and work from there with Imagemagick and SCP.

I’m going to create a new config for webcam1 that will:

  1. Capture a 1280x1024px image once every 60 seconds.
  2. Put it into a Dropbox archive.
  3. There is no part 3.

Nefarious scheming:

I’ll wrapper this in a script that will:

  1. Take the most recent image, resize it to a more web-comfortable 640×480, and upload it to my server for immediate consumption, day or night.
  2. Manage the Dropbox archive: Purge it once every 24 hours at midday by deleting the images entirely.

Next up, teh wrapper script(s?):

Easy, eh? Plug your webcam in, run webcam ./path/to/webcam/config, add capture to run once per minute via a crontab entry:

0-59 * * * * mark /usr/local/bin/capture

So what the above config file and script do in combination is, as stated, capture one image per minute to Dropbox (so even if the computer is stolen, we still have the images online), and crop and upload that image to a web-accessible server for fast preview/embedding (I could just use Dropbox, I guess). However, this kind of big archive leaves a whole lot of files piling up in a folder, so once per day we need to clean the cache out:

* 12 * * * mark rm /path/to/archive/webcam*jpg


Six Months in the Mountain Kingdom

in me

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