2

I'm trying to set up a neural network to work on multiple cameras simultaneously (or almost at least..). First of all i'm trying to get to stream 2 cameras at once with OpenCV and the python threading module.

I've come up with this code:

import cv2
import threading
import queue

def multistream(stream, q):
    ret, frame = stream.read()
    q.put(frame)

if __name__ == "__main__":
   camlink1 = "rtsp://......link1"
   camlink2 = "rtsp://......link2"
   stream1 = cv2.VideoCapture(camlink1)
   stream2 = cv2.VideoCapture(camlink2)
   print("stream is opened")
   while True:
      q = queue.Queue()
      cam1 = threading.Thread(target=multistream, args=(stream1, q))
      cam2 = threading.Thread(target=multistream, args=(stream2, q))
      cam1.start()
      cam2.start()
      cam1.join()
      cam2.join()
      while not q.empty():
          cv2.imshow("video", q.get())

The problem is that cv2.imshow shows an empty window instead of the frame while, if i add to the code print(q.get()), it prints the mats of which the frame is made, so the frame is correctly returned from the multistream function to the main thread. What's the correct fix for this?

3
  • Huh? Why create a new queue and start a new thread for every frame? Rather start your two threads that each run a loop acquiring frames and sending them to your main. Commented Jun 4, 2018 at 16:54
  • yeah, i figured it out right after and it worked as you said Commented Jun 4, 2018 at 18:11
  • 1
    @simonEE can you please tell me the changes you made in your code? Commented Aug 8, 2018 at 7:12

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.