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:
Is it possible to catch the OpenCV videoio error in Python? How do I do that?
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?