Introduction
This is a utility to control screen saver using human face detection. Human face detection is performed using OpenCV Haar-Cascade method. The software is primarily a daemon that resides in your system tray and keeps observing the input from a webcam to analyze if the user is no longer in view. Currently two angles are supported namely frontal pose and profile pose.
Background
The idea of an intelligent screen saver is not new. In the past, it was not achievable because of accuracy problems with computer vision algorithms and computation power of computers. OpenCV (open source computer vision library) has an implementation of the object detection algorithm suggested originally by Paul Viola. I have adopted from the face detection sample that comes with this library and have used the cascades files that come with it.
Using the Code
Face detection is performed by a mix-mode DLL, i.e. it uses unmanaged code to perform face detection and provides a managed wrapper. This DLL, hence can now be called from any CLR enabled language (C#, VB.NET, etc.). This DLL expects a managed System.Drawing.Image
object and performs face detection.
This managed System.Drawing.Image
object is taken from the ctlDxCam
library (a custom library built from code posted on an article on CodeProject.com, it can capture frames from a webcam as System.Drawing.Image
objects) which captures video frames from a webcam.
Screensaver is controlled by a class that uses borrowed code from another article on CodeProject.com. Please consult references section for details.
CommonVariables.FaceDetected = (faceLocator.WrapDetectFaces(LatestFrame)>0);
private void Run()
{
bool IsScreenSaverRunning;
while(KeepRunning)
{
IsScreenSaverRunning = ScreenSaverHandler.IsScreenSaverRunning();
if(!CommonVariables.IsConsumed)
{
if(CommonVariables.FaceDetected)
{
CommonVariables.VoteCount = 0;
if(IsScreenSaverRunning)
ScreenSaverHandler.StopScreenSaver();
}
else if(!IsScreenSaverRunning)
{
CommonVariables.VoteCount++;
if(CommonVariables.VoteCount >= CommonVariables.MinVoteRequired)
{
CommonVariables.VoteCount = 0;
ScreenSaverHandler.LaunchScreenSaver();
}
}
}
if(IsScreenSaverRunning)
Thread.Sleep(1000); else Thread.Sleep(CommonVariables.PollingTimer);
}
}
The source code performs polling on the camera input and evaluates the captured frame at desired interval of time. For each frame that does not have a face in it, a counter (VoteCount
) is incremented. After a specified number of such votes, screen saver is launched.
If the polling timer is set to 5 seconds and required votes is set to 2, then if a person leaves his seat, screen saver will start running in approximately (5*2) 10 seconds.
Points of Interest
This source code demonstrates an interesting concept to use OpenCV from managed code (C#, VB.NET, Managed VC++.NET). The algorithm of face detection is very robust but still has its limitations. False detection of the algorithm can be adjusted by setting the light condition of the environment and camera position, etc. User can select whether he wants frontal face detection or profile face detection and hence place camera in respective angles.
References
History
- First version, proof of concept