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!
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.