环境:
win7、Python3.7、OpenCV4.5.2、笔记本电脑
报错内容:
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (928) CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (640x480 @ 30) MFVideoFormat_RGB24(unsupported media type)
[ WARN:[email protected]] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (928) CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (640x480 @ 30) MFVideoFormat_RGB24(unsupported media type)
出错啦!
代码报错定位位置:
# 获取摄像头
cap = cv2.VideoCapture(0)
# 打开cap
cap.open(0)
解决办法:
# 获取摄像头
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
# 打开cap
cap.open(0,cv2.CAP_DSHOW)
涉及到摄像头index 的操作,都应加上cv2.CAP_DSHOW
VideoCapture函数:
创建从视频文件或摄像头中捕获视频的对象
open函数:
从摄像头获取视频流
函数原型:
(VideoCapture).open(index)
默认笔记本/台式机若只有一个0摄像头,Index=0; 如果有2个,一般Index为0和1,根据具体情况区分,摄像头接入和断开会改变Index值
接入2个以上,但只想用指定的一个,可以在设备管理器中禁用其他,同时Index设置为0
摄像头下标index查看方法:
转自:blog.csdn.net/ltl451011/article/details/123462206
import cv2
ID = 0
while(1):
cap = cv2.VideoCapture(ID,cv2.CAP_DSHOW)
# get a frame
ret, frame = cap.read()
if ret == False:
ID += 1
else:
print(ID)
break