简体   繁体   English

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

[英]python opencv could not display video while using third party camera

I have written below code to read video from camera to display and save .我写了下面的代码来从相机读取视频以显示和保存。

when I run the below code with option 0 in VideoCapture(0), it works fine and display my webcam video, when I change it to 1 in VideoCapture(1) to get the video from third party camera, I get the error.当我在 VideoCapture(0) 中使用选项 0 运行以下代码时,它工作正常并显示我的网络摄像头视频,当我在 VideoCapture(1) 中将其更改为 1 以从第三方相机获取视频时,出现错误。

I am using the 3rd party camera, with their software it plays the video , I need to capture using my python code ..我正在使用 3rd 方相机,使用他们的软件播放视频,我需要使用我的 python 代码捕获..

with the apbase code qt example also it plays the video使用apbase 代码qt 示例,它还可以播放视频

I am not able to play using the below python code我无法使用以下 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

Error错误

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

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

I got this to work by disabling the built-in webcam from the windows hardware settings menu.我通过从 Windows 硬件设置菜单禁用内置网络摄像头来实现这一点。 I did this on a friend's computer so I don't have access to it now, but check this out.我做了这个朋友的电脑上,所以我不能够访问它了,但检查这个出来。 I believe windows won't let openCV use any other video capture device but the 0th, so you have to make any camera you want to use the first in the hardware list.我相信 Windows 不会让 openCV 使用除第 0 以外的任何其他视频捕获设备,因此您必须制作要使用硬件列表中第一个的任何相机。

cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

it indeed solved my problem.它确实解决了我的问题。

You just change VideoCapture(0) to VideoCapture (1) If you using external camera.如果您使用外部摄像头,您只需将 VideoCapture(0) 更改为 VideoCapture (1)。 If we using computer camera then we are write (0) So you can make that change如果我们使用电脑摄像头,那么我们会写 (0) 所以你可以做出改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM