Opencv两个摄像头源

时间:2014-12-04 20:42:24

标签: python python-3.x

我正在使用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当我只使用一个视频源时它总是有效。

1 个答案:

答案 0 :(得分: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

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