USB webcam not working with OpenCV

Hello everyone

I have a simple python script that is supposed to take a single image with cameras with the hardware IDs that I specify (e.g. 0,1,2,3). It works with my laptop’s integrated webcam as well as an emulated camera (being a smartphone pretending to be a camera through DroidCam). However, upon plugging in the actual camera I want to use, a HP 965 4K USB webcam, the script throws this error for the camera at ID 1 (suspected to be the HP, as unplugging this fixes the issue):

[ WARN:0@3.723] global cap_msmf.cpp:476 anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -2147024865[ WARN:0@3.733] global cap_msmf.cpp:488 anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -2147024865[ WARN:1@3.741] global cap_msmf.cpp:1769 CvCapture_MSMF::grabFrame videoio(MSMF): can’t grab frame. Error: -2147024865

ChatGPT suggests to change the backend to DSHOW with a suggestion how (I am a novice), however this did not help.

Furthermore, I have a script that finds IDs of connected and working cameras, which crashes when I run it with the HP connected. The HP also then disappears from device manager after addressing it with OpenCV.

Finally the webcam works fine in the Windows 11 camera app and shows a good image.

Hopefully someone knows what I may be missing here.

BR

you can change the backend in the constructor:

VideoCapture(0, cv.CAP_DSHOW);

Hi berak, thank you for your reply. I have run the script with this different backend and it does not work still unfortunately

this HP thing might be the source of your trouble then.

be aware of “USB hubs” and how you attach your cameras.

be aware of the video mode you set. make sure to set the FOURCC property to MJPG. sometimes exactly one one order of set() calls works, so permute until it does something. I think you can set those properties right in the VideoCapture constructor

Hi crackwitz, thank you I will try these suggestions. The cameras are connected directly to a USB 3.0 port (verified working as windows camera allows it to use 4k30)

@crackwitz and @berak , unfortunately none of these suggestions have lead to a working camera. it is now on another laptop. in VLC and windows camera it shows up perfectly, and the script finds the ID and names fine too. however only the integrated webcam gives an output image, and the 4k cameras only give errors