Adding detection to your application

Here's a simple example of all you need to do to add video recognition to your application. As you can see, it couldn't be any easier!

// create motion detector
MotionDetector detector = new MotionDetector(
new SimpleBackgroundModelingDetector(),
new MotionAreaHighlighting());
// continuously feed video frames to motion detector
while ()
{
// process new video frame and check motion level
if (detector.ProcessFrame(videoFrame) > 0.02)
{
// ring alarm or do somethng else
}
}
Opening our video source
videoSourcePlayer.VideoSource = new AsyncVideoSource(source);

When we receive a new video frame, that's when all the magic happens. Here's all the code it takes to make processing a new video frame a success:

private void videoSourcePlayer_NewFrame(object sender,  
NewFrameEventArgs args)
{
lock (this)
{
if (detector != null)
{
float motionLevel = detector.ProcessFrame(args.Frame);
if (motionLevel > motionAlarmLevel)
{
// flash for 2 seconds
flash = (int)(2 * (1000 / alarmTimer.Interval));
}
// check objects' count
if (detector.MotionProcessingAlgorithm is BlobCountingObjectsProcessing)
{
BlobCountingObjectsProcessing countingDetector =
(BlobCountingObjectsProcessing)
detector.MotionProcessingAlgorithm;
detectedObjectsCount = countingDetector.ObjectsCount;
}
else
{
detectedObjectsCount = -1;
}
// accumulate history
motionHistory.Add(motionLevel);
if (motionHistory.Count > 300)
{
motionHistory.RemoveAt(0);
}
if (showMotionHistoryToolStripMenuItem.Checked)
DrawMotionHistory(args.Frame);
}
}

The key here is detecting the amount of motion that is happening in the frame, which is done with the following code. For this example, we are using a motion alarm level of  2, but you can use whatever you like. Once this threshold has been passed, you can implement your desired logic, such as sending an alert email, a text, and starting video capture, and so forth:

float motionLevel = detector.ProcessFrame(args.Frame);
if (motionLevel > motionAlarmLevel)
{
// flash for 2 seconds
flash = (int)(2 * (1000 / alarmTimer.Interval));
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.12.207