Click here to Skip to main content
15,896,201 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 184.1K   10.9K   111  
A utility to control screen saver on your computer using computer vision (human face detection), rather than idle timer.
// This is the main DLL file.
#pragma managed
#include "stdafx.h"
#include "FaceLocator.h"
#include <stdio.h>

#define FrontalFaceCascade "haarcascade_frontalface_alt.xml"
#define ProfileFaceCascade "haarcascade_profileface.xml"

FaceLocator::FaceLocator(System::String* appPath)
{
	faceMode = 0;
	strApplicationPath = appPath;
	FrontalFaceDetector = new ObjectLocator();
	ProfileFaceDetector = new ObjectLocator();
	
	CurrentFaceDetector = FrontalFaceDetector;
}

FaceLocator::~FaceLocator(void)
{	
	delete FrontalFaceDetector;	
	delete ProfileFaceDetector;	
}

int FaceLocator::SetFaceMode(int Mode)
{
	faceMode = Mode;
	if(Mode==0)
		CurrentFaceDetector = FrontalFaceDetector;
	else
		CurrentFaceDetector = ProfileFaceDetector;

	return faceMode;
}

void FaceLocator::InitCascades()
{
	char *c, *chrAppPath;

	chrAppPath = (char*)(void*)System::Runtime
		::InteropServices::Marshal::StringToHGlobalAnsi(strApplicationPath);
	c = strcat(chrAppPath,FrontalFaceCascade);	
	FrontalFaceDetector->InitObjectDetect(c);
	System::Runtime::InteropServices::Marshal::FreeHGlobal(chrAppPath);

	chrAppPath = (char*)(void*)System::Runtime
		::InteropServices::Marshal::StringToHGlobalAnsi(strApplicationPath);
	c = strcat(chrAppPath,ProfileFaceCascade);	
	ProfileFaceDetector->InitObjectDetect(c);
	System::Runtime::InteropServices::Marshal::FreeHGlobal(chrAppPath);
}


int FaceLocator::DetectFaces(IplImage* inputImage)
{
	if(InitFlag==0) 
	{
		InitFlag++;		
		CurrentFaceDetector->InitImageStorage(inputImage->width,inputImage->height, scale);
		InitCascades();	
		lastHeight = inputImage->height;
		lastWidth = inputImage->width;
	}	
	else if((lastHeight!=inputImage->height) || (lastWidth!=inputImage->width))
	{	
		CurrentFaceDetector->InitImageStorage(inputImage->width,inputImage->height, scale);
		InitCascades();
		lastHeight = inputImage->height;
		lastWidth = inputImage->width;
	}
	
	CurrentFaceDetector->ManagedImagetoUnManagedImage((inputImage));	
	CurrentFaceDetector->ImagePreProcessForFaceDetection();
	//CurrentFaceDetector->UnSetFaceROI();
	int FNum = CurrentFaceDetector->DetectObjects();
	
	return FNum;
	//return 0;
}

int FaceLocator::GetFaceCordinates(int index, int &lx, int &ly, int &rx, int &ry)
{
	int lx1, ly1, rx1, ry1;
	CurrentFaceDetector->GetObjectCoordinates(index, lx1, ly1, rx1, ry1);	
	lx = lx1;
	ly = ly1;
	rx = rx1;
	ry = ry1;
	return 0;
}

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