0

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

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

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

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')

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 is always return None after calling ret, frame = cap.read().

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

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 :/)

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

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

Any help is much appreciated!

2
  • See this answer. Commented Jul 6, 2018 at 15:45
  • Hello, my opencv was compiled with Gstreamer support, but unfortunately: frame is also always None after calling ret, frame = cap.read() @Oncel Umut TURER: I am exactly in the same position as you were: the streming works with gstreamer in command line, but I cannot make it work with python. Did you find a solution at the end ? Commented Mar 19, 2021 at 18:55

2 Answers 2

3

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

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.

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 wipe all existing binaries.

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

pip unistall opencv-python

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. I've done a similar thing after having not found an answer and it worked. However, I've been busy and couldn't post an answer here. Therefore thank you for the detailed explanations
Dependency setup can be a pain... Glad I could help.
0

I can answer the question on how to find out, if you have opencv installed with gstreamer support.

Paste this in your Python script:

print(cv2.getBuildInformation())

You should find something like that:

    Video I/O:
...
    GStreamer:                   
      base:                      YES (ver 1.8.3)
      video:                     YES (ver 1.8.3)
      app:                       YES (ver 1.8.3)
...

If it is not YES, you must reinstall opencv with gstreamer support.

1 Comment

It says NO :( So how can I install opencv with GStreamer support? I think installing them seperately is not working :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.