j0e*_*1in 7 c++ opencv gstreamer
我想在gstreamer管道中添加一些opencv进程,然后通过udpsink发送它.
我可以像这样从gstreamer读取帧:
// may add some plugins to the pipeline later
cv::VideoCapture cap("v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! appsink");
cv::Mat frame;
while(ture){
cap >> frame;
// do some processing to the frame
}
Run Code Online (Sandbox Code Playgroud)
但无法弄清楚的是如何将已处理的帧传递给以下管道: appsrc ! x264enc ! mpegtsmux ! udpsink host=localhost port=5000
我试过了
cv::VideoWriter writer = cv::VideoWriter("appsrc ! x264enc ! mpegtsmux ! udpsink host=localhost port=5000", 0, (double)30, cv::Size(640, 480), true);
writer << processedFrame;
Run Code Online (Sandbox Code Playgroud)
但是,接收方没有收到任何信息.(我使用管道$gst-launch-1.0 udpsrc port=5000 ! tsparse ! tsdemux ! h264parse ! avdec_h264 ! videoconvert ! ximagesink sync=false
作为接收器)
我的问题是,我可以将已处理的opencv Mat传递给gstreamer管道并让它进行一些编码,然后通过udpsink通过网络发送吗?如果是,我该如何实现?
附带问题,有什么办法可以调试VideoWriter吗?例如检查帧是否实际写入其中.
请注意,我在ubuntu 14.04上使用opencv 2.4.12和gstreamer 1.2.
任何帮助都很棒!
编辑:
为了提供更多信息,我测试了以下代码,它给了GStreamer Plugin: Embedded video playback halted; module appsrc0 reported: Internal data flow error.
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
int main(int argc, char *argv[]){
cv::VideoCapture cap("v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! appsink");
if (!cap.isOpened()) {
printf("=ERR= can't create capture\n");
return -1;
}
cv::VideoWriter writer;
// problem here
writer.open("appsrc ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! autovideoconvert ! ximagesink sync=false", 0, (double)30, cv::Size(640, 480), true);
if (!writer.isOpened()) {
printf("=ERR= can't create writer\n");
return -1;
}
cv::Mat frame;
int key;
while (true) {
cap >> frame;
if (frame.empty()) {
printf("no frame\n");
break;
}
writer << frame;
key = cv::waitKey( 30 );
}
cv::destroyWindow( "video" );
}
Run Code Online (Sandbox Code Playgroud)
显然,appsrc管道有问题,但我不知道出了什么问题,因为管道gst-launch-1.0 v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! ximagesink sync=false
工作正常.
j0e*_*1in 15
经过几个小时的搜索和测试,我终于得到了答案.关键是只videoconvert
在使用之后appsrc
,不需要设置上限.因此,编写器管道看起来像appsrc ! videoconvert ! x264enc ! mpegtsmux ! udpsink host=localhost port=5000
.
下面是一个示例代码,它从gstreamer管道读取图像,执行一些opencv图像处理并将其写回管道.
使用此方法,您可以轻松地将任何opencv进程添加到gstreamer管道.
// Compile with: $ g++ opencv_gst.cpp -o opencv_gst `pkg-config --cflags --libs opencv`
#include <stdio.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv) {
// Original gstreamer pipeline:
// == Sender ==
// gst-launch-1.0 v4l2src
// ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB
// ! videoconvert
// ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4
// ! mpegtsmux
// ! udpsink host=localhost port=5000
//
// == Receiver ==
// gst-launch-1.0 -ve udpsrc port=5000
// ! tsparse ! tsdemux
// ! h264parse ! avdec_h264
// ! videoconvert
// ! ximagesink sync=false
// first part of sender pipeline
cv::VideoCapture cap("v4l2src ! video/x-raw, framerate=30/1, width=640, height=480, format=RGB ! videoconvert ! appsink");
if (!cap.isOpened()) {
printf("=ERR= can't create video capture\n");
return -1;
}
// second part of sender pipeline
cv::VideoWriter writer;
writer.open("appsrc ! videoconvert ! x264enc noise-reduction=10000 tune=zerolatency byte-stream=true threads=4 ! mpegtsmux ! udpsink host=localhost port=9999"
, 0, (double)30, cv::Size(640, 480), true);
if (!writer.isOpened()) {
printf("=ERR= can't create video writer\n");
return -1;
}
cv::Mat frame;
int key;
while (true) {
cap >> frame;
if (frame.empty())
break;
/* Process the frame here */
writer << frame;
key = cv::waitKey( 30 );
}
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.;)