简体   繁体   English

使用 GStreamer 创建一个简单的 OpenCV 管道

[英]Create a simple OpenCV pipeline with GStreamer

I have a pipeline to stream images over UDP我有一条到 UDP 上的 stream 图像的管道


fps = 20
width = 500
height = 500

out_send = cv2.VideoWriter(
        "appsrc ! videoconvert ! "
        "video/x-raw,format=I420 ! "
        "jpegenc ! rtpjpegpay !"
        "udpsink host=127.0.0.1 port=5000",
        cv2.CAP_GSTREAMER, 0, fps, (width, height), True
    )

while True:
    frame = np.random.randint(255, size=(height, width, 3), dtype=np.uint8)

    out_send.write(frame)

    time.sleep(0.05)

This starts the pipeline but I am unable to receive using the following pipeline.这将启动管道,但我无法使用以下管道接收。 The streaming wont begin, just hangs.流媒体不会开始,只是挂起。

gst-launch-1.0 udpsrc port=5000 ! application/x-rtp,media=video,payload=26,clock-rate=90000,encoding-name=JPEG ! rtpjpegdepay ! jpegdec ! videoconvert ! queue ! xvimagesink

However if the frame is captured from webcam as below但是,如果帧是从网络摄像头捕获的,如下所示

cap_send = cv2.VideoCapture(0)

fps = int(cap_send.get(cv2.CAP_PROP_FPS))
width = int(cap_send.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap_send.get(cv2.CAP_PROP_FRAME_HEIGHT))

while True:
    ret, frame = cap_send.read()

    if not ret:
        break

    out_send.write(frame)

Then the receiving pipeline starts and streams without any issues.然后接收管道启动并流式传输没有任何问题。

In both cases I see the following message on the receiving end在这两种情况下,我都会在接收端看到以下消息

Setting pipeline to PAUSED ...
Pipeline is live and does not need PREROLL ...
Pipeline is PREROLLED ...
Setting pipeline to PLAYING ...
New clock: GstSystemClock

Edit:编辑:

I ran the receiving pipeline with GST_DEBUG=3 and see the following output我用GST_DEBUG=3运行接收管道,看到以下 output

gst_rtp_jpeg_depay_process:<rtpjpegdepay0> discarding data packets received when we have no header

This only happens if the sending pipeline uses raw images / numpy arrays as above.仅当发送管道使用上述原始图像 / numpy arrays 时才会发生这种情况。

What am I doing incorrect here.我在这里做错了什么。 Very new to gstreamer and need some help对 gstreamer 非常陌生,需要一些帮助

Goal: To stream images generated in opencv, asynchronously.目标:异步生成 opencv 中生成的 stream 图像。

@Micka pointed me in the right direction. @Micka 为我指出了正确的方向。

The issue was with frame sizes.问题在于帧大小。 I have tried MJPEG and H264 and both need frame sizes (width/height) that are multiples of 8. Gstreamer will round them up to the closest 8th multiple.我尝试过 MJPEG 和 H264,两者都需要 8 的倍数的帧大小(宽度/高度)。Gstreamer 会将它们四舍五入到最接近的第 8 倍数。 If the height is rounded then there is jitter seen at the end of the frame.如果高度是四舍五入的,则在帧的末端会看到抖动。 If the width is rounded then a green screen is seen at the receiver.如果宽度是圆形的,那么在接收器上会看到一个绿屏。

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

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