Jetson之opencv硬件编码输出rtsp

我们在jetson上使用 python 做 opencv 开发时,会遇到需要把处理过的 mat 编码输出到 rtsp,便于可视化结果,本文介绍相关实现方法

注意源码编译 opencv 的时候,cmake 那一步要把 -D WITH_GSTREAMER=ON 加上,否则运行不起来

安装依赖

sudo apt-get install libgstrtspserver-1.0-0 gstreamer1.0-rtsp
sudo apt-get install libgirepository1.0-dev
sudo apt-get install gobject-introspection gir1.2-gst-rtsp-server-1.0

实现代码 

#!/usr/bin/env python
import cv2
import gi
gi.require_version('Gst', '1.0')
gi.require_version('GstRtspServer', '1.0')
from gi.repository import GObject, Gst, GstRtspServer


def main():
    
    out_send = cv2.VideoWriter('appsrc is-live=true ! videoconvert ! \
                                omxh264enc bitrate=12000000 ! video/x-h264, \
                                stream-format=byte-stream ! rtph264pay pt=96 ! \
                                udpsink host=127.0.0.1 port=5400 async=false',
                                cv2.CAP_GSTREAMER, 0, 30, (1920,1080), True)


    if not out_send.isOpened():
        print('VideoWriter not opened')
        exit(0)

    rtsp_port_num = 8554 

    server = GstRtspServer.RTSPServer.new()
    server.props.service = "%d" % rtsp_port_num
    server.attach(None)
    
    factory = GstRtspServer.RTSPMediaFactory.new()
    factory.set_launch("(udpsrc name=pay0 port=5400 buffer-size=524288 \
                        caps=\"application/x-rtp, media=video, clock-rate=90000, \
                        encoding-name=(string)H264, payload=96 \")")
                        
    factory.set_shared(True)
    server.get_mount_points().add_factory("/ds-test", factory)

    # 输出rtsp码流信息
    print("\n *** Launched RTSP Streaming at rtsp://localhost:%d/ds-test ***\n\n" % rtsp_port_num)    

    cap = cv2.VideoCapture(0)

    while True:
        _, mat = cap.read()
        out_send.write(mat)
        cv2.waitKey(30) 
        
if __name__ == '__main__':
    main()

实现原理

原理是利用 gstreamer 构建一条 pipeline,通过 appsrc 接收 opencv 处理过的 mat, 然后经过硬件编码输出到 udpsink。

然后构建一个 rtspserver, 通过 udpsrc 来监听对应的端口,从而输出到 rtsp,在客户端拉流即可查看。

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页