0

I am writing a program which displays a security console for all my rtsp streams from my ip cameras. Each stream plays in a different window, I want it all to be merged into one window.

Here is my code:

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):
        camPreview(self.previewName, self.camID)

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

    else:
        rval = False

    while rval:
        frame = cv2.resize(frame, (620,400))
        cv2.imshow(previewName, frame)
        rval, frame = cam.read()
        cv2.waitKey(20)


thread1 = camThread("Camera 1", 'my-rtsp-link')
thread2 = camThread("Camera 2", "my-second-rtsp-link")
thread1.start()
thread2.start()

I've tried multiproccessing and it doesn't work since I have a while loop in my function. Any help would be greatly appreciated.

I used np.hstack to combine my rtsp streams however this makes the streams freeze. So I got both streams working by multithreading. But now each stream is playing in its individual window which is very messy if I add multiple cameras. How can I merge it all into one big window?

  • 1
    Does [how can I play multiple videos simultaneously using opencv in python?](https://stackoverflow.com/questions/47922303/how-can-i-play-multiple-videos-simultaneously-using-opencv-in-python) answer your question? – wwii Feb 19 '23 at 00:42
  • @wwii Thank you for your response! I have already seen that forum, it does play multiple videos, however it does it in 2 separate windows. I want my rtsp streams to play in one window without freezing. So instead of stacking the arrays I need to find a way to merge the 2 individual windows. – saatvik Feb 19 '23 at 00:45
  • How did you think threading would help? – wwii Feb 19 '23 at 00:57
  • @wwii threading removed the lagging and freezing of both RTSP streams. Now I Just need to merge them into one window. – saatvik Feb 19 '23 at 01:18
  • There are questions in SO on how to show frames from OpenCV using tkinter label and PhotoImage. – acw1668 Feb 19 '23 at 03:35
  • 1
    Used `np.hstack((img1,img2)` – toyota Supra Feb 19 '23 at 11:35
  • @acw1668 Thanks for your response! Can you maybe provide a link where I can see how to use tkinter to merge windows? – saatvik Feb 19 '23 at 19:56
  • See this [question](https://stackoverflow.com/questions/16366857/show-webcam-sequence-tkinter). – acw1668 Feb 20 '23 at 01:32

0 Answers0