Python使用Opencv打开笔记本电脑摄像头报错解问题及解决

Halima ·
更新时间:2024-11-10
· 750 次阅读

目录

使用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 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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。



摄像 笔记本 opencv 笔记本电脑 摄像头 电脑 Python

需要 登录 后方可回复, 如果你还没有账号请 注册新账号