使用Opencv打開筆記本電腦攝像頭報錯
Opencv打開攝像頭報錯問題
使用Opencv打開筆記本電腦攝像頭報錯近期要做一個下位機上發圖像數據給上位機的任務,調試時自己寫了一個客戶端獲取筆記本電腦的攝像頭視頻數據傳輸給服務器,然後服務器端顯示攝像頭視頻數據。結果運行時發現客戶端報錯,視頻窗口閃退。
一般獲取攝像頭圖像數據的代碼如下:
capture = cv.VideoCapture(0)
但是運行會報錯如下:
[ WARN:0] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-52oirelq\opencv\modules\videoio\src\cap_msmf.cpp (677) CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (640x480 @ 30) MFVideoFormat_RGB24(codec not found)
網上搜索了一下後發現只要對上述代碼稍作修改即可
# 建立圖像讀取對象 capture = cv.VideoCapture(0,cv.CAP_DSHOW) #更改API設置
此處0代表筆記本自帶的攝像頭,如果有外接攝像頭則可以選填1、2等。
Opencv打開攝像頭報錯問題CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (640x480 @ 30)
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
enum VideoCaptureAPIs { CAP_ANY = 0, //!< Auto detect == 0 CAP_VFW = 200, //!< Video For Windows (obsolete, removed) CAP_V4L = 200, //!< V4L/V4L2 capturing support CAP_V4L2 = CAP_V4L, //!< Same as CAP_V4L CAP_FIREWIRE = 300, //!< IEEE 1394 drivers CAP_FIREWARE = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE CAP_IEEE1394 = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE CAP_DC1394 = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE CAP_CMU1394 = CAP_FIREWIRE, //!< Same value as CAP_FIREWIRE CAP_QT = 500, //!< QuickTime (obsolete, removed) CAP_UNICAP = 600, //!< Unicap drivers (obsolete, removed) CAP_DSHOW = 700, //!< DirectShow (via videoInput) CAP_PVAPI = 800, //!< PvAPI, Prosilica GigE SDK CAP_OPENNI = 900, //!< OpenNI (for Kinect) CAP_OPENNI_ASUS = 910, //!< OpenNI (for Asus Xtion) CAP_ANDROID = 1000, //!< Android - not used CAP_XIAPI = 1100, //!< XIMEA Camera API CAP_AVFOUNDATION = 1200, //!< AVFoundation framework for iOS (OS X Lion will have the same API) CAP_GIGANETIX = 1300, //!< Smartek Giganetix GigEVisionSDK CAP_MSMF = 1400, //!< Microsoft Media Foundation (via videoInput) CAP_WINRT = 1410, //!< Microsoft Windows Runtime using Media Foundation CAP_INTELPERC = 1500, //!< RealSense (former Intel Perceptual Computing SDK) CAP_REALSENSE = 1500, //!< Synonym for CAP_INTELPERC CAP_OPENNI2 = 1600, //!< OpenNI2 (for Kinect) CAP_OPENNI2_ASUS = 1610, //!< OpenNI2 (for Asus Xtion and Occipital Structure sensors) CAP_GPHOTO2 = 1700, //!< gPhoto2 connection CAP_GSTREAMER = 1800, //!< GStreamer CAP_FFMPEG = 1900, //!< Open and record video file or stream using the FFMPEG library CAP_IMAGES = 2000, //!< OpenCV Image Sequence (e.g. img_%02d.jpg) CAP_ARAVIS = 2100, //!< Aravis SDK CAP_OPENCV_MJPEG = 2200, //!< Built-in OpenCV MotionJPEG codec CAP_INTEL_MFX = 2300, //!< Intel MediaSDK CAP_XINE = 2400, //!< XINE engine (Linux) };
附查看攝像頭下標:
import cv2ID = 0while(1): cap = cv2.VideoCapture(ID,cv2.CAP_DSHOW) # get a frame ret, frame = cap.read() if ret == False: ID += 1 else: print(ID) break
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持軟件開發網。