Python下使用opencv调用摄像头出错

报错情况:

[ WARN:0] global D:\Build\OpenCV\opencv-4.4.0\modules\videoio\src\cap_msmf.cpp (373) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): OnReadSample() is called with error status: -2147024809   
[ WARN:0] global D:\Build\OpenCV\opencv-4.4.0\modules\videoio\src\cap_msmf.cpp (385) `anonymous-namespace'::SourceReaderCB::OnReadSample videoio(MSMF): async ReadSample() call is failed with error status: -2147024809
[ WARN:1] global D:\Build\OpenCV\opencv-4.4.0\modules\videoio\src\cap_msmf.cpp (912) CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -2147024809
Traceback (most recent call last):
  File ".\opencv_opencamera.py", line 6, in 
    cv2.imshow('frame',frame)
cv2.error: OpenCV(4.4.0) D:\Build\OpenCV\opencv-4.4.0\modules\highgui\src\window.cpp:384: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

[ WARN:1] global D:\Build\OpenCV\opencv-4.4.0\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback

出现上述错误,使用如下方法解决:

cap =cv2.VideoCapture(0)
改为
cap =cv2.VideoCapture(0,cv2.CAP_DSHOW)

应该属于版本兼容问题。

Reference:
https://www.pythonf.cn/read/80895

你可能感兴趣的:(异常错误处理,opencv,python,计算机视觉)