Sunday, 10 January 2016

Why webcam autofocus in birdboxes is sometimes bad

The 2014 bird box has a Raspberry Pi + Microsoft lifecam cinema usb webcam in it that has been faultlessly streaming video since being setup. I had to switch all cameras off over the Summer & Autumn for building work, and on reactivating them... I have discovered a design flaw.

My design has a glass partition between camera / electronics and area for the birds.  Over the Summer this has got filthy - I blame my builders!  The dirt means that the camera now 'sees' the window and focuses on it rather than any in-box activity.  An example from last week (Jan 6 2016),  shows the problem:

While I don't resent the odd insect, I don't want this sort of thing:

There is an obvious solution - CLEAN THE GLASS!
...which would be great if I could locate the keys for my securely locked ladder :(
Plan B is to turn the auto focus OFF and fix the focus to a specific focal distance

Introducing uvcdynctrl
This is a handy application that gives control over your webcam's settings.  Apparently the one I'm using (Microsoft Lifecam cinema) is notorious for a skittish auto focus.

Since I'm running this on a Raspberry pi, to install do:
sudo apt-get install uvcdynctrl

To list the controls available to you do:
uvcdynctrl -c

I get the following (output depends on webcam model):
xx@xx ~ $ uvcdynctrl -c
Listing available controls for device video0:
  White Balance Temperature, Auto
  Power Line Frequency
  White Balance Temperature
  Backlight Compensation
  Exposure, Auto
  Exposure (Absolute)
  Pan (Absolute)
  Tilt (Absolute)
  Focus (absolute)
  Focus, Auto
  Zoom, Absolute

Now, obviously I cant control all of these, there's no pan and tilt controls available to me, however I can change the autofocus behaviour.
To turn OFF that pesky autofocus:
uvcdynctrl -v -d video0 --set='Focus, Auto' 0

Auto focus is OFF... just need to manually set the auto focus until it's in the correct place:
uvcdynctrl -v -d video0 --set='Focus (absolute)' 10
The number at the end is the focal distance - just needs playing with to hit the correct focal point

You can 'get' i.e., query what the focus setting is at  as follows:
uvcdynctrl -d video0 -g "Focus (absolute)"

So auto focus is off for now... just need to find my keys /access ladder, clean/remove the separating glass  and I can sort it properly, then switch auto focus back on with:
uvcdynctrl -v -d video0 --set='Focus, Auto' 1


  1. Its great that you can "manually" adjust the focus of this camera via software. I'd be inclined to set it and reset it to suit the current stage (e.g. exploration, nest building, eggs in the nest & so on) rather than let it focus itself.

    Also good to see you have early interest. I haven't put my 2 Pi boxes up yet. I was waiting for the rainy season to come to an end! I noticed last year that angling/inclining the boxes resulted in more water ingress than usual. As water runs down the box on the angled surface, gravity helps the moisture find its way in via the interface between panels. Not enough moisture to worry the birds, but I may need to take some action.

    Good luck for the 2016 season!

  2. My only angled box has been up now without water-related issue for two seasons - I think that I habitually over engineer....