多线程实现海康多路rtsp流并展示

        测试了下海康可见光和热成像的rtsp流,需要同时展示出来查看下帧率效果,所以写了下代码,通用任何rstp相机,并非限制于海康

        后续会接入模型处理可见光和热成像每帧的图像数据,后面再补充代码

import cv2
import threading
import queue
import numpy as np

# 定义全局队列存储帧
visible_frame_queue = queue.Queue(maxsize=10)
thermal_frame_queue = queue.Queue(maxsize=10)

def read_stream(url, frame_queue):
    cap = cv2.VideoCapture(url)
    while True:
        ret, frame = cap.read()
        if ret:
            if not frame_queue.full():
                frame_queue.put(frame)
            else:
                _ = frame_queue.get()
                frame_queue.put(frame)

def display_streams():
    while True:
        if not visible_frame_queue.empty() and not thermal_frame_queue.empty():
            visible_frame = visible_frame_queue.get()
            thermal_frame = thermal_frame_queue.get()
            visible_frame = cv2.resize(visible_frame, (640, 480))
            # thermal_frame = cv2.resize(thermal_frame, (640, 480))
            # 创建一个空画布,用于拼接两个视频流的帧
            canvas = np.zeros((480, 1280, 3), dtype=np.uint8)
            # 在画布上绘制左边视频流的帧
            canvas[:, :640] = visible_frame
            # 在画布上绘制右边视频流的帧
            canvas[:, 640:] = thermal_frame
            # 在窗口中显示拼接后的画布
            cv2.imshow('Dual Video Streams', canvas)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

# RTSP流的URL
visible_stream_url = "rtsp://admin:密码@ip/Streaming/Channels/1" #可见光流
thermal_stream_url = "rtsp://admin:密码@ip/8000/ch2/main/av_stream" #热成像流

# 创建并启动读取视频流的线程
visible_stream_thread = threading.Thread(target=read_stream, daemon=True, args=(visible_stream_url, visible_frame_queue)).start()
thermal_stream_thread = threading.Thread(target=read_stream, daemon=True, args=(thermal_stream_url, thermal_frame_queue)).start()
# 启动显示视频流的线程
display_streams()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莫莫莫i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值