📄 facerecognition.cpp
字号:
IplImage* img;
char filename[255];
const string &PATH_IMAGES = PATH_FACES_IMG_DB+"Images\\";
ImgObj person;
int index = -1;
vector< float > likelihood;
vector< float >::iterator it;
ehmmDb.Load( ehmmDbName, pathEhmmDb );
img = cvLoadImage(image.c_str());
cvShowImage("Video", img);
cout << endl << "RecognizeEHMMDb: Database is loaded." << endl << endl;
cout << ehmmDb;
person.Create( "Test" );
person.AddImage( image );
recog.Create( IMG_WIDTH, IMG_HEIGHT, OBS_WIDTH, OBS_HEIGHT, NO_DCT_COEFF_X, NO_DCT_COEFF_Y,
STEP_X, STEP_Y, SUPPRESS_INTESITY );
img = person.GetGrayScaleImage( 0, 100, 0 );
index = (int)recog.ComputeLikelihood( *img, ehmmDb, likelihood );
cvReleaseImage( &img );
cout << endl << "All likelihoods" << endl << endl;
for ( it = likelihood.begin( ); it < likelihood.end( ); it++ )
{
cout << *it << " ";
}
cout << endl << endl;
if ( index >= 0 )
{
cout << "Person is " << ehmmDb.GetObj( index ).GetName( ) << " ( "
<< likelihood[ index ] << " )." << endl;
}
sprintf(filename, "%s\\%s.jpg",
ehmmDb.GetObj(index).GetName().c_str(),ehmmDb.GetObj(index).GetName().c_str() );
printf("recog result filename: %s\n", filename);
img = cvLoadImage((PATH_IMAGES+filename).c_str());
cvShowImage("ID", img);
cvWaitKey(0);
cvReleaseImage(&img);
}
//*****************************************************************************
void RecognizeEHMMDbFromCam( IplImage *imgCam,
const string &pathEhmmDb,
const string &ehmmDbName)
{
EHMMObjRecognition recog;
EHMMObjDatabase ehmmDb;
IplImage* img;
char filename[255];
const string &PATH_IMAGES = PATH_FACES_IMG_DB+"Images\\";
ImgObj person;
int index = -1;
vector< float > likelihood;
vector< float >::iterator it;
ehmmDb.Load( ehmmDbName, pathEhmmDb );
cout << endl << "RecognizeEHMMDb: Database is loaded." << endl << endl;
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 );
img = CamToGray(imgCam, 100, 0);
index = (int)recog.ComputeLikelihood( *img, ehmmDb, likelihood );
cvReleaseImage( &img );
cout << endl << "All likelihoods" << endl << endl;
for ( it = likelihood.begin( ); it < likelihood.end( ); it++ )
{
cout << *it << " ";
}
cout << endl << endl;
if ( index >= 0 )
{
cout << "Person is " << ehmmDb.GetObj( index ).GetName( ) << " ( "
<< likelihood[ index ] << " )." << endl;
}
sprintf(filename, "%s\\%s.jpg",
ehmmDb.GetObj(index).GetName().c_str(),ehmmDb.GetObj(index).GetName().c_str() );
printf("recog result filename: %s\n", filename);
img = cvLoadImage((PATH_IMAGES+filename).c_str());
cvShowImage("ID", img);
cvReleaseImage(&img);
// cvReleaseImage(&imgCam);
}
IplImage *ClipFace( IplImage* src, int x, int y, int w, int h){
IplImage *target;
int i,j,x2,y2;
if(!w || !h) return 0;
//adjust window size to include more of the face
x2=x+w; y2=y+h;
if(x-MARGIN>=0)
x=x-MARGIN;
else
x=0;
if(y-MARGIN>=0)
y=y-MARGIN;
else
y=0;
if(x2+MARGIN<src->width)
x2=x2+MARGIN;
else
x2=src->width-1;
if(y2+MARGIN<src->height)
y2=y2+MARGIN;
else
y2=src->height-1;
w = x2-x;
h = y2-y;
printf("face w:%d h:%d\n", w, h);
// create target image
target = cvCreateImage( cvSize(w,h), 8, 3 );
for (i=y; i<y+h; i++){
for (j=x; j<x+w; j++){
target->imageData[(i-y)*target->widthStep+(j-x)*3]=
src->imageData[i*src->widthStep+j*3];
target->imageData[(i-y)*target->widthStep+(j-x)*3+1]=
src->imageData[i*src->widthStep+j*3+1];
target->imageData[(i-y)*target->widthStep+(j-x)*3+2]=
src->imageData[i*src->widthStep+j*3+2];
}
}
return target;
}
IplImage* FindFace( IplImage* img )
{
int scale = 1;
CvPoint pt1, pt2;
int i;
IplImage *imgFace=0;
cvClearMemStorage( storage );
if( cascade )
{
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
imgFace = ClipFace(img, pt1.x, pt1.y, r->width, r->height);
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
//just handle one face per frame,ignore the others
cvShowImage("Search",imgFace);
return imgFace;
}
}
return imgFace;
}
//*****************************************************************************
int main(int argc, char* argv[])
{
CvCapture* capture = 0;
IplImage *frame, *frame_copy = 0;
int c;
cout << endl << "HMM Face Reconition" << endl;
cout << "============================================" <<endl;
cout << "Press esc to exit program" << endl;
cout << "press any other key to recognize face" << endl;
cvNamedWindow("Video",1);
cvNamedWindow("ID",1);
cvNamedWindow("Search",1);
cvMoveWindow("Video",0,0);
cvMoveWindow("ID", 400,0);
cvMoveWindow("Search",0, 300);
if ( argc > 1 )
{
int choice;
choice = atoi(argv[1]);
if(choice==1){
//recognize from a picture
RecognizeEHMMDb( argv[2], PATH_FACES_EHMM_DB, "faces.db" );
}
else if(choice==2 || choice==3){
int optlen = strlen("--cascade=");
cascade_name = cascade_name+optlen;
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
storage = cvCreateMemStorage(0);
//recognize from live video stream
if(choice == 3)
capture = cvCaptureFromCAM(0);
else
//test with video clip
capture = cvCaptureFromAVI(argv[2]);
if(capture){
for(;;)
{
if( !cvGrabFrame( capture ))
break;
frame = cvRetrieveFrame( capture );
if( !frame )
break;
if( !frame_copy )
frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
IPL_DEPTH_8U, frame->nChannels );
if( frame->origin == IPL_ORIGIN_TL )
cvCopy( frame, frame_copy, 0 );
else
cvFlip( frame, frame_copy, 0 );
cvShowImage("Video", frame_copy);
c = cvWaitKey(10);
if (c == '\x1b'){
printf("Exiting ...\n");
goto exit_main;
}
else if(c>=0){
IplImage *imgFace;
imgFace = FindFace(frame_copy);
if(imgFace){
RecognizeEHMMDbFromCam(imgFace, PATH_FACES_EHMM_DB, "faces.db");
cvReleaseImage(&imgFace);
}
}
}
}
}
else{
cout<<"Can neither find live video camera nor input static image for Face Recognition"<<endl;
cout<<"Syntax: FaceRecognition [choice [input_image_file_name]]"<<endl;
cout<<" choice=1: recognize face from static input image, input_image_filename REQURIED"<<endl;
cout<<" choice=2: recognize face from pre-recorded video sequence"<<endl;
cout<<" choice=3: recognize face from LIVE cam"<<endl;
}
goto exit_main;
}
//create face database, training
CreateFacesImgDb( PATH_FACES_IMG_DB, "FACES.db" );
LoadImgDb( PATH_FACES_IMG_DB, "FACES.db" );
CreateFacesEHMMDb( PATH_FACES_EHMM_DB, "FACES.db" );
TrainEHMMDb( PATH_FACES_IMG_DB, "faces.db", PATH_FACES_EHMM_DB, "faces.db" );
exit_main:
cvDestroyWindow("Video");
cvDestroyWindow("ID");
// if(frame)
// cvReleaseImage(&frame);
if(frame_copy)
cvReleaseImage(&frame_copy);
if(capture)
cvReleaseCapture(&capture);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -