在python中从opencv写入Gstreamer管道 [英] Write in Gstreamer pipeline from opencv in python

查看:71
本文介绍了在python中从opencv写入Gstreamer管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 gstreamer 从 opencv 流式传输一些图像,但我在管道方面遇到了一些问题.我一般是 gstreamer 和 opencv 的新手.我在树莓派 3 上用 gstreamer 为 python3 编译了 opencv 3.2.我有一个与 raspivid 一起使用的小 bash 脚本

I'm trying to stream some images form opencv using gstreamer and I got ome issues with the pipeline. I'm new to gstreamer and opencv in general. I compiled opencv 3.2 with gstreamer for python3 on a raspberry pi 3. I have a little bash script that I use with raspivid

raspivid -fps 25 -h 720 -w 1080 -vf -n -t 0 -b 2000000 -o - | gst-launch-1.0 -v fdsrc ! h264parse ! rtph264pay config-interval=1 pt=96 ! gdppay ! tcpserversink host=192.168.1.27 port=5000

我想翻译这个管道,以便从 opencv 使用它并将我的算法操作的图像输入其中.我做了一些研究,发现我可以使用带有 appsrc 而不是 fdsrc 的 videoWriter,但出现以下错误

I wanted to translate this pipeline in order to use it from opencv and feed into it images that my algorithm manipulates. I did some research and figured that I can use videoWriter with appsrc instead of fdsrc but I get the following error

GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error.

顺便说一下,我想出来的python脚本如下导入cv2

The python script that I came up with is the following by the way import cv2

cap = cv2.VideoCapture(0)


# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('appsrc  ! h264parse ! '
                      'rtph264pay config-interval=1 pt=96 ! '
                      'gdppay ! tcpserversink host=192.168.1.27 port=5000 ',
                      fourcc, 20.0, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:
        frame = cv2.flip(frame, 0)

        # write the flipped frame
        out.write(frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

管道中是否有任何错误?我不明白这个错误.我已经有一个可以从 bash 管道读取的 Python 客户端,从延迟和消耗资源的角度来看,结果非常好.

Is there any error in the pipeline? I don't understand the error. I already have a Python client that can read from the bash pipeline and the result are pretty good from the latency perspective and consumed resources .

推荐答案

我遇到了解决方案,我希望这能帮助遇到相同问题的其他人.管道布置错误,需要视频转换.另一方面,延迟非常相关,但将 speed.preset 设置为超快解决了这个问题,即使那里没有太多压缩,这是一个很好的折衷方案.这是我的解决方案.

I came across the solution and I hope this helps other people that come across the same issue. The pipeline was mistakenly arranged and videoconvert was needed. On the other hand the latency was quite relevant but setting speed.preset to ultrafast solved the issue even if there's not much of compression going on there, it was a good compromise. Here's my solution.

import cv2

cap = cv2.VideoCapture(0)

framerate = 25.0

out = cv2.VideoWriter('appsrc ! videoconvert ! '
                      'x264enc noise-reduction=10000 speed-preset=ultrafast tune=zerolatency ! '
                      'rtph264pay config-interval=1 pt=96 !'
                      'tcpserversink host=192.168.1.27 port=5000 sync=false',
                      0, framerate, (640, 480))

while cap.isOpened():
    ret, frame = cap.read()
    if ret:

        out.write(frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()

这篇关于在python中从opencv写入Gstreamer管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆