Home > Back-end >  cv2.VideoCapture(0, cv2.DSHOW) returns none
cv2.VideoCapture(0, cv2.DSHOW) returns none

Time:10-14

I'm trying to capture video from an in-build webcam on a laptop (or external USB camera) using opencv, specifically VideoCapture with the DSHOW argument. I know there is a way to set the resolution and even FPS, however the DirectShow argument for the API returns none when I included it in the code.

For example;

# returns my webcam's stream, but all optional arguments are ignored
camera = cv2.VideoCapture(0)
camera = cv2.VideoCapture(0, cv2.CAP_V4L2)

# returns none and loops infinitely or errors out when *if im.any()*
camera = cv2.VideoCapture(0, cv2.CAP_DSHOW)

This is the code that follows after the above;

# should set resolution, settings are always ignored
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

while(True):
    retval, im = camera.read()
    if im.any(): # errors out when image is none
        cv2.imshow("image", im)

    k = cv2.waitKey(33)
    if k==27: # Esc key press
        print('Resolution: {0}x and {1}y'.format(im.shape[1],im.shape[0]))
        print('FPS: {0}'.format(camera.get(cv2.CAP_PROP_FPS)))
        break

camera.release()
cv2.destroyAllWindows()

Is the DSHOW the correct API to use and is it the only API to use that can change resolution and FPS of a camera stream using opencv? Or is there something else I'm doing incorrectly?

More details about the system.

  • Ubuntu 18.04.6
  • python 3.9.5
  • opencv-python 4.5.2.52

Thank you in advance for the help!

Regards, Tiz

CodePudding user response:

DSHOW (and MSMF) are windows only.

on linux, use V4L, FFMPEG or GSTREAMER

also, please check the return val of capture.set(), not all properties/values will be supported on any given machine

  • Related