2014-12-04 168 views
1

我正在使用opencv并有两个视频源。我正在使用下面的代码。代码有时有效,有时不起作用。我的代码有问题吗?我怎样才能弥补...Opencv两个摄像头源

import cv2 

Channel0 = cv2.VideoCapture(0) 
IsOpen0, Image0 = Channel0.read() 
Channel1 = cv2.VideoCapture(1) 
IsOpen1, Image1 = Channel1.read() 

while IsOpen0 and IsOpen1: 
    IsOpen0, Image0 = Channel0.read() 
    IsOpen1, Image1 = Channel1.read() 
    cv2.imshow("Webcamera",Image0) 
    cv2.imshow("Panasonic",Image1) 
    cv2.waitKey(10) 

PS它总是工作,当我只使用一个视频源。

+0

你能解释一下当它不工作,它呢? – user2313067 2014-12-04 22:00:10

回答

0

我想我找出了我的错误。由于某些原因,下面的代码有效。这一定是有问题的线程...

import thread 
import time 
import cv2 


def Webcamera(): 
    Channel0 = cv2.VideoCapture(0) 
    IsOpen0, Image0 = Channel0.read() 
    while IsOpen0: 
     IsOpen0, Image0 = Channel0.read() 
     cv2.imshow("Webcamera",Image0) 
     cv2.waitKey(10) 
    if not IsOpen0: 
     time.delay(0.5) 
     print "Error opening Web camera" 


def Panasonic(): 
    Channel1 = cv2.VideoCapture(1) 
    IsOpen1, Image1 = Channel1.read() 
    while IsOpen1: 
     IsOpen1, Image1 = Channel1.read() 
     cv2.imshow("Panasonic",Image1) 
     cv2.waitKey(10) 
    if not IsOpen1: 
     time.sleep(0.5) 
     print "Error opening Panasonic" 

try: 
    thread.start_new_thread(Webcamera,()) 
    thread.start_new_thread(Panasonic,()) 
except: 
    print "Error: unable to start thread" 

while 1: 
    pass