1

I have used OpenCV for Python on this device extensively without ever coming across this issue. I am running Windows 8.1 Pro x64. I get the same issue on two different webcams.

I encountered this problem first when I was migrating a script to a different process with multiprocessing. (OpenCV is operating in only one process, with one webcam). I feared like some other modules, this was OpenCV playing up with being run in a different environment, but I've only had that problem with multithreading anyway. It seems it has nothing to do with this, but rather the fact that I am now executing the entire program from the command line.

If I run a basic script with the Video Capture object below in the python IDLE, or the command line IDLE, it runs as normal. My ENV PATH for python checks out to the same installation of 3.8.

import cv2

cap = cv2.VideoCapture(0)

running, _ = cap.read()
while running:

   running, frame = cap.read()

   cv2.imshow("Feed", frame)

   key = cv2.waitKey(1)
   if key & 0xFF == ord("q"):
        
          running = False

cap.release()
cv2.destroyAllWindows()

However if I create a script called 'test.py' with these contents and run it at terminal, I get a series of errors and the webcam fails to be opened.

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\...\scripts>test.py

[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\o
pencv\modules\videoio\src\cap_msmf.cpp (373) `anonymous-namespace'::SourceReader
CB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -214
7024891
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\o
pencv\modules\videoio\src\cap_msmf.cpp (385) `anonymous-namespace'::SourceReader
CB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error sta
tus: -2147024891
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\o
pencv\modules\videoio\src\cap_msmf.cpp (912) CvCapture_MSMF::grabFrame videoio(M
SMF): can't grab frame. Error: -2147024891
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\o
pencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReader
CB::~SourceReaderCB terminating async callback

I have tried the fix some suggest with 'cv2.CAP_DSHOW', although it removes the warnings, all I get returned is a black frame.

Alex
  • 21
  • 6
  • apologies, that snippet was just the gist, I've updated it with the actual script contents. – Alex Nov 27 '20 at 11:30
  • Sorry, cant help. I can not reproduce your issue, all runs fine on windows. Try check this out: https://stackoverflow.com/questions/59371075/opencv-error-cant-open-camera-through-video-capture or this https://answers.opencv.org/question/212468/how-to-read-videos-from-three-cameras-in-the-same-time/ – iGian Nov 27 '20 at 11:52
  • Thanks for trying. Unfortunately no luck with either of these. – Alex Nov 27 '20 at 12:00
  • likely a configuation or 3rd party library installation issue. Check your cmake for correct compiled 3rd party library. and do check if they are compatiable – Dr Yuan Shenghai Nov 27 '20 at 17:22

1 Answers1

1

I resolved the issue, turns out it was Kaspersky Antivirus picking up the webcam access as a threat, which only happens when it is executed from terminal.

Courtesy of johncasey's answer here: Webcam + Open CV Python | Black screen

As is clear from the Kaspersky reports, the script file gets flagged and made restricted, returning a black frame or preventing OpenCV from running at all. I moved script.py out of the 'Low Restricted' category under 'Host Intrusion Protection' to 'Trusted', and this solved the issue.

Kasperky Host Intrusion Protection

This explains the warnings relating to MSMF now too.

Alex
  • 21
  • 6