Note that there are some explanatory texts on larger screens.

plurals
  1. PORecording from a Webcam based on motion. wpf using aforge
    text
    copied!<p>I have made a simple program that displays video from a webcam and can detect motion from the feed, but I am having a problem understanding how I go about recording from the webcam when motion is detected. I have searched on the Aforge.Net site but I am finding it hard to understand what I need to do. When motion is detected, I want to start recording to a pre determined destination and when no motion is detected, I want to continue recording for a set period of time and then stop. This is part of my program that I have so far...</p> <p>Please can you help to explain it to me and let me know if I need to supply more info or code. Thanks</p> <p>$// Open video source</p> <pre><code> 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 &lt; 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 &lt; 50) &amp;&amp; (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) &gt; 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) &lt; motionAlarmLevel) { videoSourcePlayer.BorderColor = System.Drawing.Color.Black; Dispatcher.BeginInvoke(new ThreadStart(delegate { lblMotionDetected.Content = "No Motion Detected"; })); } // if (motionLevel &gt; motionAlarmLevel) // { // // flash for 2 seconds // timer.Start(); // } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload