how to run multiple camera in threading using python

Question:

below is the code i used to play multiple videos in parallel using multi threading pool. but only one video is playing for each input. i want each video to open separately. not combined

import concurrent.futures

RTSP_URL = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4"
RTSP_List = [RTSP_URL, RTSP_URL, RTSP_URL, RTSP_URL]


def url_to_video(url):
    video = cv2.VideoCapture(url)
    while True:
        _, frame = video.read()
        cv2.imshow("RTSP", frame)
        k = cv2.waitKey(1)
        if k == ord('q'):
            break
    video.release()
    cv2.destroyAllWindows()


while True:
    with concurrent.futures.ThreadPoolExecutor() as executor:
        executor.map(url_to_video, RTSP_List)```

how to play each video separately.


Asked By: Model Trainer

||

Answers:

you just need each thread to use a different name for the window in cv2.imshow, so that each thread will generate a different window, and you should place them somewhere distinct so that they aren’t appearing one over the other, i just added in index to them so that each distinct index will have a position on screen and different title, also you shouldn’t destroy all windows when one is done …

import concurrent.futures
import cv2
RTSP_URL = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4"
RTSP_List = [(RTSP_URL,0), (RTSP_URL,1), (RTSP_URL,2), (RTSP_URL,3)]


def url_to_video(tup):
    url,index = tup
    video = cv2.VideoCapture(url)
    while True:
        _, frame = video.read()
        cv2.imshow(f"RTSP {index}", frame)
        cv2.moveWindow(f"RTSP {index}", index*300, 0)
        k = cv2.waitKey(1)
        if k == ord('q'):
            break
    video.release()


while True:
    with concurrent.futures.ThreadPoolExecutor() as executor:
        executor.map(url_to_video, RTSP_List)
    cv2.destroyAllWindows()
Answered By: Ahmed AEK
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.