2

I am trying to write a function that automatically determines which set of video capture properties are supported by a particular webcam. This is simple to do with v4l2-ctl, but I don't know how to do it cleanly using OpenCV's built-in functions. Using v4l2-ctl, I would invoke:

$ v4l2-ctl --device <webcam> --list-ctrls

which yields a different set of camera controls for my laptop's integrated webcam (/dev/video0) and for whatever USB webcam that I plug in. So far in Python OpenCV, the best I've been able to do is:

def list_supported_capture_properties(cap: cv2.VideoCapture):
    """ List the properties supported by the capture device.
    """
    supported = list()
    for attr in dir(cv2):
        if attr.startswith('CAP_PROP'):
            if cap.get(getattr(cv2, attr)) != -1:
                supported.append(attr)
    return supported

When this function is called, OpenCV prints out a lot of error messages like these:

VIDEOIO ERROR: V4L2: Autofocus is not supported by your device
VIDEOIO ERROR: V4L2: getting property #32 is not supported

If I wrap cap.get in a Python try statement, the videoio errors above are not caught, so it's as if I had no try-except at all. Specializing the except clause to cv2.error as suggested in this answer doesn't work for me. I could reroute output to dev/null as suggested in this answer, but this seems more like a band-aid than a cure to me.

So, here are my two questions:

  1. Is it possible to catch the OpenCV videoio error in Python? How do I do that?

  2. Is there a better way to get only the list of supported capture properties? Would it be better run v4l2-ctl as a subprocess, then process the text in the output to determine the capture properties?

castle-bravo
  • 1,389
  • 15
  • 34

1 Answers1

0

It's a bit late for the answer, but maybe there are others with the same problem.

That you get different properties is a bug of opencv, which is there since many years. The problem is, that there are two different libraries for v4l2-devices ( v4l2 and libv4l2) . Compiling opencv with libv4l2 leads to these errors.

Question 1: the errors you have seen are not produced by python, but by the underlying opencv c++ code . It seems that hey are directly written to stdout.

Question 2: when you have opencv compiled without libv4l2, then your code should give the exact same list as v4l2-ctl. When working with the corrupted opencv-version You can not get neither set any of the CAP_PROP parameters.

Easy_Israel
  • 841
  • 6
  • 8