Ptr<FaceRecognizer> Trainer::load(const char* fname,int type) { Ptr<FaceRecognizer> model; if(type==0) model=createFisherFaceRecognizer(); else model=createLBPHFaceRecognizer(); model->load(fname); char lable_fname[MAX_PATH]; sprintf(lable_fname,"%s.labels",fname); FILE* fp=fopen(lable_fname,"rt"); if(fp==0) { printf("Fail to load .labels file, please re-train the model\n"); return NULL; } int class_count=0; fscanf(fp,"%d",&class_count);fgetc(fp); names.clear(); char buf[256]; for(int i=0;i<class_count;++i) { fscanf(fp,"%s",buf); names.push_back(buf); } fclose(fp); return model; }
ICLASS_API FaceRecognizer* __stdcall Create_LBPHFaceRecognizer(int radius, int neighbors, int grid_x, int grid_y, double threshold) { Ptr<FaceRecognizer> r = createLBPHFaceRecognizer(radius, neighbors, grid_x, grid_y, threshold); r.addref(); return r; }
PersonRecognizer::PersonRecognizer(const vector<Mat> &imgs, int radius, int neighbors, int grid_x, int grid_y, double threshold) { //all images are faces of the same person, so initialize the same label for all. vector<int> labels(imgs.size()); for (vector<int>::iterator it = labels.begin() ; it != labels.end() ; *(it++) = PERSON_LABEL); _faceSize = Size(imgs[0].size().width, imgs[0].size().height); //build recognizer model: _model = createLBPHFaceRecognizer(radius, neighbors, grid_x, grid_y, threshold); _model->train(imgs, labels); }
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); } }
void haar_cascade::train() { // Create a FaceRecognizer and train it on the given images: // model = createFisherFaceRecognizer(); model = createLBPHFaceRecognizer(); model->train(this->images, this->labels); // That's it for learning the Face Recognition model. You now // need to create the classifier for the task of Face Detection. // We are going to use the haar cascade you have specified in the // command line arguments: // this->cascade.load(this->haar_path); }
Ptr<FaceRecognizer> new_person_detection(string filename){ Ptr<FaceRecognizer> LBPH_model = createLBPHFaceRecognizer(); vector<Mat> images; vector<int> labels; try { read_csv(filename, images, labels); } catch (Exception& e) { cerr << "Error opening file \"" << filename << "\". Reason: " << e.msg << endl; // nothing more we can do exit(1); } LBPH_model -> train(images, labels); return LBPH_model; }
void Trainer::train(const char* saveto,int type) { Ptr<FaceRecognizer> model; if(type==0) model=createFisherFaceRecognizer(); else model=createLBPHFaceRecognizer(); printf("\nbegin training...\n"); model->train(images, labels); model->save(saveto); char lable_fname[MAX_PATH]; sprintf(lable_fname,"%s.labels",saveto); FILE* fp=fopen(lable_fname,"wt"); fprintf(fp,"%d\n",names.size()); for(int i=0,iLen=names.size();i<iLen;++i) { fprintf(fp,"%s\n",names[i].c_str()); } fclose(fp); printf("Trained model saved to \"%s\"\n",saveto); }
//Globals #include "global.hpp" #ifndef GLOBAL #define GLOBAL string file_name = "lbph_results_400_with_filter.csv"; int conf = 400; string face_cascade_name = "lbpcascade_frontalface.xml"; //string face_cascade_name = "haarcascade_frontalface_alt.xml"; CascadeClassifier face_cascade; Ptr<FaceRecognizer> model_lbph = createLBPHFaceRecognizer(1,8,8,8, conf); #endif