测试了下海康可见光和热成像的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()