OpenCV: How to read autodetected api and format of Videocapture

Viewed 2562

I set the resolution to full hd (width=1920, height=1080) on my c920 webcam and used the cv2.CAP_DSHOW API which supports this camera.

However, the fps was very low. The next thing i tried is to autodetect the API by simply writing

camera = cv2.VideoCapture(0, cv2.CAP_ANY)

Now I get very high fps but it is not a 16:9 format anymore, it has some black side bars on the left and right side of the image like in this post: OpenCV capturing imagem with black side bars

Is there are way to:

  1. Find out which API has been autodetected?
  2. Find out which format is being used?
  3. Can I change the format afterwards manually without losing much of the gained performance?

Or do you have a better idea how to handle this problem?

Printing camera says it's still full hd:

(grabbed, frame0) = camera.read()
print(len(frame0[0])) # = 1920
print(len(frame0)) # = 1080
1 Answers

Multiple Devices (Webcams) Connected? A brief question before I answer do you have another webcam connected? I.e. an integrated laptop webcam, additionally to the c920? At the bottom of this answer you find a function to check all available devices that openCV recognizes.

Answer 1:
Yes there is getBackendName() to check what openCV automatically detected. See the snippet below (Link to the docs):

video = cv2.VideoCapture(1, cv2.CAP_ANY)
print(video.getBackendName())

Answer 2 & 3:
You can also check the current format with .get() and insert the property of interest. See again the snippet below (as well as the docs). In a similar fashin you can set the format with the .set() method (see also below)

video = cv2.VideoCapture(1, cv2.CAP_ANY)
print(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
# Regardless of the auto selected API you can change the format again with the .set method
video.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
video.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)

Check Available Devices
Here would be a quick function that gives you the indicies of all available devices

def get_available_devices():
    index = 0
    arr = []
    while True:
        cap = cv2.VideoCapture(index)
        if not cap.read()[0]:
            break
        else:
            arr.append(index)
        cap.release()
        index += 1
    return arr
Related