使用带Open的线程运行多摄像机

2024-09-27 23:17:11 发布

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

我正在尝试使用线程技术从多个摄像头传输视频。但是,我看到只有第一个摄影机正在启动,而第二个摄影机在ISOPENDE()函数中卡住。可以帮助我,我哪里做错了:

import cv2
import threading

class camThread(threading.Thread):
    def __init__(self, previewName, camID):

        threading.Thread.__init__(self)
        self.previewName = previewName
        self.camID = camID

    def run(self):

        print( "Starting " + self.previewName)
        camPreview(self.previewName, self.camID)

def camPreview(previewName, camID):
    cv2.namedWindow(previewName)
    cam = cv2.VideoCapture(camID)
    if cam.isOpened():  # try to get the first frame

        rval, frame = cam.read()
        print (camID,rval)

    else:

        rval = False
    while rval:

        cv2.imshow(previewName, frame)
        rval, frame = cam.read()
        key = cv2.waitKey(20)
        if key == 27:  

            break
    cv2.destroyWindow(previewName)

# Create two threads as follows

thread1 = camThread("Camera 1", 0)
thread2 = camThread("Camera 2", 1)

thread1.start()
thread2.start()

threads = []
threads.append(thread1)
threads.append(thread2)

for t in threads:
    t.join()

Tags: importselfdefcv2framethreadingcamthreads

热门问题