Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#

Intelligent Screen Saver

Rate me:
Please Sign up or sign in to vote.
3.87/5 (17 votes)
15 Aug 2007CPOL2 min read 183.2K   10.9K   111  
A utility to control screen saver on your computer using computer vision (human face detection), rather than idle timer.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using FaceDetection;
using DxCam;
namespace FaceDetection
{
	/// <summary>
	/// Summary description for frmFaceDetection.
	/// </summary>
	public class frmFaceDetection : System.Windows.Forms.Form
	{
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.NotifyIcon niSystemTrayIcon;
		private System.Windows.Forms.ContextMenu cmiMain;
		private System.Windows.Forms.MenuItem cmiPause;
		private System.Windows.Forms.MenuItem cmiResume;
		private System.Windows.Forms.MenuItem cmiRestore;
		private System.Windows.Forms.MenuItem cmiExit;
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.TrackBar tbrPollingTimer;
		private System.Windows.Forms.TrackBar tbrVoteCounts;
		private System.Windows.Forms.Timer tmrPolling;
		private System.Windows.Forms.Label label1;
		private DxCam.ctlDxCam ctlDxCam1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.RadioButton rbFront;
		private System.Windows.Forms.RadioButton rbProfile;
		private System.ComponentModel.IContainer components;

		public frmFaceDetection()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmFaceDetection());
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmFaceDetection));
			this.ctlDxCam1 = new DxCam.ctlDxCam();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.niSystemTrayIcon = new System.Windows.Forms.NotifyIcon(this.components);
			this.cmiMain = new System.Windows.Forms.ContextMenu();
			this.cmiPause = new System.Windows.Forms.MenuItem();
			this.cmiResume = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.cmiRestore = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.cmiExit = new System.Windows.Forms.MenuItem();
			this.tbrPollingTimer = new System.Windows.Forms.TrackBar();
			this.tbrVoteCounts = new System.Windows.Forms.TrackBar();
			this.tmrPolling = new System.Windows.Forms.Timer(this.components);
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.panel1 = new System.Windows.Forms.Panel();
			this.rbProfile = new System.Windows.Forms.RadioButton();
			this.rbFront = new System.Windows.Forms.RadioButton();
			((System.ComponentModel.ISupportInitialize)(this.tbrPollingTimer)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.tbrVoteCounts)).BeginInit();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// ctlDxCam1
			// 
			this.ctlDxCam1.BackColor = System.Drawing.Color.Black;
			this.ctlDxCam1.Location = new System.Drawing.Point(8, 8);
			this.ctlDxCam1.Name = "ctlDxCam1";
			this.ctlDxCam1.Size = new System.Drawing.Size(208, 152);
			this.ctlDxCam1.TabIndex = 0;
			this.ctlDxCam1.TabStop = false;
			this.ctlDxCam1.NewFrameEvent += new DxCam.NewFrameEventHandler(this.pbPicture_NewFrameEvent);
			// 
			// niSystemTrayIcon
			// 
			this.niSystemTrayIcon.ContextMenu = this.cmiMain;
			this.niSystemTrayIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("niSystemTrayIcon.Icon")));
			this.niSystemTrayIcon.Text = "Intelligent ScreenSaver (TM)";
			this.niSystemTrayIcon.Visible = true;
			// 
			// cmiMain
			// 
			this.cmiMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.cmiPause,
																					this.cmiResume,
																					this.menuItem6,
																					this.cmiRestore,
																					this.menuItem5,
																					this.cmiExit});
			this.cmiMain.Popup += new System.EventHandler(this.cmiMain_Popup);
			// 
			// cmiPause
			// 
			this.cmiPause.Index = 0;
			this.cmiPause.Text = "Pause";
			this.cmiPause.Click += new System.EventHandler(this.cmiPause_Click);
			// 
			// cmiResume
			// 
			this.cmiResume.Enabled = false;
			this.cmiResume.Index = 1;
			this.cmiResume.Text = "Resume";
			this.cmiResume.Click += new System.EventHandler(this.cmiResume_Click);
			// 
			// menuItem6
			// 
			this.menuItem6.Index = 2;
			this.menuItem6.Text = "-";
			// 
			// cmiRestore
			// 
			this.cmiRestore.Index = 3;
			this.cmiRestore.Text = "Restore";
			this.cmiRestore.Click += new System.EventHandler(this.cmiRestore_Click);
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 4;
			this.menuItem5.Text = "-";
			// 
			// cmiExit
			// 
			this.cmiExit.Index = 5;
			this.cmiExit.Text = "Exit";
			this.cmiExit.Click += new System.EventHandler(this.cmiExit_Click);
			// 
			// tbrPollingTimer
			// 
			this.tbrPollingTimer.Location = new System.Drawing.Point(224, 24);
			this.tbrPollingTimer.Minimum = 1;
			this.tbrPollingTimer.Name = "tbrPollingTimer";
			this.tbrPollingTimer.Size = new System.Drawing.Size(208, 45);
			this.tbrPollingTimer.TabIndex = 3;
			this.tbrPollingTimer.Value = 5;
			this.tbrPollingTimer.ValueChanged += new System.EventHandler(this.tbrPollingTimer_ValueChanged);
			// 
			// tbrVoteCounts
			// 
			this.tbrVoteCounts.Location = new System.Drawing.Point(224, 88);
			this.tbrVoteCounts.Minimum = 1;
			this.tbrVoteCounts.Name = "tbrVoteCounts";
			this.tbrVoteCounts.Size = new System.Drawing.Size(208, 45);
			this.tbrVoteCounts.TabIndex = 4;
			this.tbrVoteCounts.Value = 5;
			this.tbrVoteCounts.ValueChanged += new System.EventHandler(this.tbrVoteCounts_ValueChanged);
			// 
			// tmrPolling
			// 
			this.tmrPolling.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(224, 8);
			this.label1.Name = "label1";
			this.label1.TabIndex = 5;
			this.label1.Text = "Polling Timer (Sec)";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(224, 72);
			this.label2.Name = "label2";
			this.label2.TabIndex = 6;
			this.label2.Text = "False Count";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.rbProfile);
			this.panel1.Controls.Add(this.rbFront);
			this.panel1.Location = new System.Drawing.Point(224, 128);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(208, 40);
			this.panel1.TabIndex = 7;
			// 
			// rbProfile
			// 
			this.rbProfile.Location = new System.Drawing.Point(112, 8);
			this.rbProfile.Name = "rbProfile";
			this.rbProfile.Size = new System.Drawing.Size(88, 24);
			this.rbProfile.TabIndex = 1;
			this.rbProfile.Text = "Profile";
			this.rbProfile.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
			// 
			// rbFront
			// 
			this.rbFront.Checked = true;
			this.rbFront.Location = new System.Drawing.Point(16, 8);
			this.rbFront.Name = "rbFront";
			this.rbFront.TabIndex = 0;
			this.rbFront.TabStop = true;
			this.rbFront.Text = "Front";
			this.rbFront.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
			// 
			// frmFaceDetection
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(434, 168);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.tbrVoteCounts);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.tbrPollingTimer);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.ctlDxCam1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.Name = "frmFaceDetection";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Intelligent Screen Saver [Face Detection]";
			this.Resize += new System.EventHandler(this.frmFaceDetection_Resize);
			this.Closing += new System.ComponentModel.CancelEventHandler(this.frmFaceDetection_Closing);
			this.Load += new System.EventHandler(this.frmFaceDetection_Load);
			((System.ComponentModel.ISupportInitialize)(this.tbrPollingTimer)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.tbrVoteCounts)).EndInit();
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private string filename = string.Empty;
		private clsFaceDetectionWrapper faceLocator 
			= clsFaceDetectionWrapper.GetSingletonInstance();

		#region DEPCRECATED
//		private void btnLoadPicture_Click(object sender, System.EventArgs e)
//		{
//			if(openFileDialog1.ShowDialog()==DialogResult.OK)
//			{
//				pbPicture.ResumeLayout(true);
//
//				filename = openFileDialog1.FileName;
//				if(pbPicture.Image!=null) pbPicture.Image.Dispose();
//				pbPicture.Image = new Bitmap(filename);
//
//				this.Height=pbPicture.Height+50;
//				this.Width=pbPicture.Width+170;
//			}
//		}
//
//		private void btnFace_Click(object sender, System.EventArgs e)
//		{
//			if(pbPicture.Image==null)
//			{
//				MessageBox.Show("Please load an image file first");
//			}
//			else
//			{
//				pbPicture.ResumeLayout(true);
//
//				int mIndex = faceLocator.WrapDetectFaces((Bitmap)pbPicture.Image);
//				if(mIndex>0)
//				{
//					try
//					{
//						Graphics g = Graphics.FromHwnd(pbPicture.Handle);
//						
//						Pen p = new Pen(Color.Red, 5.0f);
//						unsafe
//						{
//							int lx = new Int32(); int ly = new Int32();
//							int rx = new Int32(); int ry = new Int32();
//							for( int i=0 ; i < mIndex ; i++ )
//							{
//								faceLocator.WrapGetFaceCordinates(i,&lx,&ly,&rx,&ry);
//								g.DrawRectangle(p,(float)(lx),(float)(ly),(float)(rx),(float)(ry));																						
//							}
//						}
//						System.Drawing.Drawing2D.GraphicsState gs = g.Save();						
//						g.Dispose();
//						p.Dispose();
//					}
//					catch
//					{
//						MessageBox.Show("Unexpected error has occured");
//					}
//				}
//			}
//		}
//
//		private void btnEye_Click(object sender, System.EventArgs e)
//		{
//			if(pbPicture.Image==null)
//			{
//				MessageBox.Show("Please load an image file first");
//			}
//			else
//			{
//				//int mIndex = clsFaceDetectionWrapper.GetSingletonInstance().DetectFaces((Bitmap)pbPicture.Image);
//				//if(mIndex>0)
//				{
//					Graphics g = Graphics.FromImage(pbPicture.Image);
//					Pen p = new Pen(Color.Red, 2.0f);
//					unsafe
//					{
//						int lx = new Int32(); int ly = new Int32();
//						int rx = new Int32(); int ry = new Int32();
//						for( int i=0 ; i < 1 ; i++ )
//						{
//							//clsFaceDetectionWrapper.GetSingletonInstance().GetEyeCordinates(i,&lx,&ly,&rx,&ry);
//							g.DrawRectangle(p,(float)(lx),(float)(ly),(float)(rx),(float)(ry));
//						}
//					}
//					g.Dispose();
//					p.Dispose();
//				}
//			}
//		}
//
		#endregion DEPCRECATE

		System.Drawing.Bitmap LatestFrame;
		private static object lockingObject = new object();
		private void pbPicture_NewFrameEvent(System.Drawing.Bitmap Frame)
		{
			if(!CommonVariables._theMainThread.IsRunning)
				CommonVariables._theMainThread.Start();

			lock(lockingObject)
			{
				if(LatestFrame!=null)
				{
					LatestFrame.Dispose();				
				}

				LatestFrame = Frame;
			}
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			tmrPolling.Enabled = false;
			
			if(CommonVariables.IsConsumed)
			{
				lock(lockingObject)
				{
					if(LatestFrame!=null)
					CommonVariables.FaceDetected = 
						(faceLocator.WrapDetectFaces(LatestFrame)>0);				
				}
			}

			if(ScreenSaverHandler.IsScreenSaverRunning())
				tmrPolling.Interval = 1000;
			else
				tmrPolling.Interval = (CommonVariables.PollingTimer*1000);

			tmrPolling.Enabled = true;
		}

		private void cmiPause_Click(object sender, System.EventArgs e)
		{
			CommonVariables._theMainThread.Pause();
		}

		private void cmiResume_Click(object sender, System.EventArgs e)
		{
			CommonVariables._theMainThread.Resume();
		}

		private void cmiExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void frmFaceDetection_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			tmrPolling.Enabled = false;
			ScreenSaverHandler.SetScreenSaverTimeout(99999);//10 minutes
			ctlDxCam1.Stop();
			CommonVariables._theMainThread.Stop();
			niSystemTrayIcon.Visible = false;
		}

		private void frmFaceDetection_Load(object sender, System.EventArgs e)
		{
			//LatestFrame = new Bitmap(@"D:\FACEREC\RAW\{2513FA3C-ECFC-4781-805F-A21FADBB7778}5.jpg");
			ctlDxCam1.Start();
			tmrPolling.Enabled = true;
		}

		private void tbrPollingTimer_ValueChanged(object sender, System.EventArgs e)
		{
			CommonVariables.PollingTimer = (int)tbrPollingTimer.Value;
		}

		private void tbrVoteCounts_ValueChanged(object sender, System.EventArgs e)
		{
			CommonVariables.MinVoteRequired = (int)tbrVoteCounts.Value;
		}

		private void cmiMain_Popup(object sender, System.EventArgs e)
		{
			cmiPause.Enabled = !CommonVariables._theMainThread.IsSuspended;
			cmiResume.Enabled = CommonVariables._theMainThread.IsSuspended;
			cmiRestore.Enabled = (this.WindowState == FormWindowState.Minimized);
		}

		private void frmFaceDetection_Resize(object sender, System.EventArgs e)
		{
			if(this.WindowState == FormWindowState.Minimized)
			{
				this.Hide();
			}
		}

		private void cmiRestore_Click(object sender, System.EventArgs e)
		{
			if(this.WindowState == FormWindowState.Minimized)
			{
				this.Show();
				this.WindowState = FormWindowState.Normal;
				this.Size = new Size(440,200);				
			}
		}

		private void rb_CheckedChanged(object sender, System.EventArgs e)
		{
			if(rbFront.Checked) faceLocator.WrapSetFaceDetectionMode(0);
			else faceLocator.WrapSetFaceDetectionMode(1);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Pakistan Pakistan
BCSE - Software Engineering (2000 - 2004)
Foundation University Institute of Management and Computer Sciences.
Pakistan.

MS - Computer Sciences (2004 - 2005)
Lahore Univeristy of Management Sciences
Pakistan.

Comments and Discussions