Monday, April 22, 2013

Manually Focusing a Logitech Pro 9000 on Ubuntu or Raspbian with Python

The Logitech Pro 9000 is a cute little plug-n-play webcam that will work with Linux systems (in my case, Ubuntu on my Giada and Raspbian on my Raspberry Pi). Below are command line instructions to set it up and use the webcam.

To set it up, open a terminal and simply run from command line:
$sudo apt-get install fswebcam

Then, you should search to see where the camera hardware is: 
$ls /dev

Once you find where your device is, you can now call your camera from the proper directory to capture and save an image with (NOTE: every time you run this code, you write over the same jpg file): 
$fswebcam -d /dev/video0 "image.jpg"

To be safe, check to see if your image was saved with: 
$ls

If you are unable to click and open your image to view it, I have been using the default Eye of Gnome image viewer: 
$eog image.jpg


In my efforts to test the Logitech webcam for my engineering design project, I ran into the issue of getting my webcam to focus at short focal lengths. My test subject placed inches away from the camera lens was super blurry but things at infinite distance were focused. Took me some time, but I finally figured out how to control the focus of the webcam. 

If you have been Googling "Logitech Pro 9000 Linux Focus" or anything like that, don't even bother trying to follow any tutorials that direct you to the QuickCamTeam website. It's shut down and you can't get their logitech.xml file. Fuck it. 

Download the Video4Linux Utilities library with: 
$sudo apt-get install v4l-utils

With Video4Linux, you will be able to manually control your webcam. I needed to turn off the auto focus on mine and set it to a fixed focus length. So where should you start?

To see a list of all the common options:
$v4l2-ctl --help

To see a list of things you can control on the webcam:
$v4l2-ctl --list-ctrls

To alter your webcam controls, your command should look like:
$v4l2-ctl --set-ctrl=<ctrl>=<value>

I needed to play around with my camera's ability to focus. To set it to autofocus, I used:
$v4l2-ctl --set-ctrl=focus=0

But that's not what I need my camera to do for my project since the device will be at a set distance from its subject. So, I can use any value from 0 to 255 to set my focus. I used a value of 200, which makes my camera focus clearly on objects 2 inches away.


If you want to call these bash command lines from Python code:
import os
os.system('v4l2-ctl --set-ctrl=<ctrl>=<value>')
os.system('fswebcam -d /dev/video0 image.jpg')


If you have SimpleCV installed and would like to display your camera for more adjusting purposes:
import os
from SimpleCV import *
os.system('v4l2-ctl --set-ctrl=<ctrl>=<value>')
cam = Camera()
disp = Display()
while disp.isNotDone()
  img = cam.getImage()
  img.save(disp)

No comments:

Post a Comment