2014-12-04 166 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