蟒蛇opencv无法显示视频,而使用第三方相机

问题描述:

我已经写下面的代码从相机读取视频来显示和保存。蟒蛇opencv无法显示视频,而使用第三方相机

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

我使用的是第三方的摄像头,与他们的软件它播放视频,我需要用我的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,石灰89,在cv2.imshow( 'images',frame)

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

+0

在Linux系统上'VideoCapture'默认使用'libv4l2'作为视频输入设备。安装'v4l-utils'并执行'v4l2-ctl --all'。看看它是否显示你的相机。 – zindarod

+0

我想在windows上 – user3607698

+0

对于windows,它最可能使用DShow。用你的显示摄像头,运行'cap = cv2.VideoCapture(0 + cv2.CAP_DSHOW)',如果它有效,那么它使用'DSHOW'。 – zindarod

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

+0

嗨,我禁用了默认摄像头,但仍然是同样的问题,我注意到只有摄像头下的“成像设备”我的第三方相机正在“通用串行总线控制器”下作为“aptina Demo”,因此有什么与此有关的 – user3607698

+0

您是否安装了相机的Windows驱动程序? –

+0

是的,那么只有它检测为“通用串行总线控制器”下的aptina demo – user3607698