首页 文章

使用第三方相机时,python opencv无法显示视频

提问于
浏览
1

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

当我在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,lime 89,in cv2.imshow('images',frame)

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

1 回答

  • 1

    我通过从Windows硬件设置菜单禁用内置网络摄像头来实现此功能 . 我在一个朋友's computer so I don'现在可以访问它,但检查this . 我相信windows不会让openCV使用任何其他视频捕获设备而是使用0,所以你必须在硬件列表中制作你想要使用的第一个摄像头 .

相关问题