【Python教程】Opencv打开笔记本电脑摄像头报错解问题及解决

零 Python教程1 218字数 2566阅读8分33秒阅读模式

所需工具:

Python

聪明的大脑文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

勤劳的双手文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

注意:本站只提供教程,不提供任何成品+工具+软件链接,仅限用于学习和研究,禁止商业用途,未经允许禁止转载/分享等文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

 文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

教程如下

使用Opencv打开笔记本电脑摄像头报错

近期要做一个下位机上发图像数据给上位机的任务,调试时自己写了一个客户端获取笔记本电脑的摄像头视频数据传输给服务器,然后服务器端显示摄像头视频数据。结果运行时发现客户端报错,视频窗口闪退。文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

一般获取摄像头图像数据的代码如下:文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

[php]
capture = cv.VideoCapture(0)
[/php]文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

但是运行会报错如下:文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

[ 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)文章源自灵鲨社区-https://www.0s52.com/bcjc/pythonjc/12560.html

网上搜索了一下后发现只要对上述代码稍作修改即可

[php]
# 建立图像读取对象
capture = cv.VideoCapture(0,cv.CAP_DSHOW) #更改API设置
[/php]

此处0代表笔记本自带的摄像头,如果有外接摄像头则可以选填1、2等。

Opencv打开摄像头报错问题

CvCapture_MSMF::initStream Failed to set mediaType (stream 0, (640x480 @ 30)

[php]
cap=cv2.VideoCapture(0,cv2.CAP_DSHOW)
[/php]

[php]
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)
};
[/php]

附查看摄像头下标:

[php]
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
[/php]

 

 

零
  • 转载请务必保留本文链接:https://www.0s52.com/bcjc/pythonjc/12560.html
    本社区资源仅供用于学习和交流,请勿用于商业用途
    未经允许不得进行转载/复制/分享
    • doudou
      doudou 4

      L

    发表评论