简体   繁体   English

无法从 OpenCV Gstreamer 接收 gstreamer UDP Stream

[英]Cannot receive gstreamer UDP Stream from OpenCV Gstreamer

I'm working on Gazebo Sim which uses 'Gstreamer Plugin' to stream Camera Video via UDP.我正在通过 UDP 使用“Gstreamer 插件”到 stream 摄像机视频的 Gazebo Sim 工作。 Simulation is started on Ubuntu 18.04.在 Ubuntu 18.04 上开始模拟。

There is some resources to understand backend of this structer.有一些资源可以理解这个结构的后端。 Gazebo Simulation PX4 Guide Gazebo Simulation PX4 指南

And they mentions how to create pipeline:他们提到了如何创建管道:

The video from Gazebo should then display in QGroundControl just as it would from a real camera.然后,来自 Gazebo 的视频应该像来自真实相机一样显示在 QGroundControl 中。

It is also possible to view the video using the Gstreamer Pipeline.也可以使用 Gstreamer Pipeline 查看视频。 Simply enter the following terminal command:只需输入以下终端命令:

gst-launch-1.0  -v udpsrc port=5600 caps='application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264' \
! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink fps-update-interval=1000 sync=false

And it works well on terminal.它在终端上运行良好。 I read these questions:我读了这些问题:

using gstreamer with python opencv to capture live stream? 使用带有 python opencv 的 gstreamer 来捕获实时 stream?

Write in Gstreamer pipeline from opencv in python 从 python 中的 opencv 写入 Gstreamer 管道

So then, I tried to implement this pipeline into opencv by using following lines:因此,我尝试使用以下几行将此管道实现到 opencv 中:

video = cv2.VideoCapture('udpsrc port=5600 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264" ! rtph264depay ! avdec_h264 ! videoconvert ! autovideosink fps-update-interval=1000 sync=false', cv2.CAP_GSTREAMER)
    #video.set(cv2.CAP_PROP_BUFFERSIZE,3)
    # Exit if video not opened.
    if not video.isOpened():
        print("Could not open video")
        sys.exit()

    # Read first frame.
    ok, frame = video.read()
    if not ok:
        print('Cannot read video file')
        sys.exit()

But it's only giving error:但它只给出错误:

Could not open video无法打开视频

And I tried different variations of this pipeline in opencv but None of them helped me.我在 opencv 中尝试了这条管道的不同变体,但没有一个对我有帮助。

Currently, your pipeline provides no way for OpenCV to extract decoded video frames from the pipeline.目前,您的管道无法让 OpenCV 从管道中提取解码的视频帧。 This is because all frames go to the autovideosink element at the end which takes care of displaying the frames on-screen.这是因为所有帧 go 到最后的autovideosink元素,它负责在屏幕上显示帧。 Instead, you should use the appsink element, which is made specifically to allow applications to receive video frames from the pipeline.相反,您应该使用appsink元素,该元素专门用于允许应用程序从管道接收视频帧。

video = cv2.VideoCapture(
    'udpsrc port=5600 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)H264"'
    ' ! rtph264depay'
    ' ! avdec_h264'
    ' ! videoconvert'
    ' ! appsink', cv2.CAP_GSTREAMER)

Following code works without errors:以下代码可以正常工作:

# Read video
video = cv2.VideoCapture("udpsrc port=5600 ! application/x-rtp,payload=96,encoding-name=H264 ! rtpjitterbuffer mode=1 ! rtph264depay ! h264parse ! decodebin ! videoconvert ! appsink", cv2.CAP_GSTREAMER);

I think decode option wasn't true.我认为解码选项不正确。

I tried this code but did not work.我尝试了这段代码,但没有奏效。 I also tried a different pipeline.我还尝试了不同的管道。 Below are my terminal pipelines:以下是我的终端管道:

Sender:发件人:

gst-launch-1.0 -v realsensesrc serial=$rs_serial timestamp-mode=clock_all enable-color=true ! rgbddemux name=demux demux.src_depth ! queue ! colorizer near-cut=300 far-cut=3000 ! rtpvrawpay ! udpsink host=192.168.100.80 port=9001

Receiver:接收者:

gst-launch-1.0 udpsrc port=9001 caps = "application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW, sampling=(string)RGB, depth=(string)8, width=(string)1280, height=(string)720, payload=(int)96" ! rtpvrawdepay ! videoconvert ! queue ! fpsdisplaysink sync=false

I can see the video using the above terminal pipeline in the receiver.我可以在接收器中使用上述终端管道观看视频。 BUT when I converted it to python code, the output is:但是当我将它转换为 python 代码时,output 是:

Could not open Video无法打开视频

gst_receiver.py gst_receiver.py

import cv2
import sys

video = cv2.VideoCapture(
    'udpsrc port=9001 ! application/x-rtp, media=(string)video, clock-rate=(int)90000, encoding-name=(string)RAW,'
    'sampling=(string)RGB, depth=(string)8, width=(string)1280, height=(string)720, payload=(int)96'
    ' ! rtpvrawdepay ! decodebin ! videoconvert ! queue ! appsink', cv2.CAP_GSTREAMER)

# video.set(cv2.CAP_PROP_BUFFERSIZE,3)
# Exit if video not opened.
if not video.isOpened():
    print("Could not open Video")
    sys.exit()

# Read first frame.
ok, frame = video.read()
if not ok:
    print('Cannot read Video file')
    sys.exit()

System:系统:

Sender-PC = Ubuntu 18.04
Receiver-PC = Windows 10
Python = 3.7.9
OpenCV = 4.5.5 

Check if your OpenCV has Gstreamer support by检查您的 OpenCV 是否支持 Gstreamer

print(cv2.getBuildInformation())

There should be应该有

Video I/O:
    FFMPEG:                      YES
      avcodec:                   YES (58.54.100)
      avformat:                  YES (58.29.100)
      avutil:                    YES (56.31.100)
      swscale:                   YES (5.5.100)
      avresample:                YES (4.0.0)
    GStreamer:                   YES (1.16.2)
    v4l/v4l2:                    YES (linux/videodev2.h)
    Intel Media SDK:             YES (/mnt/nfs/msdk/lin-18.4.1/lib64/libmfx.so)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM