4

SYSTEM AND INSTALL INFORMATION

  • 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.

HOWEVER

If I run it through the Python debugger (either with python -m pdb webcam.py 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, but it doesn't appear to have been resolved: https://answers.opencv.org/question/220309/videocapture-from-camera-works-only-in-debug-python/

I have also tried it in C++ (OpenCV), and using a different library in Rust (Non-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(sys.version_info)
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())

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

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

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\__init__.py", line 181, in <module>
    bootstrap()
  File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\site-packages\cv2\__init__.py", line 153, in bootstrap        
    native_module = importlib.import_module("cv2")
  File "C:\Users\user1\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", 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?
birdistheword99
  • 179
  • 1
  • 15
  • I have encountered this error while integrating the webcam in Django using streamingHttpResponse object, where it seems that the error occurs because the webcam doesn't generate the frame while the code is expecting a frame to be there. – Mathpdegeek497 Feb 27 '23 at 17:41
  • Whenever I run this script standalone in VSC, it works fine. I am not able to reproduce this error outside the Django integration. – Mathpdegeek497 Feb 27 '23 at 17:43
  • Do you have any anti virus running on your system? – Sreyas Feb 28 '23 at 15:31
  • 1
    @Sreyas disabling antivirus worked! My work has a pretty restrictive policy using Kaspersky and normally it flags up when something has been blocked but it doesn't do it in this case... I can't award the bounty to a comment, so can you submit this as an answer so I can award it? – birdistheword99 Mar 01 '23 at 09:38

2 Answers2

1

Usually this error occurred to me when some application like Antivirus interfering OpenCV, blocking it from accessing the camera. It might be the case that some antivirus software has tendency to block OpenCV when run in non debug mode. My current hypothesis is that in debug mode, the OpenCV libraries may be more transparent, which may not trigger the same behavior in your antivirus software.

You can temporarily try disabling antivirus and if it works you can add an exception for OpenCV in your antivirus software to prevent it from being blocked in the future.

Sreyas
  • 461
  • 3
  • 15
  • Thanks @Sreyas, it turned out to be an AntiVirus issue. Disabling Kaspersky resolved the issue. Interestingly Kaspersky doesn't show OpenCV in its list of blocked threats, so I will have to work out exactly what its blocking so I can add an exception... – birdistheword99 Mar 01 '23 at 10:16
0

I might have misread your code, you might already be doing this actually but...
I'll link another StackOverflow post here
But maybe you could check that the camera is not none, in your case

    if frame is not None:
        [your code]

but im not sure that your problem is related, because it doesnt seem to say any line for the error.

Does the warning show up before or after ret, frame = vid.read() if its after maybe use the solution i suggest if not, i'm not sure of the issue...

PILIX
  • 1
  • 3
  • The frame returns `None`, but the camera reports `true` when you call `.isOpened()`, so it thinks the camera is available but can't capture a frame. The warning shows up after trying to read a frame. I tried debugging it to see exactly which line it triggers the warning but if I run it with the debugger attatched it works! Very confusing... – birdistheword99 Mar 01 '23 at 09:25
  • ok, so yeah it might actually be because your program runs too fast on the first pass and the camera isnt open at the time, if you try the suggestion i had with the `if frame is not None` does it end up opening the camera? – PILIX Mar 01 '23 at 16:29
  • The issue has now been resolved and was caused by Antivirus, thanks for contributing though! – birdistheword99 Mar 02 '23 at 12:52