2

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()
9
  • 1
    AFAIK, you cannot do cv2.waitKey() and imshow() 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. Commented Dec 16, 2019 at 23:02
  • @nathancy, Thanks for you answer, I create two objects, and I was able to run both videos together. However, I wanted to show them in two different windows as they are running right now in the same window. Forgive me as I am quite of a beginner in python and OpenCV Commented Dec 17, 2019 at 9:17
  • I think you can have 2 windows, but you can't have 2 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 the waitKey() in the main thread... github.com/skvark/opencv-python/issues/134 Commented Dec 17, 2019 at 17:34
  • Make sure that you have two different show frames, for instance: cv2.imshow('frame_1',frame1) and cv2.imshow('frame_2',frame2). That will probably let you show two windows while you are capturing video in each individual thread Commented Dec 17, 2019 at 20:32
  • 2
    You can define a new class variable like self.id = 'thread_1' and self.id = 'thread_2' (maybe pass the id as an argument to the object) then when you show the image you can do cv2.imshow(self.id,frame1) or cv2.imshow(self.id,frame2) Commented Dec 17, 2019 at 21:39

1 Answer 1

2

Using Multiprocessing works for me!

import numpy as np
import cv2
from multiprocessing import Process

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 = r"C:\Users\bernad.peter\Downloads\Singapore Port.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()


if __name__ == '__main__':
    p1= Process(target = local_video)
    p2= Process(target = webcam_video)
    p1.start() 
    p2.start()

    p1.join()
    p2.join()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.