[英]C# Emgucv turn off webcam
目前我的程序可以打开网络摄像头然后动态捕捉人脸,但是,我不知道如何停止摄像头,因为即使 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 问题已经解决。 但是,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);
}
您已经解决了该问题,应该调用Dispose方法。
CameraCapture实现了DisposableObject,您不应将其作为静态变量,而应将其保留为变量并在完成后处置。
我看到您说它“有效,但由于内存问题而崩溃”,如果仍然存在问题,请在下面描述内存问题的问题或评论中发表。
我可以注意到这个代码挑战是旧的,并且在本文发布时没有太多的解决方案响应。 但是,提供的响应并不能真正解决问题。 我遇到了同样的问题,并找到了一种方法来避免内存 NullReferenceError。 为了方便起见,我将在这里使用我自己的代码,但挑战是相同的,所以它适用。 选择适用于您的实例的代码部分。
我的观察
任何 Bitmap bitmap、object 都必须正确处理 (bitmap.Dispose()) 以使 ZCD69B4957F06CDE808D7 过载天然垃圾收集器似乎不会在其 function 末尾捡起它。
Emgu.CV.Capture _capture; object 也必须处理 (_capture.Dsipose()) 但必须使用 boolean 控制来完成以避免 NullReference 错误。
公共部分 class 主要:Form { bool isStreaming; 布尔相机; 捕获_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.