Video Capture from webcam only works when debugging


  • System Information
  • OpenCV Version 4.7.0
  • Operating System: Windows 10.0.17763 (Pro - Version 21H2)
  • CMake: 3.24.2
  • Python Version: 3.8.6
  • OpenCV version Installed from pip (but also built from source as part of diagnostics, reverted back to pip version as no change)

Description of issue

I am unable to capture any frames from a webcam, as I always get the following error: [ WARN:0@32.309] global cap_msmf.cpp:1759 CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147483638

The camera opens OK, as the light next to the webcam turns on, and vid.isOpened() returns true. I have tried with a secondary external USB Webcam (Logitec) and I have the same behaviour. If I run it with the cv2.CAP_DSHOW backend then the warning goes away, however the image is still no displayed. The camera works in other apps such as MS Teams, Google Meet, and the built-in camera app that comes with windows.

I have tried removing and re-adding OpenCV and it did not help. I also Tried compiling it from source and still no luck. I also tried the contrib build but this had no effect. I believe this maybe related to my hardware in some way, as I can run the same script on a colleagues laptop without issue. However both cameras I tried work with other apps, so I don’t know what could be causing it to fail.


If I run it through the Python debugger (either with python -m pdb or through VS Code runner), then strangely it works. The first thing I checked was that there were not multiple versions installed and that the debugger wasn’t calling one version rather than the other, but their appears to be no change. Its the same version of Python and the same version of OpenCV for both.

I found another person with exactly the same issue on the older forum, but it doesn’t appear to have been resolved: VideoCapture from camera works only in debug (Python) - OpenCV Q&A Forum

I have also tried it in C++ (OpenCV), but neither were able to successfully capture a frame from the built-in webcam, or the external one.

Steps to reproduce

Code to replicate the issue, nothing fancy going on:

# import the opencv library
import cv2
import sys
print("OPEN CV: ", cv2.__version__)

# define a video capture object
vid = cv2.VideoCapture(0)
# vid = cv2.VideoCapture(0, cv2.CAP_DSHOW)

cv2.namedWindow('Test Window', cv2.WINDOW_NORMAL)
print("Camera Opened: ", vid.isOpened())

    # Capture the video frame
    ret, frame =
    if ret:
        cv2.imshow('Test Window', frame)
        print("Error Drawing Frame")

    if cv2.waitKey(1) & 0xFF == ord('q'):
# After the loop release the cap object
# Destroy all the windows

Side Note/ Possible red herring…

If I enable the OPENCV_VIDEOIO_DEBUG enviroment variable ( Set-Item -Path Env:OPENCV_VIDEOIO_DEBUG -Value ($Env:OPENCV_VIDEOIO_DEBUG + ";1" ) Then it gives a different error, however this may be a red herring. I have posted it here for completeness:

Traceback (most recent call last):
  File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\site-packages\cv2\", line 181, in <module>
  File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\site-packages\cv2\", line 153, in bootstrap        
    native_module = importlib.import_module("cv2")
  File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\importlib\", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: DLL load failed while importing cv2: A dynamic link library (DLL) initialization routine failed.

I Don’t know which DLL is causing this issue though, and since I can get it to work if I run it in the debugger, this seems unrelated.

Next Steps

  1. What other debugging can I do to help resolve this issue?
  2. Are there any core Windows libraries that OpenCV (for example) uses that I could perhaps try re-installing, like MSMF? If so, how?
  3. Has anyone else even seen this issue?

try to use the directshow backend instead of MSMF:

vid = cv2.VideoCapture(0, CAP_DSHOW)

also, try to find more recent drivers for your hw

UVC driver comes with the OS, so maybe see about upgrading the OS to 22H2, which is the latest Win10.

more likely it’s due to your custom build of OpenCV not finding all the DLLs it was built for, because they lie somewhere that’s not in PATH

Thank you for you comment @crackwitz , but it turns out the issue was related to my Antivirus. Kaspersky was silently blocking access to the camera, no idea why, so I will have to find a way to allow this.

SOLUTION: Disable Antivirus!