问题
I'm trying to stream a video from a raspberry pi camera to my PC through a local network.
On the RaspberryPi side, I use gstreamer with the following command:
raspivid -n -t 0 -rot 270 -w 960 -h 720 -fps 30 -b 6000000 -o - | gst-launch-1.0 -e -vvvv fdsrc ! h264parse ! rtph264pay pt=96 config-interval=5 ! udpsink host=192.168.1.85 port=5000
And I use the following command on the PC side:
gst-launch-1.0 -e -v udpsrc port=5000 ! application/x-rtp, payload=96 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! fpsdisplaysink sync=false text-overlay=false
In this case, the video gets displayed and everything works fine. However, I would like to capture the stream with OpenCV in a C++ program. I'm trying to do this using OpenCV 3.2 compiled with gstreamer-1.0 32bit.
When I run the following code to try to capture the stream:
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
cv::VideoCapture cap("udpsrc port=5000 ! application/x-rtp, payload=96 ! rtpjitterbuffer ! rtph264depay ! avdec_h264 ! appsink");
return 0;
}
I get the following error:
GStreamer Plugin: Embedded video playback halted; module udpsrc0 reported: Internal data stream error.
OpenCV Error: Unspecified error (GStreamer: unable to start pipeline) in cvCaptureFromCAM_GStreamer, file:\opencv\modules\videoio\src\cap_gstreamer.cpp, line 832
I was able to receive other streams using OpenCV, but I can't get it to work with gstreamer. I searched for this problem, but I can't find anything helpful, so I would really appreciate some help.
回答1:
I have given this problem another try, and it works now. I'll post how I did it in the hope others will have an easier time.
I followed the instructions by wumpus to make it work. On the pc side I run:
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[])
{
cv::VideoCapture cap("udpsrc port=5000 ! gdpdepay ! rtph264depay ! avdec_h264 ! videoconvert ! appsink sync=false");
return 0;
}
And then I run on the raspberry pi:
gst-launch-1.0 rpicamsrc bitrate=3000000 ! video/x-h264, width=960, height=720 ! h264parse ! queue ! rtph264pay config-interval=1 pt=96 ! gdppay ! udpsink host=192.168.1.85 port=5000
To make this work you need to install gstreamer and rpicamsrc on the raspberry pi.
Also you need to have opencv compiled with gstreamer 32 bit on your pc. First install gstreamer, developer version, and then compile opencv. See this for help on compiling opencv in visual studio.
来源:https://stackoverflow.com/questions/41398886/capture-stream-from-raspberrry-pi-using-gstreamer-in-opencv