简体   繁体   中英

Windows named pipe to EmguCV VideoCapture

I want to show a h264 video stream in C# with emguCV . I send the stream from a raspberry pi with netcat , if I use netcat+vcl I can see the stream but not from the c# program (I'm new to emgucv).

NamedPipeServerStream ncatPipe = new NamedPipeServerStream(
    "ncatPipe",
    PipeDirection.InOut,
    NamedPipeServerStream.MaxAllowedServerInstances,
    PipeTransmissionMode.Message,
    PipeOptions.None,
    1024,
    1024
);

Process ncat = new Process();
ncat.StartInfo.FileName = "CMD.exe";
ncat.StartInfo.Arguments = "/C ncat -v -l 5000 >\\\\.\\pipe\\ncatPipe 2>nul";
ncat.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ncat.Start();

ncatPipe.WaitForConnection();
VideoCapture videoCapture = new VideoCapture("\\\\.\\pipe\\ncatPipe");
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FourCC, VideoWriter.Fourcc('h', '2', '6', '4'));
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 60);
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 480);
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 640);

Timer playerTimer = new Timer();
playerTimer.Interval = 1000 / 60;
playerTimer.Tick += new EventHandler((object sender1, EventArgs e1) =>
{
    Bitmap captureBmp = videoCapture.QueryFrame().Bitmap;
    if (captureBmp != null)
    {
        picture.Image = captureBmp;
    }
    else
    {
        picture.Image = null;
        playerTimer.Stop();

        ncatPipe.Disconnect();
        ncatPipe.Dispose();

        ncat.Kill();
        ncat.Dispose();
        videoCapture.Dispose();
    }
});
playerTimer.Start();

Exception thrown: 'Emgu.CV.Util.CvException' in Emgu.CV.World.dll

[ERROR:0] global D:\\bb\\cv_x86\\build\\opencv\\modules\\videoio\\src\\cap.cpp (122) cv::VideoCapture::open VIDEOIO(CV_IMAGES): raised unknown C++ exception!

EDIT 1: Now I'm trying to use FFMPEG to get the bitmap frame from the pipe.

EDIT 2: FFMPEG works but my latency is around 4000ms.

Mencoder solution latency: 200ms~ , even less latency incorporating mplayer gpu acc. window in the form. windows Mplayer & Mencoder build: http://mplayerwin.sourceforge.net/downloads.html

(setting the fps higher on the client auto fix the latency(with Mplayer & Mencoder))

Raspberry PI command:

raspivid -t 0 -n -o - -fl -md 4 -w 1296 -h 972 -fps 30 -ih | nc -u 192.168.137.1 5000

C#:

Process ncat = new Process();
ncat.StartInfo.FileName = "ncat\\ncat.exe";
ncat.StartInfo.Arguments = "-u -l 5000";
ncat.StartInfo.UseShellExecute = false;
ncat.StartInfo.CreateNoWindow = true;
ncat.StartInfo.RedirectStandardOutput = true;
ncat.Start();

Process mencoder = new Process();
mencoder.StartInfo.FileName = "mplayer\\mencoder.exe";
mencoder.StartInfo.Arguments = "-fps 60 -nosound - -ofps 30 -ovc raw -of rawvideo -vf format=bgr24 -really-quiet -o -";
mencoder.StartInfo.UseShellExecute = false;
mencoder.StartInfo.CreateNoWindow = true;
mencoder.StartInfo.RedirectStandardInput = true;
mencoder.StartInfo.RedirectStandardOutput = true;
mencoder.Start();

ncat.StandardOutput.BaseStream.CopyToAsync(mencoder.StandardInput.BaseStream);

byte[] raw = new byte[1296 * 972 * 3];
byte[,,] rawEmgu = new byte[972, 1296, 3];

while (true)
{
    mencoder.StandardOutput.BaseStream.Flush();
    mencoder.StandardOutput.BaseStream.Read(raw, 0, 1296 * 972 * 3);

    int n = 0;
    for (int y = 0; y < 972; y++)
    {
        for (int x = 0; x < 1296; x++)
        {
            rawEmgu[y, x, 0] = raw[n++];
            rawEmgu[y, x, 1] = raw[n++];
            rawEmgu[y, x, 2] = raw[n++];
        }
    }

    imageBox1.Image = new Image<Bgr, Byte>(rawEmgu);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM