Search code examples
c#webcamemgucv

C# Emgucv turn off webcam


currently my program can open the webcam then dynamic capture the human face, however, I have no idea how to stop the camera because it will keep capturing the face even the windows are closed.

private static VideoCapture _cameraCapture;

public VideoSurveilance()
    {
        InitializeComponent();
        Run();

    }


    void Run()
    {

        try
        {
            _cameraCapture = new VideoCapture();


        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);
            return;
        }

        _fgDetector = new 
        Emgu.CV.VideoSurveillance.BackgroundSubtractorMOG2();
        _blobDetector = new CvBlobDetector();
        _tracker = new CvTracks();

        Application.Idle += ProcessFrame;
    }

    private void btnStopCamera_Click(object sender, EventArgs e)
    {

        _cameraCapture.Pause();//not working
        _cameraCapture.Stop();//not working
        _cameraCapture.Dispose();//worked but crashed due to memory issue

        this.Close();

        faceManipulate fm = new faceManipulate();
        fm.Show();

Memory issue already solved. However, Dispose will cause the process frame Null Reference Object.

void ProcessFrame(object sender, EventArgs e)
    {
        Mat frame = _cameraCapture.QueryFrame();
        Mat smoothedFrame = new Mat();
        CvInvoke.GaussianBlur(frame, smoothedFrame, new Size(3, 3), 1); 
}

Solution

  • You already solved the issue, you should call the Dispose method.

    CameraCapture implements DisposableObject, you should not have it as a static variable, instead you should keep it as a variable and dispose when you are done with it.

    I saw that you said that it "worked but crashed due to memory issue", if this is still a problem post a question or comment below describing the memory issue.