Skip to content Skip to sidebar Skip to footer

Disable Webcam's Autofocus In Linux

I'm working in embebed system in the beagleboard. The source code is in Python, but I import libraries from OpenCV to do image processing. Actually, I'm using the webcam Logitech c

Solution 1:

Use program v4l2-ctl from your shell to control hardware settings on your webcam. To turn off autofocus just do:

v4l2-ctl -c focus_auto=0

You can list all possible controls with:

v4l2-ctl -l

The commands default to your first Video4Linux device, i.e. /dev/video0. If you got more than one webcam plugged in, use -d switch to select your target device.


Installing v4l-utils

Easiest way to install the utility is using your package manager, e.g. on Ubuntu or other Debian-based systems try:

apt-get install v4l-utils

or on Fedora, CentOS and other RPM-based distros use:

yum install v4l-utils

Solution 2:

You can also do it in Linux with:

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_AUTOFOCUS, 0)

For some people this doesn't work in Windows (see Disable webcam's autofocus in Windows using opencv-python). In my system it does (ubuntu 14.04, V4L 2.0.2, opencv 3.4.3 ,logitech c922).

Solution 3:

In case anyone finds is useful, I've come up with a small snippet that can be added to your .bashrc. It will detect which of your webcams have autofocus and then disable it.

Prerequisite: v4l-utils

# Get a space-separated list of all the video devices available
webcams=$(ls -pd /dev/* | grep video | tr'\n'' ')
for webcam in$webcams; do# Check if the tested video device features autofocus
  no_autofocus=$(v4l2-ctl --device=$webcam --all | grep focus)
  # If it does, disable itif [ -n "${no_autofocus}" ];
  then
      v4l2-ctl --device=$webcam --set-ctrl=focus_auto=0
  fidone

This snippet does certain assumptions:

  • You want to disable autofocus on all the video devices featuring it

Post a Comment for "Disable Webcam's Autofocus In Linux"