Example #1
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();
}
Example #2
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);
	}
}
Example #3
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);
 }
}
Example #4
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();
}
Example #5
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;
}
Example #6
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();
}
// 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();
    }
    
}
Example #8
0
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();
}
Example #9
0
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);
	}
}
Example #10
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();
}
// 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) ) );
    }
    
}