I am trying to run on python using openCV two functions at the same time. One function is supposed to show local video and the other should show frames from my webcam. When running the code below, the two windows freezes and blacks out. I am running it on Ubuntu 16.04
import cv2
import numpy as np
from threading import Thread
def webcam_video():
cap = cv2.VideoCapture(0)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
def local_video():
path = "video-1.mp4"
cap = cv2.VideoCapture(path)
while(True):
ret, frame = cap.read()
if ret == True:
cv2.imshow('frame_2',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else :
break
cap.release()
cv2.destroyAllWindows()
t1= Thread(target = webcam_video)
t2= Thread(target = local_video)
t1.start()
t2.start()
cv2.waitKey()andimshow()in 2 different threads. I think you'd need to have a single thread doing display and waiting for keys. You can, as nathancy shows, acquire in multiple threads.waitKey()calls from different threads. You can keep capturing video in 2 threads without problem. It's just that you must display the images and do thewaitKey()in the main thread... github.com/skvark/opencv-python/issues/134cv2.imshow('frame_1',frame1)andcv2.imshow('frame_2',frame2). That will probably let you show two windows while you are capturing video in each individual threadself.id = 'thread_1'andself.id = 'thread_2'(maybe pass the id as an argument to the object) then when you show the image you can docv2.imshow(self.id,frame1)orcv2.imshow(self.id,frame2)