Delay in preview of webcam, FPS drop on live preview and In video recording, video get pixelated when user select high resolution(1920 * 1080 : 60FPS)

303 views Asked by At

Issue 1: Delay in preview of webcam, FPS drop on live preview

On high resolution i am facing issue of delay in preview of webcam, FPS drop on live preview in AForge.Video.DirectShow(Version=2.2.5.0). I am using AForge.Video.DirectShow for getting camera feed in WPF c# application. On high resolution(1920 * 1080 : 60 FPS) user can observe delay in preview of webcam. On normal resolution preview is look good. But when user select higher resolution current frame rate is drooping and user can observe delay in preview of webcam.

This is event for get camera frame and assign to picture box and calculation for get framerate

public void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        //// make the FPS measurement
        stopwatch.Stop();
        TimeSpan ts = stopwatch.Elapsed;
        stopwatch.Restart();

        // this is called exponential smoothing. The weight for the previous
        // avgFramePeriod and the weight for the new measurement should equal 1
        avgFramePeriod = 0.9 * avgFramePeriod + 0.1 * ts.TotalSeconds;
        try
        {
            previewBox.Image = (Bitmap)eventArgs.Frame.Clone();
            _latestFrame = (Bitmap)eventArgs.Frame.Clone();
            if (_recording && _writer.IsOpen)
            {
                if (_firstFrameTime != null)
                {
                    _writer.WriteVideoFrame(_latestFrame, DateTime.Now - _firstFrameTime.Value);
                }
                else
                {
                    _writer.WriteVideoFrame(_latestFrame);
                    _firstFrameTime = DateTime.Now;
                }
            }
        }
        catch { }

        // suggest the garbage collector run -- with high res images, if you don't 
        // do this, it can run out of memory
        GC.Collect();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (this.Visible)
        {
            double avgFPS = 1.0 / avgFramePeriod;
            fpsValStatusLabel.Text = avgFPS.ToString("0.##");
        }
    }

Issue 2 video get pixelated when user select high resolution(1920 * 1080 : 60 FPS) For video recording i have used Accord.Video.FFMPEG.VideoFileWriter(Version=3.8.0.0) package but in this video get pixelated when user select high resolution(1920 * 1080 : 60 FPS) In this screen shoot we can see that its pixelated video on high resolution(1920 * 1080 : 60 FPS)

This is button click event where I am creating VideoFileWriter object for creating video

private void button1_Click(object sender, EventArgs e)
    {
        _writer = new Accord.Video.FFMPEG.VideoFileWriter();
        string fileName = $"temp_{DateTime.Now.ToFileTime()}.mp4";
        string InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
        string pathToNexigoFolder = System.IO.Path.Combine(InitialDirectory, "Nexigo");
        if (!Directory.Exists(pathToNexigoFolder))
        {
            try
            {
                Directory.CreateDirectory(pathToNexigoFolder);
            }
            catch
            {
                pathToNexigoFolder = InitialDirectory;
            }
        }
        string filePath = System.IO.Path.Combine(pathToNexigoFolder, fileName);
        _writer.Open(filePath, _latestFrame.Width, _latestFrame.Height, 60, Accord.Video.FFMPEG.VideoCodec.MPEG4, 60);
        _recording = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _recording = false;
        _writer.Close();
        _writer.Dispose();

    }

This is call-back event from AForge.Video.DirectShow library for getting each frame from webcam

public void VideoCaptureDevice_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        //// make the FPS measurement
        stopwatch.Stop();
        TimeSpan ts = stopwatch.Elapsed;
        stopwatch.Restart();

        // this is called exponential smoothing. The weight for the previous
        // avgFramePeriod and the weight for the new measurement should equal 1
        avgFramePeriod = 0.9 * avgFramePeriod + 0.1 * ts.TotalSeconds;
        try
        {
            previewBox.Image = (Bitmap)eventArgs.Frame.Clone();
            _latestFrame = (Bitmap)eventArgs.Frame.Clone();
            if (_recording && _writer.IsOpen)
            {
                if (_firstFrameTime != null)
                {
                    _writer.WriteVideoFrame(_latestFrame, DateTime.Now - _firstFrameTime.Value);
                }
                else
                {
                    _writer.WriteVideoFrame(_latestFrame);
                    _firstFrameTime = DateTime.Now;
                }
            }
        }
        catch { }

        // suggest the garbage collector run -- with high res images, if you don't 
        // do this, it can run out of memory
        GC.Collect();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (this.Visible)
        {
            double avgFPS = 1.0 / avgFramePeriod;
            fpsValStatusLabel.Text = avgFPS.ToString("0.##");
        }
    }
0

There are 0 answers