bool EigenfaceRecognizer::train()
	{
		isTrained = false;

		if(!loadTrainingImages())
			return false;

		//do principal component analysis
		doPCA();

		// project the training images onto the PCA subspace
		projectedTrainImage = cvCreateMat( numTrainedImages, numEigenvalues, CV_32FC1 );
	
		int offset = projectedTrainImage->step / sizeof(float);
	
		for(int i = 0; i < numTrainedImages; i++)
		{
			//int offset = i * nEigens;
			cvEigenDecomposite(
				images[i],
				numEigenvalues,
				eigenVectors,
				0, 0,
				averageImage,
				//projectedTrainFaceMat->data.fl + i*nEigens);
				projectedTrainImage->data.fl + i*offset);
		}

		isTrained = true;

		return isTrained;
	}
示例#2
0
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");
 printf("%d test faces loaded\n", nTestFaces);
 
 // load the saved training data
 if( !loadTrainingData( &trainPersonNumMat ) ) return;
 
 // project the test images onto the PCA subspace
 projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );
 for(i=0; i<nTestFaces; i++)
 {
 int iNearest, nearest, truth;
 
 //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];
 
 printf("nearest = %d, Truth = %d\n", nearest, truth);
 }
}
示例#3
0
//////////////////////////////////
// learn()
//
void  faceRecognition::learn()
{   
	AfxMessageBox("训练人脸识别模型");
	int i, offset;
	// load training data
	nTrainFaces = loadFaceImgArray(learnFileListPath);
	if( nTrainFaces < 2){
		CString S;
		S.Format("Need 2 or more training faces\nInput file contains only %d\n", nTrainFaces);AfxMessageBox(S);    return;
	    return ;
	}
	// do PCA on the training faces
	doPCA();
	// project the training images onto the PCA subspace
	projectedTrainFaceMat = cvCreateMat( nTrainFaces, nEigens, CV_32FC1 );
	offset = projectedTrainFaceMat->step / sizeof(float);
	for(i=0; i<nTrainFaces; i++){
		//int offset = i * nEigens;
		cvEigenDecomposite(
			faceImgArr[i],
			nEigens,
			eigenVectArr,
			0, 0,
			pAvgTrainImg,
			//projectedTrainFaceMat->data.fl + i*nEigens);
			projectedTrainFaceMat->data.fl + i*offset);
	}
	// store the recognition data as an xml file
	storeTrainingData();
}
示例#4
0
void learn()
{
 int i;
 
 // load training data
 nTrainFaces = loadFaceImgArray("train.txt");
 if( nTrainFaces < 2 )
 {
 fprintf(stderr,
 "Need 2 or more training faces\n"
 "Input file contains only %d\n", nTrainFaces);
 return;
 }
 
 // do PCA on the training faces
 doPCA();
 printf("Starting Matrix creation.\n");
 // project the training images onto the PCA subspace
 projectedTrainFaceMat = cvCreateMat(nTrainFaces, nEigens, CV_32FC1);
 printf("Entering for loop.\n");
 for(i=0; i<nTrainFaces; i++)
 {
 cvEigenDecomposite(
 faceImgArr[i],
 nEigens,
 eigenVectArr,
 0, 0,
 pAvgTrainImg,
 projectedTrainFaceMat->data.fl + i*nEigens);
 }
 // store the recognition data as an xml file
 storeTrainingData();
}
示例#5
0
/**
 * 顔を認識して、該当する人のIDを返す
 */
int EigenFace::recognize(IplImage* testFace)
{
  // 事前加工
  IplImage* resizedFaceImage = preProcess(testFace);
  
  float * projectedTestFace = 0;
  
  // project the test images onto the PCA subspace
  projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );
  int iNearest, nearest;

  // project the test image onto the PCA subspace
  cvEigenDecomposite(
    resizedFaceImage,
    nEigens,
    eigenVectArr,
    0, 0,
    pAvgTrainImg,
    projectedTestFace);

  iNearest = findNearestNeighbor(projectedTestFace);
  nearest  = trainPersonNumMat->data.i[iNearest];
  
  cvReleaseImage(&resizedFaceImage);

  return nearest;
}
示例#6
0
/**
 * 顔を学習する
 */
void EigenFace::learn(char* trainFileName)
{
	int i, offset;
  
	// load training data
	nTrainFaces = loadFaceImgArray(trainFileName);
	if( nTrainFaces < 2 )
	{
		LOGE("Need 2 or more training faces\n"
         "Input file contains only %d\n", nTrainFaces);
		return;
	}
  
	// do PCA on the training faces
	doPCA();
  
	// project the training images onto the PCA subspace
	projectedTrainFaceMat = cvCreateMat( nTrainFaces, nEigens, CV_32FC1 );
	offset = projectedTrainFaceMat->step / sizeof(float);
	for(i=0; i<nTrainFaces; i++)
	{
		//int offset = i * nEigens;
		cvEigenDecomposite(
                       faceImgArr[i],
                       nEigens,
                       eigenVectArr,
                       0, 0,
                       pAvgTrainImg,
                       //projectedTrainFaceMat->data.fl + i*nEigens);
                       projectedTrainFaceMat->data.fl + i*offset);
	}
  
	// store the recognition data as an xml file
	storeTrainingData();
}
示例#7
0
//////////////////////////////////
// recognize()
//返回本次训练通过的图片数
int faceRecognition::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(testFileListPath);
	// load the saved training data
	if( !loadTrainingData( &trainPersonNumMat ) ) return 0;
	// project the test images onto the PCA subspace
	projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );
	//处理输出结果,记录一次训练中正确匹配的图片数
	int count=0;
	for(i=0; i<nTestFaces; i++)
	{  int iNearest, nearest, truth;
		// 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];
	//	AfxMessageBox("nearest = %d, Truth = %d", nearest, truth);
		if(nearest==truth)
			  count++;
	}//for
	return count;
}
	std::string EigenfaceRecognizer::recognize(cv::Mat &face)
	{
		//int i, nTestFaces  = 0;         // the number of test images
		//CvMat * trainPersonNumMat = 0;  // the person numbers during training
		//float * projectedTestFace = 0;

		IplImage faceToRecognize = (IplImage) face;
		//IplImage *faceToRecognize = cvLoadImage("s1/1.pgm", CV_LOAD_IMAGE_GRAYSCALE);

		float *projectedTestImage = (float *)cvAlloc(sizeof(float) * numEigenvalues);
	
		//project the test image onto the PCA subspace
		cvEigenDecomposite(
			&faceToRecognize,
			numEigenvalues,
			eigenVectors,
			0, 0,
			averageImage,
			projectedTestImage);

		int nearest = findNearestNeighbor(projectedTestImage);

		//take threshold into account, mark as not recognized if not found!

		return recognitionDescriptor[nearest].second;
	}
示例#9
0
//识别阶段代码
void recognize()
{
	int i, nTestFaces  = 0;         // 测试人脸数
	CvMat * trainPersonNumMat = 0;  // 训练阶段的人脸数
	float * projectedTestFace = 0;

	// 加载测试图像,并返回测试人脸数
	nTestFaces = loadFaceImgArray("test.txt");
	printf("%d test faces loaded\n", nTestFaces);

	// 加载保存在.xml文件中的训练结果
	if( !loadTrainingData( &trainPersonNumMat ) ) return;

	// 
	projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );
	for(i=0; i<nTestFaces; i++)
	{
		int iNearest, nearest, truth;

		//将测试图像投影到子空间中
		cvEigenDecomposite(
			faceImgArr[i],
			nEigens,
			eigenVectArr,
			0, 0,
			pAvgTrainImg,
			projectedTestFace);

		iNearest = findNearestNeighbor(projectedTestFace);
		truth    = personNumTruthMat->data.i[i];
		nearest  = trainPersonNumMat->data.i[iNearest];

		printf("nearest = %d, Truth = %d\n", nearest, truth);
	}
}
// Train from the data in the given text file, and store the trained data into the file 'facedata.xml'.
void learn(char szFileTrain[252])
{
    int i, offset;
    
    
    // load training data
    printf("Loading the training images in '%s'\n", szFileTrain);
    nTrainFaces = loadFaceImgArray(szFileTrain);
    printf("Got %d training images.\n", nTrainFaces);
    if( nTrainFaces < 2 )
    {
        fprintf(stderr,
                "Need 2 or more training faces\n"
                "Input file contains only %d\n", nTrainFaces);
        return;
    }
    
    // do PCA on the training faces
    doPCA();
    
    // project the training images onto the PCA subspace
    projectedTrainFaceMat = cvCreateMat( nTrainFaces, nEigens, CV_32FC1 );
    offset = projectedTrainFaceMat->step / sizeof(float);
    
    for(i=0; i<nTrainFaces; i++)
    {
        //int offset = i * nEigens;
        cvEigenDecomposite(
                           faceImgArr[i],
                           nEigens,
                           eigenVectArr,
                           0, 0,
                           pAvgTrainImg,
                           //projectedTrainFaceMat->data.fl + i*nEigens);
                           projectedTrainFaceMat->data.fl + i*offset);
    }
    
    // store the recognition data as an xml file
    storeTrainingData();
    
    // Save all the eigenvectors as images, so that they can be checked.
    if (SAVE_EIGENFACE_IMAGES) {
        storeEigenfaceImages();
    }
    
}
示例#11
0
文件: facerecog.c 项目: atiti/OFR
void learn_eigenfaces() {
	int i, offset;
	nTrainFaces = loadFaceImgArray("train_eigen.txt");
	if (nTrainFaces < 2) {
		fprintf(stderr, "Need more than 2 faces to train\n");
		return;
	}

	doPCA();
	
	projectedTrainFaceMat = cvCreateMat(nTrainFaces, nEigens, CV_32FC1);
	offset = projectedTrainFaceMat->step / sizeof(float);
	for(i=0;i<nTrainFaces;i++) {
		cvEigenDecomposite(faceImgArr[i], nEigens, eigenVectArr, 0, 0,
				   pAvgTrainImg, projectedTrainFaceMat->data.fl + i*nEigens);

	}
	storeTrainingData_eigenfaces();
}
示例#12
0
文件: facerecog.c 项目: atiti/OFR
void recognize_eigenfaces() {
	int i, nTestFaces = 0;
	CvMat *trainPersonNumMat = 0;
	float *projectedTestFace = 0;

	nTestFaces = loadFaceImgArray("test.txt");
	printf("%d test faces loaded\n", nTestFaces);
	if (!loadTrainingData_eigenfaces(&trainPersonNumMat)) return;

	projectedTestFace = (float *)cvAlloc(nEigens*sizeof(float));
	for(i=0;i<nTestFaces;i++) {
		int iNearest, nearest, truth;
		cvEigenDecomposite(faceImgArr[i], nEigens, eigenVectArr,
				   0, 0, pAvgTrainImg, projectedTestFace);

		iNearest = findNearestNeighbor_eigenfaces(projectedTestFace);
		truth = personNumTruthMat->data.i[i];
		nearest = trainPersonNumMat->data.i[iNearest];

		printf("nearest = %d, truth = %d\n", nearest, truth);
	}
}
void learn()
{
    int i, offset;
    numberOfTrainingFaces = loadfaceImageArray("/etc/pam_face_authentication/facemanager/face.key");
    doPCA();
    projectedTrainFaceMat = cvCreateMat( numberOfTrainingFaces, numberOfEigenVectors, CV_32FC1 );
    offset = projectedTrainFaceMat->step / sizeof(float);
    for (i=0; i<numberOfTrainingFaces; i++)
    {
        //int offset = i * numberOfEigenVectors;
        cvEigenDecomposite(
            faceImageArray[i],
            numberOfEigenVectors,
            eigenVectorArray,
            0, 0,
            averageTrainingImage,
            //projectedTrainFaceMat->data.fl + i*numberOfEigenVectors);
            projectedTrainFaceMat->data.fl + i*offset);
    }

    storeTrainingData();
}
示例#14
0
//学习阶段代码
void learn()
{
	int i, offset;

	//加载训练图像集
	nTrainFaces = loadFaceImgArray("train.txt");
	if( nTrainFaces < 2 )
	{
		fprintf(stderr,
			"Need 2 or more training faces\n"
			"Input file contains only %d\n", nTrainFaces);
		return;
	}

	// 进行主成分分析
	doPCA();

	//将训练图集投影到子空间中
	projectedTrainFaceMat = cvCreateMat( nTrainFaces, nEigens, CV_32FC1 );
	offset = projectedTrainFaceMat->step / sizeof(float);
	for(i=0; i<nTrainFaces; i++)
	{
		//int offset = i * nEigens;
		cvEigenDecomposite(
			faceImgArr[i],
			nEigens,
			eigenVectArr,
			0, 0,
			pAvgTrainImg,
			//projectedTrainFaceMat->data.fl + i*nEigens);
			projectedTrainFaceMat->data.fl + i*offset);
	}

	//将训练阶段得到的特征值,投影矩阵等数据存为.xml文件,以备测试时使用
	storeTrainingData();
}
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{

    // Structure for getting video from camera or avi
    CvCapture* capture = 0;

    // Images to capture the frame from video or camera or from file
    IplImage *frame, *frame_copy = 0;

    // Input file name for avi or image file.
    const char* vectorList;
    const char* testImage;

    // Check for the correct usage of the command line
    if( argc == 2 )
    {
        //vectorList = argv[1];
        testImage = argv[1];
    }
    else
    {
        fprintf( stderr, "Usage: eigenDecomp testImage\n" );
        return -1;
    }

    // Allocate the memory storage
    storage = cvCreateMemStorage(0);

    // Create a new named window with title: result
    cvNamedWindow( "result", 1 );

    std::vector<IplImage*> images;
    std::vector<char*> labels;

    //Load Labels
    printf("Loading Labels...  ");
    FILE* f = fopen( "labels.txt", "rt" );
    if( f )
    {
        char buf[100+1];

        // Get the line from the file
        while( fgets( buf, 100, f ) )
        {

            // Remove the spaces if any, and clean up the name
            int len = (int)strlen(buf);
            while( len > 0 && isspace(buf[len-1]) )
                len--;
            buf[len] = '\0';

            char* str = (char*)malloc(sizeof(char)*100);
            memcpy(str, buf, 100);
            labels.push_back(str);
        }
        // Close the file
        fclose(f);
        printf("%d Labels loaded.\n", labels.size());
    } else
        printf("Failed.\n");

    //Load Eigenvectors:
    printf("Loading Eigenvectors...  ");
    CvFileStorage* fs2 = cvOpenFileStorage("eigenvectors.yml", NULL, CV_STORAGE_READ);
    char vectorname[50];
    CvFileNode* vectorloc = cvGetFileNodeByName(fs2, NULL, "vector0");
    for(int i = 1; vectorloc != NULL; i++) {
        images.push_back((IplImage*)cvRead(fs2, vectorloc, &cvAttrList(0,0)));
        //printf("pushed %s\n", vectorname);
        sprintf(vectorname, "vector%d", i);
        vectorloc = cvGetFileNodeByName(fs2, NULL, vectorname);
    }
    //cvReleaseFileStorage(&fs2); This may delete the images
    printf("%d Eigenvectors (and 1 average) loaded.\n", images.size()-1);

    //TODO: unfix this number! - Done!
    //printf("FLANN DIMS: %d, %d\n", 165, images.size());
    //cv::Mat mat( 165, images.size(), CV_32F );
    //flann::Matrix<float> flannmat;
    //flann::load_from_file(flannmat, "flann.dat", "flann.dat");
    //flann::Index::Index flannIndex(flannmat, flann::LinearIndexParams());
    printf("Loading Nearest Neighbor Matrix...  ");
    CvMat* flannmat;
    CvFileStorage* fs = cvOpenFileStorage("nn.yml", NULL, CV_STORAGE_READ);
    flannmat = (CvMat*)cvRead(fs, cvGetFileNodeByName(fs, NULL, "data"), &cvAttrList(0,0));
    //cvReleaseFileStorage(&fs); This will delete the matrix
    printf("Done. DIMS: %d, %d\n", flannmat->rows, flannmat->cols);
    cv::flann::Index::Index flannIndex(flannmat, cv::flann::LinearIndexParams());

    int nn_dims = flannmat->cols; //number of nearest neighbor dimensions available
    int projection_dims = images.size()-1; //number of dimensions to project into eigenspace.

    IplImage* eigenArray[projection_dims];

    IplImage* avgImage = images[0];
    for(int i = 0; i < projection_dims; i++) {
        eigenArray[i] = images[i+1];
    }

    //load test image
    printf("Loading Test Image...\n");
    IplImage* testImg = cvLoadImage(testImage, CV_LOAD_IMAGE_GRAYSCALE);
    float projection[projection_dims];

    // Project the test image onto the PCA subspace
    printf("Conducting Eigen Decomposite...\n");
    cvEigenDecomposite(
        testImg, //test object
        projection_dims, //number of eigen vectors
        (void*)eigenArray, //eigenVectors
        0, 0, //ioflags, user callback data
        avgImage, //root eigen vector
        projection);


    //print projection
    //printf("Eigenvector Coefficents:\n");
    //for(int i = 0; i < projection_dims; i++)
    //printf("%5f ", projection[i]);
    //printf("\n");

    int neighbors = 10; //to test against

    std::vector<float> proj(nn_dims);
    std::vector<float> dists(neighbors);
    std::vector<int> indicies(neighbors);
    for(int i = 0; i < nn_dims; i++)
        proj[i] = projection[i];

    flannIndex.knnSearch(proj, indicies, dists, neighbors, NULL);
    for(int i = 0; i < neighbors; i++)
        printf("Index Match0: %4d, dist: %13.3f, Label: %s\n",
               indicies[i], dists[i], labels[indicies[i]]);

    // Destroy the window previously created with filename: "result"
    cvDestroyWindow("result");
    printf("Done.\n");
    // return 0 to indicate successfull execution of the program
    return 0;
}
// Recognize the face in each of the test images given, and compare the results with the truth.
void recognizeFileList(char *szFileTest)
{
    int i, nTestFaces  = 0;         // the number of test images
    CvMat * trainPersonNumMat = 0;  // the person numbers during training
    float * projectedTestFace = 0;
    char *answer;
    int nCorrect = 0;
    int nWrong = 0;
    double timeFaceRecognizeStart;
    double tallyFaceRecognizeTime;
    float confidence;
    
    // load test images and ground truth for person number
    nTestFaces = loadFaceImgArray(szFileTest);
    printf("%d test faces loaded\n", nTestFaces);
    
    // load the saved training data
    if( !loadTrainingData( &trainPersonNumMat ) ) return;
    
    // project the test images onto the PCA subspace
    projectedTestFace = (float *)cvAlloc( nEigens*sizeof(float) );
    timeFaceRecognizeStart = (double)cvGetTickCount();	// Record the timing.
    for(i=0; i<nTestFaces; i++)
    {
        int iNearest, nearest, truth;
        
        // project the test image onto the PCA subspace
        cvEigenDecomposite(
                           faceImgArr[i],
                           nEigens,
                           eigenVectArr,
                           0, 0,
                           pAvgTrainImg,
                           projectedTestFace);
        
        iNearest = findNearestNeighbor(projectedTestFace, &confidence);
        truth    = personNumTruthMat->data.i[i];
        if(iNearest!=-1)
        {
            nearest  = trainPersonNumMat->data.i[iNearest];
        }
        else
        {
            nearest = 0;
        }
        
        if (nearest == truth) {
            answer = "Correct";
            nCorrect++;
        }
        else {
            answer = "WRONG!";
            nWrong++;
        }
        printf("nearest = %d, Truth = %d (%s). Confidence = %f\n", nearest, truth, answer, confidence);
    }
    tallyFaceRecognizeTime = (double)cvGetTickCount() - timeFaceRecognizeStart;
    if (nCorrect+nWrong > 0) {
        printf("TOTAL ACCURACY: %d%% out of %d tests.\n", nCorrect * 100/(nCorrect+nWrong), (nCorrect+nWrong));
        printf("TOTAL TIME: %.1fms average.\n", tallyFaceRecognizeTime/((double)cvGetTickFrequency() * 1000.0 * (nCorrect+nWrong) ) );
    }
    
}
示例#17
0
/**
 * Performs PCA on the current training data, projects the training faces, and stores them in a DB.
 */
void Eigenfaces::EigenfacesPriv::learn(int index, IplImage* newFace)
{
    int i;
    std::vector<IplImage*> tempFaces;

    tempFaces.push_back(newFace);
    tempFaces.push_back(faceImgArr.at(index));

    float* projectedFace = (float*)malloc(sizeof(float));

    CvSize size=cvSize(FACE_WIDTH, FACE_HEIGHT);

    //Set PCA's termination criterion
    CvTermCriteria mycrit = cvTermCriteria(CV_TERMCRIT_NUMBER,
            1,0);
    //Initialise pointer to the pointers with eigen objects
    IplImage** eigenObjects = new IplImage *[2];

    float* eigenValues;
    //Initialize array with eigen values
    if( !(eigenValues = (float*) cvAlloc( 2*sizeof(float) ) ) )
        cout<<"Problems initializing eigenValues..."<<endl;

    IplImage* pAvgTrainImg;
    //Initialize pointer to the average image
    if( !(pAvgTrainImg = cvCreateImage( size, IPL_DEPTH_32F, 1) ) )
        cout<<"Problems initializing pAvgTrainImg..."<<endl;

    for(i = 0; i < 2; i++ )
    {
        eigenObjects[i] = cvCreateImage( size, IPL_DEPTH_32F, 1 );
        if(!(eigenObjects[i] ) )
            cout<<"Problems initializing eigenObjects"<<endl;
    }

    //Perform PCA
    cvCalcEigenObjects(2, &tempFaces.front(), eigenObjects,
            CV_EIGOBJ_NO_CALLBACK, 0, NULL, &mycrit, pAvgTrainImg, eigenValues );

    cvEigenDecomposite(tempFaces.at(0), 1, eigenObjects,
            CV_EIGOBJ_NO_CALLBACK, NULL, pAvgTrainImg, projectedFace );

    IplImage* proj = cvCreateImage(size, IPL_DEPTH_8U, 1);
    cvEigenProjection(eigenObjects, 1,
            CV_EIGOBJ_NO_CALLBACK, NULL, projectedFace, pAvgTrainImg, proj);

    //LibFaceUtils::showImage(proj);

    cvReleaseImage(&faceImgArr.at(index));
    faceImgArr.at(index) = proj;

    //free other stuff allocated above.
    cvFree_(eigenValues);
    free(projectedFace);

    cvReleaseImage(&pAvgTrainImg);
    cvReleaseImage(&eigenObjects[0]);
    cvReleaseImage(&eigenObjects[1]);

    tempFaces.clear();
}