如何使用opencv和python获取Gstreamer实时流? [英] How to get Gstreamer live stream using opencv and python?

查看:1388
本文介绍了如何使用opencv和python获取Gstreamer实时流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过使用终端中的以下代码从树莓派0流式传输实时USB摄像机供稿:

I am streaming a live usb camera feed from raspberry pi zero by using below code in terminal:

gst-launch-1.0 -v v4l2src device=/dev/video0 ! "image/jpeg,width=640, height=480, framerate=30/1" ! rtpjpegpay ! udpsink host=<MyRPiIPHere> port=5000

通过在终端中使用以下代码,我可以从我的计算机(ubuntu 18.4)上获得低延迟高帧率流:

And I can get that low-latency high framerate stream from my computer (ubuntu 18.4) without any problems by using below code in terminal:

gst-launch-1.0 -e -v udpsrc port=5000 ! application/x-rtp, encoding-name=JPEG, payload=26 ! rtpjpegdepay ! jpegdec ! autovideosink

但是,我想使用python和opencv获取该流,以便对图像进行一些操作.

However, I want to get that stream using python and opencv inorder to make some operations on the image.

当我尝试使用具有以下代码的cv2库获取图像时:

When I try to get image by using cv2 library with this code:

cap = cv2.VideoCapture('udpsrc port=5000 ! application/x-rtp, encoding-name=JPEG, payload=26 ! rtpjpegdepay ! jpegdec ! autovideosink')

它什么也没捉住.我还尝试过将各种类型的! videoconvert ! video/x-raw, format=(string)BGR ! appsink版本添加到视频捕获参数中.

it doesn't catch anything. I have also tried adding various types of ! videoconvert ! video/x-raw, format=(string)BGR ! appsink versions to videocapture parameters.

frame总是返回.

我分别安装了opencv(cv2 .__ version__ = 3.4.0)和gstreamer(gst-launch-1.0).

I have installed opencv (cv2.__version__ = 3.4.0) and gstreamer (gst-launch-1.0) seperately.

我为该解决方案进行了大量搜索,但找不到简单的解决方案.我读过类似您应该在gstreamer支持下构建opencv" 之类的内容.如何检查我的opencv是否具有gstreamer支持,以及如何在计算机上同时安装了opencv和gstreamer的情况下安全地使用gstreamer支持构建opencv(我不介意卸载它们,但是由于我的Linux知识不好一个应该给我逐步说明:/)

I search a lot for the solution but couldn't find an easy one. I have read things like "you should build opencv with gstreamer support". How do i check if my opencv has gstreamer support and how can I build opencv with gstreamer support safely having in mind that I have both opencv and gstreamer installed on the computer (I don't mind uninstalling them but since my linux knowledge is not good one should give me step-by-step instructions :/)

我有什么解决方案可以通过python和cv2库获得该流,而无需卸载重新构建opencv吗?

Is there any solution for me to get that stream with python and cv2 library without uninstalling rebuilding opencv?

如果需要在Gstreamer模块支持下重建opencv,如何安全卸载已安装的库并重建opencv?

If I need to rebuild opencv with Gstreamer module support how can I safely uninstall my installed libraries and rebuild opencv?

非常感谢您的帮助!

推荐答案

我本人也遇到了类似的问题,并且在调试它的过程中度过了美好的时光". 此处是我的相关文章.

I just went through a similar issue myself and had a 'good' time debugging it. Here is my related post.

您的问题没有提供有关如何安装cv2 python模块的离散见解.如果您使用pip来获取软件包,则无法为该软件包启用gstreamer.这是因为pip仅部署cv2软件包的预构建二进制文件,并且都不支持gstreamer.

Your question gives no discrete insight, as to how you installed your cv2 python module. In case you used pip to get the package, you can't enable gstreamer for the package. That is because pip only deploys pre-built binaries of the cv2 package and none of them support gstreamer.

您需要做的(确实很简单)是,按照

What you would need to do (it is indeed fairly easy) is, follow these instructions to build OpenCV from the source distribution.

当心不要犯与我相同的错误.使用cmake或ccmake配置内部版本时,请确保仅将WITH_GSTREAMER设置为ON.确保WITH_GSTREAMER_0_10设置为OFF.

Beware not to make the same mistake I did. When configuring the build using cmake or ccmake, make sure to only set WITH_GSTREAMER to ON. Make sure WITH_GSTREAMER_0_10 is set OFF.

此外,在从源代码进行安装之前,请确保先鞭打所有现有的二进制文件.

Also, before you do the installation from source, make sure to whipe all existing binaries.

对于cv2的pip安装将意味着:

For pip installations of cv2 that is going to mean something like:

pip unistall opencv-python

如果您已经从opencv源构建了二进制文件,则必须:

In case you already built binaries from the opencv source, you'll have to:

sudo make uninstall

文件夹中的

.然后继续进行下去.

from the <opencv-src>/build folder. Then go ahead and give it another go.

这篇关于如何使用opencv和python获取Gstreamer实时流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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