跳至主要內容

视频流传输

大约 1 分钟使用指南ImageZMQvideo_streamingpython

使用 imagezmq 传输视频流

视频流发送端

from imutils.video import VideoStream
import imagezmq
import socket
import cv2

# 创建VideoCapture对象,参数0表示使用默认的摄像头
cap = cv2.VideoCapture(0)

#ubuntu 的ip
sender = imagezmq.ImageSender(connect_to='tcp://10.144.54.202:5555') 
cam_id = socket.gethostname()
print("cam_id: ",cam_id)

while True:
    # 读取视频流的帧
    ret, frame = cap.read()
    sender.send_image(cam_id, frame)
    print(frame.shape)

    resized_frame = cv2.resize(frame, (640, 480))

    cv2.imshow('Resized Frame', resized_frame)

    # 按下'q'键退出循环
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# 释放VideoCapture对象
cap.release()

# 关闭所有打开的窗口
cv2.destroyAllWindows()

视频流接收端

import cv2
import imagezmq
image_hub = imagezmq.ImageHub()

while True:  
    cam_id, frame = image_hub.recv_image()
    # cv2.imshow(cam_id, frame)  
    print(frame.shape)
    cv2.waitKey(1)

    image_hub.send_reply(b'OK')

使用opencv从 接受rtsp视频流

import cv2

def main():
    # RTSP视频流地址,包含用户名和密码
    rtsp_url = 'rtsp://<USERNAME>:<PASSWORD>@<RTSP_SERVER_IP>:<RTSP_SERVER_PORT>/video'

    # 创建VideoCapture对象,连接到RTSP视频流
    cap = cv2.VideoCapture(rtsp_url)

    while True:
        # 读取视频流的帧
        ret, frame = cap.read()

        # 检查帧是否成功读取
        if not ret:
            break

        # 在这里对帧进行处理,例如显示、保存等
        cv2.imshow('Frame', frame)

        # 按下'q'键退出循环
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # 释放VideoCapture对象
    cap.release()

    # 关闭所有打开的窗口
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()

参考: