0
我已經做了一個簡單的程序,顯示來自攝像頭的視頻,並可以檢測來自飼料的運動,但我有一個問題,瞭解如何在檢測到運動時從網絡攝像頭進行記錄。我在Aforge.Net網站上搜索過,但我發現很難理解我需要做什麼。當檢測到動作時,我想開始記錄到預先確定的目的地,並且當沒有檢測到動作時,我想要繼續記錄一段時間,然後停止。這是我迄今爲止的計劃的一部分...基於運動從網絡攝像機錄製。 wpf使用aforge
請你幫忙解釋一下,如果我需要提供更多信息或代碼,請告訴我。由於
$ //打開視頻源
private void OpenVideoSource(IVideoSource source)
{
// set busy cursor
//this.Cursor = Cursors.WaitCursor;
// close previous video source
CloseVideoSource();
//motionDetectionType = 1;
//SetMotionDetectionAlgorithm(new TwoFramesDifferenceDetector());
//motionProcessingType = 1;
//SetMotionProcessingAlgorithm(new MotionAreaHighlighting());
// start new video source
videoSourcePlayer.VideoSource = new AsyncVideoSource(source);
videoSourcePlayer.NewFrame +=new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
//videoSourcePlayer.DesiredFrameRate = 30;
//webcam's default frame rate will be used instead of above code that has an error
// create new video file
writer.Open("test.avi", width, height, 25, VideoCodec.MPEG4);
// create a bitmap to save into the video file
Bitmap image = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// write 1000 video frames
for (int i = 0; i < 1000; i++)
{
image.SetPixel(i % width, i % height, System.Drawing.Color.Red);
writer.WriteVideoFrame(image);
}
writer.Close();
try
{
videoSourcePlayer.Start();
//motionAlarmLevel = sldMotionSensitivity.Value/100;
}
catch (Exception x)
{
System.Windows.MessageBox.Show(x.ToString());
}
//videoSource.DesiredFrameSize = new System.Drawing.Size(800,600);
videoSourcePlayer.BorderColor = System.Drawing.Color.Blue;
// reset statistics
statIndex = statReady = 0;
// start timers
clock.Start();
//alarm.Start();
videoSource = source;
//this.Cursor = Cursors.Default;
}
// Close current video source
private void CloseVideoSource()
{
// set busy cursor
//this.Cursor = Cursors.WaitCursor;
// stop current video source
videoSourcePlayer.SignalToStop();
// wait 2 seconds until camera stops
for (int i = 0; (i < 50) && (videoSourcePlayer.IsRunning); i++)
{
Thread.Sleep(100);
}
if (videoSourcePlayer.IsRunning)
videoSourcePlayer.Stop();
// stop timers
clock.Stop();
//alarm.Stop();
motionHistory.Clear();
// reset motion detector
if (detector != null)
detector.Reset();
//videoSourcePlayer.BorderColor = System.Drawing.Color.Red;
//this.Cursor = Cursors.Default;
}
// New frame received by the player
void videoSourcePlayer_NewFrame(object sender, ref Bitmap image)
{
lock (this)
{
if (detector != null)
{
//motion detected
if (detector.ProcessFrame(image) > motionAlarmLevel)
{
//float motionLevel = detector.ProcessFrame(image);
videoSourcePlayer.BorderColor = System.Drawing.Color.Red;
Dispatcher.BeginInvoke(new ThreadStart(delegate { lblMotionDetected.Content = "Motion Detected"; }));
//lblMotionDetected.Content = "Motion Detected";
//flash = (int)(2 * (1000/alarm.Interval));
}
// no motion detected
if (detector.ProcessFrame(image) < motionAlarmLevel)
{
videoSourcePlayer.BorderColor = System.Drawing.Color.Black;
Dispatcher.BeginInvoke(new ThreadStart(delegate { lblMotionDetected.Content = "No Motion Detected"; }));
}
// if (motionLevel > motionAlarmLevel)
// {
// // flash for 2 seconds
// timer.Start();
// }
}
}
}