Webcam doesn't work anymore with OpenCV 4.x

I am happily using Python and OpenCV for several projects, and, among the others, I collect images from three different webcam mounted on three different microscopes. Everithing was fine with Python 2.7 and OpenCV 2.x. Now I have to move to Python 3, which support only OpenCV 3.x or higher, but if I try the usual:

cap = cv.VideoCapture(0)
ret, frame = cap.read()

frame now is returned as None. It should not be a matter of pc or drivers: I can reinstall old Python and OpenCV version and the capture from the webcam works again. I also tried with different computers, different Windows versions (7 and 10), different OpenCV version, but it really seems that some new feature in an OpenCV version made it incompatible. Summarizing:

Python 2.7 with OpenCV 2.x: ok
Python 2.7 OpenCV 3.1: ok (even if I don’t know how it was installed, considering Python 2.7 should not support OpenCV 3.x)
Python 3.x with OpenCV 3.x or 4.x: ko.

The only hint I found on the web is about a possible different way to handle hardware acceleration by the new version of OpenCV, but I wasn’t able to go further.

Any idea?

but it wont come back …

since you seem to be on windows, try:

VideoCapture(0, c2.CAP_DSHOW)

(more “modern” ocv is using CAP_MSMF by default, which may not work for you)

Assuming you mean cv2.CAP_DSHOW… it works!

Can you comment a bit more what CAP_DSHOW and CAP_MSMF are?

Thank you a lot.

sorry broken key here :wink:

different backends / implementations of the videocapture code.
some years ago, ms threatened to phase out dshow in favour of their newly fangled media framework, so the opencv devs gave that higher priority