0

Im using threading and opencv in Python.
I wanna display two windows at once.
But only one window can display at moment.
Here my code.

import cv2, time
class Core:
    @staticmethod
    def detection(ip):
        capture = cv2.VideoCapture('rtsp://'+str(ip))
        while (capture.isOpened()):
            ret, frame = capture.read()
            cv2.imshow('Video',frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        capture.release()
class Thread (threading.Thread):
    def __init__(self, threadID,ip):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.ip = ip

    def run(self):
        print("Start threadID" +str(self.threadID))
        Core.detection(self.ip)
        print("Exiting " + str(self.threadID))

threads = []
thread1 = Thread(1,'192.168.1.4:5554/playlist.m3u')
thread2 = Thread(2,'192.168.1.4:5554/playlist.m3u')
threads.append(thread1)
threads.append(thread2)
for i in threads:
    i.start()
print("Exit to main thread")

I wonder if there were any solution to solve this problem.
Thanks you so much.

Cedabyr
  • 33
  • 5
  • 1
    AFAIK, you cannot do display and keyboard interaction in threads, i.e. `cv2.imshow()` and `cv2.waitKey()`. I think you are obliged to put them in your main thread. I believe you can put reads/writes with `VideoCapture()` in threads. – Mark Setchell Apr 23 '21 at 05:54

1 Answers1

1
import threading
import cv2

class Thread (threading.Thread):
    def __init__(self, threadID,ip):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.ip = ip

    def run(self):
        print("Start threadID" +str(self.threadID))
        Core.detection(self.ip)
        print("Exiting " + str(self.threadID))
class Core:
    @staticmethod
    def detection(ip):
        capture = cv2.VideoCapture(ip)
        capture.set(cv2.CAP_PROP_BUFFERSIZE, 0)
        while (capture.isOpened()):

            ret, frame = capture.read()
            if ret:
                frame = cv2.resize(frame,(600,400))
                cv2.imshow(ip,frame)
                #cv2.waitKey(1)
                if cv2.waitKey(1) & 0xFF == ord('q'):
                    break
            else:
                print('attempting to reconnect', ip)
                # threads = []
                # for i, cam in enumerate(ip):
                #     thread1 = Thread(i, cam)
                #     threads.append(thread1)
                # for i in threads:
                #     i.start()
                # thread1 = Thread(1, ip)
                # thread1.start()
                Core.detection(ip)
        capture.release()
        cv2.destroyAllWindows()
if __name__=='__main__':
    cam_list=[
           "rtsp://10.11.25.65:554/stream1","rtsp://10.11.25.66:554/stream1"
            ]
    threads = []
    for i, cam in enumerate(cam_list):
        thread1 = Thread(i, cam)
        threads.append(thread1)
    for i in threads:
        i.start()
  • cv2.imshow(ip,frame), window show name each camera different – V B N REDDY Feb 25 '22 at 13:19
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 25 '22 at 17:40
  • if__name__=='__main__': cam_list=[1,2,3,4] threads = [] for i, cam in enumerate(cam_list): thread1 = Thread(i, cam) threads.append(thread1) for i in threads: i.start() print("Exit to main thread") – V B N REDDY Feb 28 '22 at 05:48