Click here to Skip to main content
11,702,168 members (65,338 online)
Click here to Skip to main content

MicroDVR - Simple Video Security Using Webcams

, 2 Mar 2012 CPOL 45.4K 62
Rate this:
Please Sign up or sign in to vote.
A simple video-security application that allows video capture from PC webcams, detect motion, record videos on local storage using .NET 3.5, AForge and FFMpeg.

Introduction

This is a simple application that allows the user to use the available VideoCaptureDevices (Webcams) as security cameras. The interface allows the user to record the videocapture of a specific camera to a video file (.avi) to local storage (The recording path also can be set via the interface). The user can at anytime set the focus on any camera. For each camera, we can enable the Motion Detection, Beep on motion and Automatic recording on motion.

Background

The basic idea of this application is to create a low cost/cost free video security program for a rather small place (a small store in my case) that covers the basic DVR functionalities (Motion detection, recording).

Code Explanation

a - The CameraMonitor Class

The "CameraMonitor" class is the essencial core of this application, as it provides the interface to control the "VideoCaptureDevice" as well as the other functions like Video Recording and motion detection.

here's the class's public members and properties :

The way we can create "CameraMonitor" object is through its constructor:
new CameraMonitor(
		PictureBox display, // a PictureBox where we will be displaying the incoming frames
		string monikerString, // the monikerSring is the WMI Object Path of our VideoCaptureDevice
		String  cameraName // a camera name (used for naming the video files recorded)
		);
The CameraMonitor first creates the "VideoCaptureDevice" using the monikerString passed to it, then sets the event handler for new incoming frames and finally it starts the Video capture.
cam = new VideoCaptureDevice(monikerString);
cam.NewFrame += new NewFrameEventHandler(cam_NewFrame); // the method "cam_NewFrame" is called when a new frame arrives
cam.Start(); // starts the videoCapture 
It also creates the MotionDetector:
md = new MotionDetector(new TwoFramesDifferenceDetector(), new MotionAreaHighlighting()); // creates the motion detector
Now when a NewFrameEvent is detected the "cam_NewFrame" method is ready to handle it, first it gets the BitMap from the new frame , then it displays it on the PictureBox for the user.
Bitmap bit = (Bitmap)eventArgs.Frame.Clone(); // get a copy of the BitMap from the VideoCaptureDevice
this.display.Image = (Bitmap)bit.Clone(); // displays the current frame on the main form

The Motion Detection Algorithm

If the motion detection is activated by the user, "cam_NewFrame" executes the code below:
if (this.MotionDetection && !this.motionDetected)
{
    // if motion detection is enabled and there werent any previous motion detected
    Bitmap bit2 = (Bitmap)bit.Clone(); // clone the bits from the current frame

    if (md.ProcessFrame(bit2) > 0.001) // feed the bits to the MD 
    {
        if (this.calibrateAndResume > 3)
        {
            // if motion was detected in 3 subsequent frames
            Thread th = new Thread(MotionReaction);
            th.Start(); // start the motion reaction thread
        }
        else this.calibrateAndResume++;
    }
}
Note : The "calibrateAndResume" counter is added to make sure that there is a real motion going on (withing 3 subsequent frames). Before, the motion detector would trigger even for a small change in the room lighting.

If Motion is detected, a new Thread is launched to do whatever needs to done (display a message and/or beep and/or start recording).

private void MotionReaction()
        {
            this.motionDetected = true; // pauses motion detection for a while
            if (this.RecordOnMotion)
            {
                this.StartRecording(); // record if Autorecord is toggled
            }
            if (this.BeepOnMotion)
            {
                // beep if BeepOnMotion is toggeled
                System.Console.Beep(400, 500);
                System.Console.Beep(800, 500);
            }
            
            Thread.Sleep(10000); // the user is notified for 10 seconds
            calibrateAndResume = 0;
            this.motionDetected = false; // resumes motion detection
            Thread.Sleep(3000);
            // the thread waits 3 seconds if there is no motion detected we stop the AutoRecord
            if (!this.forceRecord && this.motionDetected == false)
            {	
				// if the motion has totally stopped we stop AutoRecording
                this.StopRecording();
            }

        }

Video Recording

To record videos we're using a Bitmap Queue to store the frames coming from the camera, then the recorder thread will dequeue frame by frame and write it to a file using a "VideoFileWriter"
private void DoRecord()
        {
            // we set our VideoFileWriter as well as the file name, resolution and fps
            VideoFileWriter writer = new VideoFileWriter();
            writer.Open(RecordingPath+"\\" + this.cameraName +String.Format("{0:_dd-M-yyyy_hh-mm-ss}",DateTime.Now) +".avi", this.Width, this.Height, 30);
            
            // as long as we're recording 
            // we dequeue the BitMaps waiting in the Queue and write them to the file
            while (IsRecording)
            {
                if (frames.Count > 0)
                {
                    Bitmap bmp = frames.Dequeue();
                    writer.WriteVideoFrame(bmp);
                }
            }
            writer.Close();
        }
Note: we can always set the recording path via the public property "RecordingPath". Note: here the FrameRate is not properly set as it must be retrieved from the camera FrameRate, this will be noticed in the output videofile (speed not accurate).

b - The user interface (MainForm Class)

Using the CameraMonitor

When the form loads, it fetches the FilterInfoCollection array which contain the monikerString strings required to start the VideoCaptureDevice in the CameraMonitor. At this point the available cameras will be working and displaying video on the PictureBox controls we passed to them.
webcam = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            // we create our CameraMonitors
            for (int i = 0; i < webcam.Count && i<4; i++)
            {
                this.CamMonitor[i] = new CameraMonitor(this.DisplayReference[i],webcam[i].MonikerString,"Camera"+(i+1));
                // Enable the user controls coressponding to the CameraMonitor
                this.camPanels[i].Enabled = true;
                this.camOptions[i].Enabled = true;
            }

Saving and Loading User Options

The last part is making the application remember stuff like the recording path or which cam was detecting motion etc., so when the user runs the application, he would not have to RESET all options manually. To do this, I've created a Config DataSet that stors associations ( KEY => VALUE ) where we'll save user option upon aplication exit, and reload them at application run using the DataSet.WriteXml() and DataSet.ReadXml()

Example: fetching options from the user interface and saving them to an XML file:

try
    {
        // we try to get the option record by key
        DataRow r = this.config.Options.Select("Key = 'Camera1'")[0];
        // then we retrieve the value from the user control
        r[1] = ((!this.MotionDetection1.Checked) ? "0" : "1") +
            ((!this.AutoRecord1.Checked) ? "0" : "1") +
            ((!this.BeepOnMotionCheck1.Checked) ? "0" : "1");
    }
    catch (Exception ex) // if somthing goes wrong (ie. Option key is not found)
    {
        // we create a new Option record
        this.config.Options.AddOptionsRow("Camera1",
            ((!this.MotionDetection1.Checked) ? "0" : "1") +
            ((!this.AutoRecord1.Checked) ? "0" : "1") +
            ((!this.BeepOnMotionCheck1.Checked) ? "0" : "1"));
    }
// finally we write everyting to an xml file
    this.config.WriteXml("config.xml");
Example: fetching options from XML File and applying them to the user interface:
this.config.ReadXml("config.xml");
	try
            {
                // we try to get the option by its Key
                DataRow r = this.config.Options.Select("Key = 'Camera1'")[0];
                string option = r[1].ToString();
                // we apply changes to the user interface
                this.MotionDetection1.Checked = (option[0] == '0') ? false : true;
                this.AutoRecord1.Checked = (option[1] == '0') ? false : true;
                this.BeepOnMotionCheck1.Checked = (option[2] == '0') ? false : true;
            }
            catch (Exception ex) { }

Points of Interest

As a programmer, this application enabled me to scratch the surface to working with Video using AForge, writing video files (FFMpeg),playing with some image processing, so all in all, its good programming experience!

History

First Release : Feb 2012.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Osman Kalache
Software Developer Smart Solutions Médéa
Algeria Algeria
No Biography provided

You may also be interested in...

Comments and Discussions

 
Questionispy.Video.ffmpeg Pin
Member 24522045-Apr-15 13:20
memberMember 24522045-Apr-15 13:20 
Questionmemory leak (picturebox) Pin
coderaw11-Mar-15 4:02
membercoderaw11-Mar-15 4:02 
Questionthank Pin
saajan123453-Jul-14 15:53
membersaajan123453-Jul-14 15:53 
QuestionAforge.Video.Ffmpeg error Pin
Member 1066213130-Apr-14 14:15
memberMember 1066213130-Apr-14 14:15 
GeneralMy vote of 5 Pin
Ahmed Bensaid27-Feb-14 23:02
professionalAhmed Bensaid27-Feb-14 23:02 
Generalthanks Pin
whble14-Nov-13 1:19
memberwhble14-Nov-13 1:19 
QuestionUnknown DLLs Pin
Tuner-Test-Coder24-Oct-13 3:09
memberTuner-Test-Coder24-Oct-13 3:09 
AnswerRe: Unknown DLLs Pin
Osman Kalache3-Nov-13 5:30
memberOsman Kalache3-Nov-13 5:30 
GeneralMy vote of 5 Pin
Tuner-Test-Coder24-Oct-13 2:19
memberTuner-Test-Coder24-Oct-13 2:19 
QuestionCamera's URL Pin
r.bodini19-Sep-13 4:03
memberr.bodini19-Sep-13 4:03 
AnswerRe: Camera's URL Pin
Osman Kalache19-Sep-13 4:29
memberOsman Kalache19-Sep-13 4:29 
GeneralRe: Camera's URL Pin
r.bodini19-Sep-13 4:35
memberr.bodini19-Sep-13 4:35 
GeneralRe: Camera's URL Pin
Osman Kalache19-Sep-13 4:50
memberOsman Kalache19-Sep-13 4:50 
AnswerRe: Camera's URL Pin
briccardo95619-Sep-13 5:23
memberbriccardo95619-Sep-13 5:23 
GeneralMy vote of 5 Pin
hendi29-Jun-13 11:09
memberhendi29-Jun-13 11:09 
QuestionDisplay properties for cameras Pin
BoringPortlander8-Apr-13 16:28
memberBoringPortlander8-Apr-13 16:28 
AnswerRe: Display properties for cameras Pin
Osman Kalache8-Apr-13 23:32
memberOsman Kalache8-Apr-13 23:32 
GeneralRe: Display properties for cameras Pin
BoringPortlander9-Apr-13 10:41
memberBoringPortlander9-Apr-13 10:41 
GeneralRe: Display properties for cameras Pin
BoringPortlander9-Apr-13 11:36
memberBoringPortlander9-Apr-13 11:36 
QuestioncalibrateAndResume question Pin
Jacob Pachansky4-Nov-12 11:12
memberJacob Pachansky4-Nov-12 11:12 
AnswerRe: calibrateAndResume question Pin
Osman Kalache28-Nov-12 15:26
memberOsman Kalache28-Nov-12 15:26 
QuestionIs it possible to save MPEG4? Pin
gxdata23-Aug-12 22:08
membergxdata23-Aug-12 22:08 
AnswerRe: Is it possible to save MPEG4? Pin
Osman Kalache18-Sep-12 1:17
memberOsman Kalache18-Sep-12 1:17 
AnswerRe: Is it possible to save MPEG4? Pin
Member 930431025-Jan-13 15:29
memberMember 930431025-Jan-13 15:29 
Questionmy vote of 5 Pin
muhamad yousef4-Jun-12 22:35
membermuhamad yousef4-Jun-12 22:35 
QuestionError code when recording Pin
Wilksey-373-Mar-12 6:19
memberWilksey-373-Mar-12 6:19 
AnswerRe: Error code when recording Pin
Osman Kalache3-Mar-12 11:27
memberOsman Kalache3-Mar-12 11:27 
GeneralRe: Error code when recording Pin
Wilksey-373-Mar-12 12:40
memberWilksey-373-Mar-12 12:40 
AnswerRe: Error code when recording Pin
finnishflash6-Aug-12 5:03
memberfinnishflash6-Aug-12 5:03 
AnswerRe: Error code when recording Pin
Kazidub11-Mar-12 1:25
memberKazidub11-Mar-12 1:25 
GeneralRe: Error code when recording Pin
gxdata23-Aug-12 22:05
membergxdata23-Aug-12 22:05 
AnswerRe: Error code when recording Pin
vaughanatworld16-Apr-12 3:53
membervaughanatworld16-Apr-12 3:53 
GeneralRe: Error code when recording Pin
vaughanatworld16-Apr-12 9:55
membervaughanatworld16-Apr-12 9:55 
GeneralRe: Error code when recording Pin
vaughanatworld16-Apr-12 10:09
membervaughanatworld16-Apr-12 10:09 
QuestionError 1 The type 'WebcamSecurity.Config' already contains a definition for 'OptionsRowChangeEvent' Error 1 The type 'WebcamSecurity.Config' already contains a definition for 'OptionsRowChangeEvent' E:\WebcamSecurity\WebcamSecurity\Config.Designer.cs Pin
fdasfsafasf3-Mar-12 2:09
memberfdasfsafasf3-Mar-12 2:09 
AnswerRe: Error 1 The type 'WebcamSecurity.Config' already contains a definition for 'OptionsRowChangeEvent' Error 1 The type 'WebcamSecurity.Config' already contains a definition for 'OptionsRowChangeEvent' E:\WebcamSecurity\WebcamSecurity\Config.Designer Pin
Osman Kalache3-Mar-12 2:57
memberOsman Kalache3-Mar-12 2:57 
GeneralRe: Error 1 The type 'WebcamSecurity.Config' already contains a definition for 'OptionsRowChangeEvent' Error 1 The type 'WebcamSecurity.Config' already contains a definition for 'OptionsRowChangeEvent' E:\WebcamSecurity\WebcamSecurity\Config.Designer Pin
fdasfsafasf3-Mar-12 18:39
memberfdasfsafasf3-Mar-12 18:39 
QuestionVery Good, and a question Pin
Dewey2-Mar-12 21:20
memberDewey2-Mar-12 21:20 
AnswerRe: Very Good, and a question Pin
Osman Kalache3-Mar-12 0:55
memberOsman Kalache3-Mar-12 0:55 
QuestionWhere is the codes? Pin
BigWCat2-Mar-12 10:21
memberBigWCat2-Mar-12 10:21 
AnswerRe: Where is the codes? Pin
Osman Kalache2-Mar-12 11:32
memberOsman Kalache2-Mar-12 11:32 
GeneralRe: Where is the codes? Pin
Osman Kalache2-Mar-12 12:23
memberOsman Kalache2-Mar-12 12:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 2 Mar 2012
Article Copyright 2012 by Osman Kalache
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid