python opencv在使用第三方cam时无法显示视频

2024-10-03 06:22:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了下面的代码从摄像机读取视频显示和保存。在

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

我用的是第三方摄像头,用他们的软件播放视频,我需要用我的python代码捕捉。。在

对于apbase codeqt示例,它还播放视频

我不能使用下面的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\视频.py,lime 89,in cv2.imshow('图像',帧)

eror:…\opencv\modules\hihggui\src\窗口.cpp:错误:(-215)尺寸、宽度和gt;0&;尺寸.高度>;0在函数cv::imshow中


Tags: 代码视频ifdef错误filenameoutcv2
2条回答
cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

它确实解决了我的问题。在

我通过禁用windows硬件设置菜单中的内置摄像头来实现这一点。我在一个朋友的电脑上做了这件事,所以现在无法访问它,但请检查一下this。我相信windows不会让openCV使用第0个视频捕获设备,所以你必须让任何一个你想用的摄像头成为硬件列表中的第一个。在

相关问题 更多 >