栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python并行读取显示同一个视频

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python并行读取显示同一个视频

import cv2
from multiprocessing.pool import ThreadPool
import multiprocessing
import subprocess


def show_stream(stream_name: str, win_name:str, sleep_time = -1):
    print(f"show stream, name = {stream_name}, win_name = {win_name}, sleep_time = {sleep_time}")

    filename = stream_name
    cap = cv2.VideoCapture(filename)
    assert cap.isOpened(), f'Failed to open {filename}'
    w = int(cap.get(cv2.CAP_PROP_frame_WIDTH))
    h = int(cap.get(cv2.CAP_PROP_frame_HEIGHT))
    fps = cap.get(cv2.CAP_PROP_FPS)  # 30 FPS fallback
    frames = int(cap.get(cv2.CAP_PROP_frame_COUNT))  # infinite stream fallback
    m_sec = cap.get(cv2.CAP_PROP_POS_MSEC)

    total_time = frames / fps

    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))

    print(w, h, fps, frames, fourcc, m_sec/1000, total_time)

    # cap.release()

    n = 0
    read = 1
    t_start = time.time()
    while cap.isOpened() and n < frames:
        # lock.acquire()
        # logger.info(f"========== {threading.current_thread().name}, {threading.current_thread().ident}")
        t1 = time.time()
        n += 1
        # _, self.imgs[index] = cap.read()
        cap.grab()
        if n % read == 0:
            success, im = cap.retrieve()
            if success:
                cv2.imshow(win_name, im)
                cv2.waitKey(1)
            imgs = im if success else imgs * 0

        # lock.release()
        if sleep_time < 0:
            time.sleep(1.0 / fps)  # wait time
        t2 = time.time()
        print(f"{n} th frame read time = {t2 - t1} s, thread = {current_thread().name}")

    t_end = time.time()

    print("total_time = ", t_end - t_start)

filename = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov"

thread1 = multiprocessing.Process(target=show_stream, args=(filename, "111", -1,), name="thread111")
thread2 = multiprocessing.Process(target=show_stream, args=(filename, "222", 0,), name = "thread222")
thread3 = multiprocessing.Process(target=show_stream, args=(filename, "333", 0.036,), name = "thread333")


thread1.start()
thread2.start()
thread3.start()


# while True:
#     if thread1.is_alive():
#         print("thread1 is alive")
#     else:
#         print("thread1 dead")
#         break

#     if thread2.is_alive():
#         print("thread2 is alive")
#     else:
#         print("thread2 dead")
#         break
#     time.sleep(1)

print("main end")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/276262.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号