簡體   English   中英

如何使用 python 在線程中運行多個攝像頭

[英]how to run multiple camera in threading using python

下面是我用來使用多線程池並行播放多個視頻的代碼。 但每個輸入只播放一個視頻。 我希望每個視頻單獨打開。 不合並

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.


您只需要每個線程在 cv2.imshow 中為cv2.imshow使用不同的名稱,以便每個線程將生成不同的 window,並且您應該將它們放在不同的地方,這樣它們就不會出現在另一個上,我剛剛添加在它們的index中,以便每個不同的index在屏幕上都有一個 position 和不同的標題,而且你不應該在完成后銷毀所有 windows...

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM