【问题标题】:python opencv could not display video while using third party camerapython opencv在使用第三方摄像头时无法显示视频
【发布时间】:2018-03-04 23:33:42
【问题描述】:

我已经编写了下面的代码来从相机读取视频以显示和保存。

当我在 VideoCapture(0) 中使用选项 0 运行以下代码时,它可以正常工作并显示我的网络摄像头视频,当我在 VideoCapture(1) 中将其更改为 1 以从第三方摄像头获取视频时,我得到错误。

我正在使用 3rd 方相机,通过他们的软件播放视频,我需要使用我的 python 代码进行捕捉..

使用apbase code qt 示例也可以播放视频

我无法使用以下 python 代码玩游戏

import cv2
import numpy as np
import time
def nothing(x):
    pass

cv2.namedWindow('images')
switch = 'Recording'
cv2.createTrackbar(switch, 'images',0,1,nothing)
cap = cv2.VideoCapture(0)

def writeVideo(frmae):
    pass

switchstatus = 0

currentpos = 0
fourccs = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('sample.avi', fourccs, 20.0, (640,480))
created = 0
startrecord = 0



def RecordVideo(frame):
    global out
    global created;
    global startrecord
    print "In the Record video" ,created, startrecord
    if created == 0 and startrecord ==1:
        filename ='test.avi'
        filename=time.strftime("%Y-%m-%d-%H-%M-%S")+'.avi'
        print "filename", filename
        out = cv2.VideoWriter(filename,fourccs, 20.0, (640,480))
        created = 1;
        out.write(frame)
    elif created == 1 and startrecord ==1:
        out.write(frame)


def positionChanged(s):
    global currentpos
    global created
    global startrecord

    print "position changed", s
    currentpos = s
    if s==1:
        startrecord = 1
        created = 0
    else:
        startrecord = 0
    if created==1:
        created =0
        out.release()




def switchchanged(s):
    global switchstatus;
    if switchstatus != s:
        switchstatus = s
        positionChanged(s)



while(1):
    ret, frame = cap.read()
        RecordVideo(frame)
    cv2.imshow('images',frame)

    s = cv2.getTrackbarPos(switch,'images')

    switchchanged(s)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        out.release()
        cv2.destroyAllWindows()
        break

错误

文件“C:\Python32Bit\video.py,lime 89, in cv2.imshow('images',frame)

错误:........\opencv\modules\hihggui\src\window.cpp:error:(-215) size.width>0 && size.height>0 in function cv::imshow

【问题讨论】:

  • 在 Linux 系统上 VideoCapture 默认使用 libv4l2 用于视频输入设备。安装v4l-utils 并执行v4l2-ctl --all。看看它是否显示你的相机。
  • 我在 windows 上试试
  • 对于 windows,它很可能使用DShow。使用您的显示网络摄像头,运行cap = cv2.VideoCapture(0+cv2.CAP_DSHOW),如果它工作,那么它正在使用DSHOW
  • 0+cv2.CAP_DSHOW 正在为我的网络摄像头工作,而 1+cv2.CAP_DSHOW 给出了同样的错误。出于测试目的,我交叉检查,安装了该相机“aptina m031”的驱动程序
  • 好的,让我们再试一次。 cap = cv2.VideoCapture(0+cv2.CAP_FFMPEG)。顺便说一句,你是从源代码还是从 opencv_python 绑定安装 OpenCV 的?

标签: python opencv camera


【解决方案1】:
cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

确实解决了我的问题。

【讨论】:

    【解决方案2】:

    我通过从 Windows 硬件设置菜单中禁用内置网络摄像头来实现此功能。我是在朋友的电脑上完成的,所以我现在无法访问它,但请查看this。我相信windows不会让openCV使用除第0个以外的任何其他视频捕获设备,所以你必须在硬件列表中制作任何你想使用的相机。

    【讨论】:

    • 嗨,我禁用了默认摄像头,但仍然是同样的问题,我注意到“成像设备”下只有摄像头,我的第三方摄像头作为“aptina Demo”在“通用串行总线控制器”下,所以这有什么关系吗
    • 你安装了摄像头的windows驱动了吗?
    • 是的,那么只有在“通用串行总线控制器”下检测为aptina demo
    • 我不确定,我知道 Windows 有一个很好的功能,即在第一次安装驱动程序时不工作,所以你必须重新安装驱动程序。所以你可以尝试重新安装它们。也许做一些经典的 Windows troubleshooting
    【解决方案3】:

    您只需将 VideoCapture(0) 更改为 VideoCapture (1) 如果您使用外部摄像头。 如果我们使用电脑摄像头,那么我们写 (0) 所以你可以做出改变

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2011-02-05
    • 2017-09-12
    • 1970-01-01
    相关资源
    最近更新 更多