Example #1
0
// Start training from the collected faces.
// The face recognition algorithm can be one of these and perhaps more, depending on your version of OpenCV, which must be atleast v2.4.1:
//    "FaceRecognizer.Eigenfaces":  Eigenfaces, also referred to as PCA (Turk and Pentland, 1991).
//    "FaceRecognizer.Fisherfaces": Fisherfaces, also referred to as LDA (Belhumeur et al, 1997).
//    "FaceRecognizer.LBPH":        Local Binary Pattern Histograms (Ahonen et al, 2006).
Ptr<FaceRecognizer> learnCollectedFaces(const vector<Mat> preprocessedFaces, const vector<int> faceLabels, const string facerecAlgorithm)
{
    Ptr<FaceRecognizer> model;

    cout << "Learning the collected faces using the [" << facerecAlgorithm << "] algorithm ..." << endl;

    // Make sure the "contrib" module is dynamically loaded at runtime.
    // Requires OpenCV v2.4.1 or later (from June 2012), otherwise the FaceRecognizer will not compile or run!
    bool haveContribModule = initModule_contrib();
    if (!haveContribModule) {
        cerr << "ERROR: The 'contrib' module is needed for FaceRecognizer but has not been loaded into OpenCV!" << endl;
        exit(1);
    }

    // Use the new FaceRecognizer class in OpenCV's "contrib" module:
    // Requires OpenCV v2.4.1 or later (from June 2012), otherwise the FaceRecognizer will not compile or run!
    model = Algorithm::create<FaceRecognizer>(facerecAlgorithm);
    if (model.empty()) {
        cerr << "ERROR: The FaceRecognizer algorithm [" << facerecAlgorithm << "] is not available in your version of OpenCV. Please update to OpenCV v2.4.1 or newer." << endl;
        exit(1);
    }

    // Do the actual training from the collected faces. Might take several seconds or minutes depending on input!
    model->train(preprocessedFaces, faceLabels);

    return model;
}
// Start training from the collected faces.
// The face recognition algorithm can be one of these and perhaps more, depending on your version of OpenCV, which must be atleast v2.4.1:
//    "FaceRecognizer.Eigenfaces":  Eigenfaces, also referred to as PCA (Turk and Pentland, 1991).
//    "FaceRecognizer.Fisherfaces": Fisherfaces, also referred to as LDA (Belhumeur et al, 1997).
//    "FaceRecognizer.LBPH":        Local Binary Pattern Histograms (Ahonen et al, 2006).
//Ptr<FaceRecognizer> learnCollectedFaces(const vector<Mat> preprocessedFaces, const vector<int> faceLabels, const string facerecAlgorithm)
void *learnCollectedFaces(void *arg)
{
    Ptr<FaceRecognizer> model;
	
	while (true)
	{				
		//std::unique_lock<std::mutex> lock(trainingSignalMutex);
		//waitForTrainingSignal.wait(lock, [] {return 1;});
		pthread_mutex_lock(&ptrainingSignalMutex);
		pthread_cond_wait(&pwaitForTrainingSignal, &ptrainingSignalMutex);
		
		cout << "Learning the collected faces using the [" << facerecAlgorithm << "] algorithm ..." << endl;

		// Make sure the "contrib" module is dynamically loaded at runtime.
		// Requires OpenCV v2.4.1 or later (from June 2012), otherwise the FaceRecognizer will not compile or run!
		bool haveContribModule = initModule_contrib();
		if (!haveContribModule) {
		    cerr << "ERROR: The 'contrib' module is needed for FaceRecognizer but has not been loaded into OpenCV!" << endl;
		    exit(1);
		}

		// Use the new FaceRecognizer class in OpenCV's "contrib" module:
		// Requires OpenCV v2.4.1 or later (from June 2012), otherwise the FaceRecognizer will not compile or run!
		model = Algorithm::create<FaceRecognizer>(facerecAlgorithm);
		if (model.empty()) {
		    cerr << "ERROR: The FaceRecognizer algorithm [" << facerecAlgorithm << "] is not available in your version of OpenCV. Please update to OpenCV v2.4.1 or newer." << endl;
		    exit(1);
		}
		
		trackTrainingCompletion = 1;
		// Do the actual training from the collected faces. Might take several seconds or minutes depending on input!
		model->train(preprocessedFaces, faceLabels);
		if (canUseFaceRecognitionModel == false)
		{
			// This variable prevents use of face recognizer model, if training has not been done even once
			canUseFaceRecognitionModel = true;
		}
		trackTrainingCompletion = 2;
		
		pthread_mutex_unlock(&ptrainingSignalMutex);

		//return model;
		//std::lock_guard<std::mutex> guard(modelUpdationMutex);
		pthread_mutex_lock(&pmodelUpdationMutex);
		faceRecognitionModel = model;
		pthread_mutex_unlock(&pmodelUpdationMutex);
	}
}