CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #1
    Join Date
    Jun 2010
    Posts
    10

    Face detection program bug

    Hi guys, I'm currently trying to produce a facial detection application, but I seem to have some problems. My program, is able to recognise multiple users, and match their captured image to one saved in the database. However, if a person who has not registered is photographed, it is supposed open a pop-up of a picture of my choosing (in the code below I've chosen a picture of Mario) and tell the user that he/she is not in the database. The code for the condition of both recognizing and showing an unrecognised user is below.

    The problem I have, is that when an unrecognised user is captured, it will still show user 1 or 2, instead of a "Mario" image. I'm unsure if it is the recognition algorithm (code also below) or my "if" statement that is at fault. I'm now trying with only 2 users to minimize the chance of errors. Can anyone help?

    Code:
    void CzebraDlg::OnBnClickedCapturetrain()
    {
    else if( LearnOrRecognize == 2)  // CASE 2 (CAPTURE FOR RECOGNIZE)
    	{
    	sprintf(PicFname, "%s", "test.jpg");
    	sprintf(TestFname, "%s", "test.txt");
    
    	ofstream outfile;   //An object:myfile of type "ofstream" is created to allow the file to be opened for writing
    	outfile.open(TestFname, ios::app);
    
    	char *temp  = new char[50];
    	
    	sprintf(temp, "%s", "1 test.jpg");
    	outfile << temp <<endl;
    
    	cvSaveImage(PicFname, gregion);
    
    	recognize();
    
    IplImage* Identified_Person_Img;
    
    		if (RESULT_NEAREST == 1) //Nearest refers to ID of person being recognized
    		{
    		cvNamedWindow( "ID01", 0 );
    		cvResizeWindow( "ID01", 160, 160 );
    		cvMoveWindow( "ID01", 673, 506 );
    		Identified_Person_Img = cvLoadImage( "D:\\FYP\\01\\1.jpg", 1 );
    		cvShowImage("ID01", Identified_Person_Img);
    		EEE(node_number=0); //meant for hardware interfacing, not used
    		}
    		if (RESULT_NEAREST !=1)
    		{
    		 if (RESULT_NEAREST == 2)
    		{
    		cvNamedWindow( "ID02", 0 );
    		cvResizeWindow( "ID02", 160, 160 );
    		cvMoveWindow( "ID02", 673, 506 );
    		Identified_Person_Img = cvLoadImage( "D:\\FYP\\02\\1.jpg", 1 );
    		cvShowImage("ID02", Identified_Person_Img);
    		EEE(node_number=1);
    		}
    }
    		if (!(RESULT_NEAREST == 1) && !(RESULT_NEAREST == 2))
    		{
    		cvNamedWindow( "Unknown Face Detected", 0 );
    		cvResizeWindow( "Unknown Face Detected", 160, 160 );
    		cvMoveWindow( "Unknown Face Detected", 673, 506 );
    		IplImage* UnknownFace;
    		UnknownFace = cvLoadImage( "D:\\FYP\\MARIO1.jpg", 1 );
    		IplImage *FUnknownFace = 0;
    	              FUnknownFace = cvCreateImage(cvSize(160, 160),            
    								UnknownFace->depth, UnknownFace->nChannels);
    				  cvResize( UnknownFace, FUnknownFace, CV_INTER_LINEAR );
    		cvShowImage("Unknown Face Detected", FUnknownFace);
    		EEE(node_number=20);
    		}
    outfile.close();
    }
    }

    Recognition function is below for reference and error checking.

    Code:
    void recognize()
    {
    	int i, nTestFaces  = 0;         // the number of test images
    	CvMat * trainPersonNumMat = 0;  // the person numbers during training
    	float * projectedTestFace = 0;
    
    	// load test images and ground truth for person number
    	nTestFaces = loadFaceImgArray("test.txt");
    
    	// load the saved training data
    	if( !loadTrainingData( &trainPersonNumMat ) ) return;
    
    	// project the test images onto the PCA subspace
    	projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );
    
    	int iNearest, nearest, truth;
    
    	for(i=0; i<nTestFaces; i++)
    	{
    		// project the test image onto the PCA subspace
    		cvEigenDecomposite
    			(
    				faceImgArr[i],
    				nEigens,
    				eigenVectArr,
    				0, 0,
    				pAvgTrainImg,
    				projectedTestFace);
    
    		iNearest = findNearestNeighbor(projectedTestFace);
    		truth    = personNumTruthMat->data.i[i];
    		nearest  = trainPersonNumMat->data.i[iNearest];
    		
    		}
            RESULT_NEAREST = nearest;
    		RESULT_TRUTH   = truth;
    }
    Function that gives values to the one above's code is below

    Code:
    int findNearestNeighbor(float * projectedTestFace)
    {
    	double leastDistSq = DBL_MAX;
    	int i, iTrain, iNearest = 0;
    
    	for(iTrain=0; iTrain<nTrainFaces; iTrain++)
    	{
    		double distSq=0;
    
    		for(i=0; i<nEigens; i++)
    		{
    			float d_i =
    				projectedTestFace[i] -
    				projectedTrainFaceMat->data.fl[iTrain*nEigens + i];
    			distSq += d_i*d_i / eigenValMat->data.fl[i];
    			
    		}
    
    		if(distSq < leastDistSq)
    		{
    			leastDistSq = distSq;
    			iNearest = iTrain;
    		}
    	}
    
    	return iNearest;
    }
    I'm using MFC for the GUI creation, but I don't think that should be a problem as of now. Thanks a lot for any help in advance. If you need to see the entire code that these snippets are taken from, I have attached it (zebraDlg.cpp).
    Attached Files Attached Files
    Last edited by meepokman; June 17th, 2010 at 07:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured