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; }
void ofxFaceRecognizer::setup(int method_used, int _maxFaces, bool bAlreadySavedModel, string folderName) { //eigen take much longer to load and longer to generate. also makes much larger yml file string method_names[3] = {"eigen","fisher","lbph"}; // Create a FaceRecognizer and train it on the given images: methodId = method_used; methodName = method_names[method_used]; if(methodId == 0){ model = createEigenFaceRecognizer(); } if(methodId == 1){ model = createFisherFaceRecognizer(); } if(methodId == 2){ model = createLBPHFaceRecognizer(); } //if(_maxFaces > 0){ maxFaces = _maxFaces; //by default my training images should be 150x150 pixels //will reset if you use loadTrainingImages() image_width = 150; image_height = 150; //load in training images loadTrainingImages(folderName,maxFaces); string compiledDatabasePath = ofToDataPath("model_"+methodName+"_"+ofToString(maxFaces)+"_"+folderName+".yml"); if(bAlreadySavedModel){ cout<<"model .yml supposedly existing"<<endl; model->load(compiledDatabasePath); cout<<"loaded "<<maxFaces<<" faces with model "<<methodName<<endl; }else{ cout<<"start training new model. this might take a very long time"<<endl; cout<<"compiledDatabasePath "<<compiledDatabasePath<<endl; cout<<"more so for fisher than eigen"<<endl; model->train(allTrainingMats, allTrainingLabels); model->save(ofToDataPath(compiledDatabasePath)); cout<<"trained and saved .yml with "<<maxFaces<<" faces with model "<<methodName<<endl; } // Quit if there are not enough images for this demo. if(allTrainingMats.size() <= 1) { string error_message = "This demo needs at least 2 images to work. Please add more images to your data set!"; CV_Error(CV_StsError, error_message); } }