Warm tip: This article is reproduced from serverfault.com, please click

Video Capture fails when script is run from the command line

发布于 2020-11-27 10:59:55

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.

Questioner
Alex
Viewed
0
Alex 2020-11-28 21:18:36

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.