diff --git a/src/ofxFaceRecognizer.cpp b/src/ofxFaceRecognizer.cpp index 222df92..0bf973d 100644 --- a/src/ofxFaceRecognizer.cpp +++ b/src/ofxFaceRecognizer.cpp @@ -70,6 +70,9 @@ void ofxFaceRecognizer::setup(int method_used, int _maxFaces, bool bAlreadySaved } + if(methodId == 0 || methodId == 1) generateEigenFishFaces(); + + } void ofxFaceRecognizer::update(ofImage _img){ @@ -144,6 +147,126 @@ void ofxFaceRecognizer::loadTrainingImages(string _folderName, int _maxFaces){ } +static Mat norm_0_255(InputArray _src) { + Mat src = _src.getMat(); + // Create and return normalized image: + Mat dst; + switch(src.channels()) { + case 1: + cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC1); + break; + case 3: + cv::normalize(_src, dst, 0, 255, NORM_MINMAX, CV_8UC3); + break; + default: + src.copyTo(dst); + break; + } + return dst; +} + +void ofxFaceRecognizer::generateEigenFishFaces(){ + int height = allTrainingMats[0].rows; + + // Here is how to get the eigenvalues of this Eigenfaces model: + Mat eigenvalues = model->getMat("eigenvalues"); + cout<<"eigenvalues.row "<getMat("eigenvectors"); + cout<<"W.row "<getMat("mean"); + cout<<"mean.row "<(a)); + cout << msg << endl; + // get eigenvector #a + Mat ev = W.col(a).clone(); + // Reshape to original size & normalize to [0...255] for imshow. + Mat grayscale = norm_0_255(ev.reshape(1, height)); + // Show the image & apply a Bone colormap for better sensing. + Mat cgrayscale; + applyColorMap(grayscale, cgrayscale, COLORMAP_JET); + + // Display or save: + ofImage ti; + toOf(cgrayscale,ti); + cgrayscaleJET_array.push_back(ti); + + applyColorMap(grayscale, cgrayscale, COLORMAP_BONE); + toOf(cgrayscale,ti); + cgrayscaleBONE_array.push_back(ti); + + } + + + reconstruction_array.clear(); + // Display or save the image reconstruction at some predefined steps: + if(methodId == 0){ + for(int num_component = min(W.cols, 10); num_component < min(W.cols, 300); num_component+=15) { + Mat evs = Mat(W, Range::all(), Range(0, num_component)); + Mat projection = subspaceProject(evs, mean, allTrainingMats[0].reshape(1,1)); + Mat reconstruction = subspaceReconstruct(evs, mean, projection); + // Normalize the result: + reconstruction = norm_0_255(reconstruction.reshape(1, allTrainingMats[0].rows)); + + // Normalize the result: + reconstruction = norm_0_255(reconstruction.reshape(1, allTrainingMats[0].rows)); + // Display or save: + + ofImage ti; + toOf(reconstruction,ti); + //reconstruction_array[num_component] = ti; + reconstruction_array.push_back(ti); + } + } + if(methodId == 1){ + for(int num_component = 0; num_component < min(16, W.cols); num_component++) { + // Slice the Fisherface from the model: + Mat ev = W.col(num_component); + Mat projection = subspaceProject(ev, mean, allTrainingMats[0].reshape(1,1)); + Mat reconstruction = subspaceReconstruct(ev, mean, projection); + + // Normalize the result: + reconstruction = norm_0_255(reconstruction.reshape(1, allTrainingMats[0].rows)); + + // Display or save: + ofImage ti; + toOf(reconstruction,ti); + reconstruction_array.push_back(ti); + } + } + +} + +int ofxFaceRecognizer::getEigenfaceSize(){ + return cgrayscaleJET_array.size(); +} +int ofxFaceRecognizer::getReconstructionSize(){ + return reconstruction_array.size(); +} +void ofxFaceRecognizer::drawEigefaceJET(int _id, float _x, float _y, float _w, float _h){ + cgrayscaleJET_array[_id].draw(_x, _y, _w, _h); +} +void ofxFaceRecognizer::drawEigenfaceBONE(int _id, float _x, float _y, float _w, float _h){ + cgrayscaleBONE_array[_id].draw(_x, _y, _w, _h); +} + +void ofxFaceRecognizer::drawReconstructionImage(int _id, float _x, float _y, float _w, float _h){ + reconstruction_array[_id].draw(_x, _y, _w, _h); +} + int ofxFaceRecognizer::getPrediction(){ return prediction; } diff --git a/src/ofxFaceRecognizer.h b/src/ofxFaceRecognizer.h index 747c3ed..037ba4f 100644 --- a/src/ofxFaceRecognizer.h +++ b/src/ofxFaceRecognizer.h @@ -40,6 +40,13 @@ class ofxFaceRecognizer { int getMethodId(); string getMethodName(); + void generateEigenFishFaces(); + void drawEigefaceJET(int _id, float _x, float _y, float _w, float _h); + void drawEigenfaceBONE(int _id, float _x, float _y, float _w, float _h); + void drawReconstructionImage(int _id, float _x, float _y, float _w, float _h); + int getEigenfaceSize(); + int getReconstructionSize(); + vector allTrainingLabels; protected: @@ -47,13 +54,7 @@ class ofxFaceRecognizer { Ptr model; int maxFaces; - - /* - int method_used; - int maxFaces; - bool bAlreadySavedModel; - string folderName; - */ + vector allTrainingImages; vector oneImagePerPerson; @@ -70,4 +71,9 @@ class ofxFaceRecognizer { string methodName; int methodId; + vectorcgrayscaleJET_array; + vectorcgrayscaleBONE_array; + vectorreconstruction_array; + + }; \ No newline at end of file