Code

The following snippet shows 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 something else
}
}

We now open our video source:

videoSourcePlayer.VideoSource = new AsyncVideoSource(source);

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

private void videoSourcePlayer_NewFrame(object sender, NewFrameEventArgsargs)
{
lock (this)
{
if (detector != null)
{
floatmotionLevel = 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 0.2, but you can use whatever you like. Once this threshold has been passed, you can do whatever logic you like such as send an email alert, text, start a video capture operation, 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