📄 facerecognition.cpp
字号:
#include "EHMMObjDatabase.h"
#include "ImgObjDatabase.h"
#include "EHMMObjRecognition.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <highgui.h>
#include <io.h>
#include <direct.h>
using namespace std;
string PATH_CV_IMG_DB = "c:\\hmmfaces\\DATABASE\\CvImgDB\\";
string PATH_CV_EHMM_DB = "c:\\hmmfaces\\DATABASE\\CvEhmmDB\\";
string PATH_FACES_IMG_DB = "c:\\hmmfaces\\DATABASE\\FacesImgDB\\";
string PATH_FACES_EHMM_DB = "c:\\hmmfaces\\DATABASE\\FacesEhmmDB\\";
//Training/Recognition parameters
const int IMG_WIDTH = 100;
const int IMG_HEIGHT = 0;
const int OBS_WIDTH = 12;
const int OBS_HEIGHT = 12;
const int NO_DCT_COEFF_X = 4;
const int NO_DCT_COEFF_Y = 4;
const int STEP_X = 4;
const int STEP_Y = 4;
const bool SUPPRESS_INTESITY = true;
#define MARGIN 15 // add margin to the detect face window
string PATH_IMAGES;
static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;
IplImage *FindFace( IplImage* image );
IplImage *ClipFace(IplImage* src, int x, int y, int w, int h);
const char* cascade_name =
"--cascade=haarcascade_frontalface_alt.xml";
int totalPeople = 12;
int totalFaces = 28;
void CreateFacesImgDb( const string &pathCvImgDb,
const string &imgCvDbName )
{
int i;
char filename[255];
PATH_IMAGES = pathCvImgDb + "Images\\";
struct _finddata_t filedata;
long hFile;
int len;
totalPeople = 0;
totalFaces = 0;
ImgObjDatabase imgDb;
//Create the database
imgDb.Create( imgCvDbName );
_chdir(PATH_IMAGES.c_str());
//scan image database directory to add training images
if( (hFile = _findfirst( "*.*", &filedata )) == -1L )
printf( "No people found in current directory!\n" );
else
{
//find the first directory
if((filedata.attrib & _A_SUBDIR) &&
stricmp(filedata.name, ".")!=0 &&
stricmp(filedata.name, "..")!=0)
{
printf( " %-12s %9ld\n", filedata.name, filedata.size );
totalPeople++;
imgDb.AddObj( filedata.name);
}
/* Find the rest of the directories */
while( _findnext( hFile, &filedata ) == 0 )
{
if((filedata.attrib & _A_SUBDIR) &&
stricmp(filedata.name, ".")!=0 &&
stricmp(filedata.name, "..")!=0)
{
printf( " %-12s %9ld\n", filedata.name, filedata.size );
totalPeople++;
imgDb.AddObj( filedata.name );
}
}
_findclose( hFile );
}
//Add all the images of the users
for(i=0;i<totalPeople; i++){
string name = imgDb.GetObj(i).GetName();
_chdir((PATH_IMAGES+name).c_str());
if( (hFile = _findfirst( "*.*", &filedata )) == -1L )
printf( "No images found in current directory!\n" );
else
{
if(stricmp(filedata.name, ".")!=0 &&
stricmp(filedata.name, "..")!=0)
{
if(len>4 && filedata.name[len-1]=='m' &&
filedata.name[len-2]=='g' &&
filedata.name[len-3]=='p')
{
printf( " %-12s %9ld\n", filedata.name, filedata.size );
sprintf(filename, "%s\\%s", name.c_str(), filedata.name);
imgDb.GetObj(i).AddImage( PATH_IMAGES + filename );
}
}
/* Find the rest of the .c files */
while( _findnext( hFile, &filedata ) == 0 )
{
len = strlen(filedata.name);
if(stricmp(filedata.name, ".")!=0 &&
stricmp(filedata.name, "..")!=0)
{
if(len>4 && filedata.name[len-1]=='m' &&
filedata.name[len-2]=='g' &&
filedata.name[len-3]=='p')
{
printf( " %-12s %9ld\n", filedata.name, filedata.size );
sprintf(filename, "%s\\%s", name.c_str(), filedata.name);
imgDb.GetObj(i).AddImage( PATH_IMAGES + filename );
}
}
}
_findclose( hFile );
}
}
cout << endl << "CreateFacesImgDb: Created '" << imgCvDbName << "'." << endl << endl;
cout << imgDb;
imgDb.Save( pathCvImgDb );
}
//*****************************************************************************
void LoadImgDb( const string &pathCvImgDb, const string &imgDbName )
{
ImgObjDatabase imgDb;
imgDb.Load( imgDbName, pathCvImgDb );
cout << endl <<"LoadImgDb: Loaded '" << imgDbName << "'." << endl << endl;
cout << imgDb;
for ( size_t i = 0; i < imgDb.GetNoObjs( ); i++ )
{
cout << "loading "<< imgDb.GetObj(i).GetName();
for ( size_t j = 0; j < imgDb.GetObj( i ).GetNoImages( ); j++ )
{
cout << j << "...";
IplImage *img = imgDb.GetObj( i ).GetGrayScaleImage( j, 0, 0, false );
cvReleaseImage( &img );
}
cout << endl;
}
}
//*****************************************************************************
void CreateFacesEHMMDb( const string &pathEhmmDb, const string &ehmmCvDbName )
{
EHMMObjDatabase ehmmDb;
int noStates[] = { 5, 3, 6, 6, 6, 3 };
int numMixtures[] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
int vecSize = 15; //because we suppress the first coefficient DCT_SIZE_X * DCT_SIZE_Y - 1
//Create the database
ehmmDb.Create( ehmmCvDbName, noStates, numMixtures, vecSize );
struct _finddata_t filedata;
long hFile;
int len;
_chdir(PATH_IMAGES.c_str());
if( (hFile = _findfirst( "*.*", &filedata )) == -1L )
printf( "No people found in current directory!\n" );
else
{
if((filedata.attrib & _A_SUBDIR) &&
stricmp(filedata.name, ".")!=0 &&
stricmp(filedata.name, "..")!=0)
{
ehmmDb.AddObj( filedata.name );
}
/* Find the rest of the .c files */
while( _findnext( hFile, &filedata ) == 0 )
{
if((filedata.attrib & _A_SUBDIR) &&
stricmp(filedata.name, ".")!=0 &&
stricmp(filedata.name, "..")!=0)
{
ehmmDb.AddObj( filedata.name );
}
}
_findclose( hFile );
}
cout << endl << "CreateCVEHMMDb: Created '" << ehmmCvDbName << "'." << endl << endl;
cout << ehmmDb;
ehmmDb.Save( pathEhmmDb );
}
//*****************************************************************************
void TrainEHMMDb( const string &pathImgDb, const string &imgDbName,
const string &pathEhmmDb, const string &ehmmDbName )
{
EHMMObjRecognition recog;
ImgObjDatabase imgDb;
EHMMObjDatabase ehmmDb;
imgDb.Load( imgDbName, pathImgDb );
ehmmDb.Load( ehmmDbName, pathEhmmDb );
cout << endl << "TrainEHMMDb: Databases are loaded." << endl << endl;
cout << imgDb;
cout << ehmmDb;
recog.Create( IMG_WIDTH, IMG_HEIGHT, OBS_WIDTH, OBS_HEIGHT, NO_DCT_COEFF_X, NO_DCT_COEFF_Y,
STEP_X, STEP_Y, SUPPRESS_INTESITY );
recog.Train( imgDb, ehmmDb );
ehmmDb.Save( pathEhmmDb );
}
IplImage* CamToGray( IplImage *imgCam, int imgWidth, int imgHeight)
{
IplImage *imgDb;
IplImage *imgHdd;
imgHdd = cvCreateImage(cvSize(imgCam->width, imgCam->height), IPL_DEPTH_8U, 1);
cvCvtColor(imgCam, imgHdd, CV_BGR2GRAY);
//Create the output image at the requested size
if ( imgWidth && imgHeight )
{
imgDb = cvCreateImage( cvSize( imgWidth, imgHeight ), IPL_DEPTH_8U, 1 );
}
else if ( imgHeight )
{
imgDb = cvCreateImage( cvSize( imgHdd->width * imgHeight / imgHdd->height, imgHeight ),
IPL_DEPTH_8U, 1 );
}
else if ( imgWidth )
{
imgDb = cvCreateImage( cvSize( imgWidth, imgHdd->height * imgWidth / imgHdd->width ),
IPL_DEPTH_8U, 1 );
}
else
{
imgDb = cvCreateImage( cvSize( imgHdd->width, imgHdd->height ), IPL_DEPTH_8U, 1 );
}
assert( imgDb != 0 );
//Resize the output to the requested size
cvResize( imgHdd, imgDb );
//Set a roi to the full image
cvSetImageROI( imgDb, cvRect( 0, 0, imgDb->width, imgDb->height ) );
//Delete all the temp images
cvReleaseImage( &imgHdd );
return imgDb;
}
//*****************************************************************************
void RecognizeEHMMDb( const string &image,
const string &pathEhmmDb,
const string &ehmmDbName)
{
EHMMObjRecognition recog;
EHMMObjDatabase ehmmDb;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -