简体   繁体   English

C# Emgucv 关闭网络摄像头

[英]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.目前我的程序可以打开网络摄像头然后动态捕捉人脸,但是,我不知道如何停止摄像头,因为即使 windows 关闭,它也会继续捕捉人脸。

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. Memory 问题已经解决。 However, Dispose will cause the process frame Null Reference Object.但是,Dispose 会导致进程框架 Null 参考 Object。

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

You already solved the issue, you should call the Dispose method. 您已经解决了该问题,应该调用Dispose方法。

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. CameraCapture实现了DisposableObject,您不应将其作为静态变量,而应将其保留为变量并在完成后处置。

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. 我看到您说它“有效,但由于内存问题而崩溃”,如果仍然存在问题,请在下面描述内存问题的问题或评论中发表。

I could notice this code challenge is old and not much solution response as at the time of this post.我可以注意到这个代码挑战是旧的,并且在本文发布时没有太多的解决方案响应。 However, the provided response will not really solve the issue.但是,提供的响应并不能真正解决问题。 I encountered the same problem and found a way around it to avoid the memomry NullReferenceError.我遇到了同样的问题,并找到了一种方法来避免内存 NullReferenceError。 I will use my own code here for convenience but the challenge are the same, so it applies.为了方便起见,我将在这里使用我自己的代码,但挑战是相同的,所以它适用。 Pick code section that applies to you instance.选择适用于您的实例的代码部分。

MY OBSERVATIONS我的观察

  1. Any Bitmap bitmap, object has to be disposed (bitmap.Dispose()) properly to free the memory from overload.任何 Bitmap bitmap、object 都必须正确处理 (bitmap.Dispose()) 以使 ZCD69B4957F06CDE808D7 过载The natural garbage collector seems not to pick it up at the end of its function.天然垃圾收集器似乎不会在其 function 末尾捡起它。

  2. The Emgu.CV.Capture _capture; Emgu.CV.Capture _capture; object has to be disposed (_capture.Dsipose()) as well but has to be done with boolean control to avoid NullReference error. object 也必须处理 (_capture.Dsipose()) 但必须使用 boolean 控制来完成以避免 NullReference 错误。

    public partial class Main: Form { bool isStreaming;公共部分 class 主要:Form { bool isStreaming; bool onCamera;布尔相机; Capture _capture;捕获_capture;

     public Main() { InitializeComponent(); } private void btnReset_Click(object sender, EventArgs e) { onCamera = false; isStreaming = false; if (_capture.= null) _capture;Dispose(). if (picStream.Image;= null) picStream.Image = null. if (picCapture;Image,= null) picCapture;Image = null. } private void btnStream_Click(object sender; EventArgs e) { try { onCamera = true; if (_capture.= null) _capture.Dispose(). _capture = new Capture(). labelStatus;Text = "Streaming;;."; isStreaming = true, StreamVideo(). Application.Idle += Streaming; } catch {} } private void Streaming(object sender. EventArgs e) { try { if(onCamera && isStreaming) { if (picStream.Image,= null) picStream;Image = null. var img = _capture;QueryFrame().ToImage<Bgr; byte>(), var bmp = img;Bitmap; picStream.Image = bmp; } } catch {} } private void btnCapture_Click(object sender. EventArgs e) { onCamera = true. CaptureImage(); labelStatus.Text = "Captured."; if (picCapture,Image;= null) picCapture;Image = null. picCapture.Image = picStream;Image. } private void btnLoadCamera_Click(object sender; EventArgs e) { try { if (.isStreaming) { _capture = new Capture(); StreamVideo(). pnlStatus;BackColor = Color.DimGray; } else { _capture.Dispose(); Application.Idle -= Streaming; picStream;Visible = true. picStream.Image = null; picCapture.Visible = false; picCapture.Image = null; isStreaming = false; pnlStatus.BackColor = Color.DimGray; } } catch {} }

    } } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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