如何使用单个摄像机并行运行两个视频帧

时间:2019-02-20 15:29:44

标签: python opencv parallel-processing

我正在使用单个cam输出创建两个不同的帧。我需要分别处理它们并并行运行它们。

Import cv2
Import numpy as no

cap=videocapture(0)

def frame_1():
        ret,frame=cap.read()
        cv2.imshow("frame1",frame)
        print("a")

def frame_2():
        ret,frame=cap.read()
        cv2.imshow("frame2",frame)
        print("b")
        print("c")

while True:
         frame_1()
         frame_2()
         If cv2.waitkey(1)&00ff=ord("z"):
                  break
 cap.release()
 cv2.destroyallwindows()

我需要这两个框架并行工作。当我运行代码时,会出现两个帧,但是一帧会冻结,而另一帧会冻结。而且我还可以使用打印选项找到不同之处。首先,它打印a,然后打印b和c,就像这样。如何使这些框架并行工作?

1 个答案:

答案 0 :(得分:0)

下面是使用线程的示例代码:

import cv2
import threading

def frame(win, chars):
    while not flag.is_set() and cap.isOpened():
        rv, frame = cap.read()
        if rv:
            cv2.imshow(win, frame)
            for c in chars: print(c)
        if cv2.waitKey(1) & 0xff == 27: break
    flag.set() # terminate other threads as well

cap = cv2.VideoCapture(0)
flag = threading.Event()

t1 = threading.Thread(target=frame, args=('win1','a'))
t1.start()
t2 = threading.Thread(target=frame, args=('win2',('b','c')))
t2.start()

t1.join()
t2.join()

cap.release()