// 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). public static BasicFaceRecognizer LearnCollectedFaces(List <Mat> preprocessedFaces, List <int> faceLabels, string facerecAlgorithm = "FaceRecognizer.Eigenfaces") { BasicFaceRecognizer model = null; Debug.Log("Learning the collected faces using the [" + facerecAlgorithm + "] algorithm ..."); if (facerecAlgorithm == "FaceRecognizer.Fisherfaces") { model = FisherFaceRecognizer.create(); } else if (facerecAlgorithm == "FaceRecognizer.Eigenfaces") { model = EigenFaceRecognizer.create(); } if (model == null) { Debug.LogError("ERROR: The FaceRecognizer algorithm [" + facerecAlgorithm + "] is not available in your version of OpenCV. Please update to OpenCV v2.4.1 or newer."); //exit(1); } // Do the actual training from the collected faces. Might take several seconds or minutes depending on input! MatOfInt labels = new MatOfInt(); labels.fromList(faceLabels); model.train(preprocessedFaces, labels); return(model); }
/// <summary> /// Raises the load button click event. /// </summary> public void LoadModel() { string loadDirectoryPath = Path.Combine(Application.persistentDataPath, saveDirectoryName); if (!Directory.Exists(loadDirectoryPath)) { Debug.Log("load failure. saved train data file does not exist."); return; } // Restart everything! dispose(); if (facerecAlgorithm == "FaceRecognizer.Fisherfaces") { model = FisherFaceRecognizer.create(); } else if (facerecAlgorithm == "FaceRecognizer.Eigenfaces") { model = EigenFaceRecognizer.create(); } if (model == null) { Debug.LogError("ERROR: The FaceRecognizer algorithm [" + facerecAlgorithm + "] is not available in your version of OpenCV. Please update to OpenCV v2.4.1 or newer."); m_mode = R_MODES.MODE_DETECTION; return; } // load the train data. model.read(Path.Combine(loadDirectoryPath, "traindata.yml")); int maxLabel = (int)Core.minMaxLoc(model.getLabels()).maxVal; if (maxLabel < 0) { Debug.Log("load failure."); model.Dispose(); return; } // Restore the save data. #if UNITY_WEBGL && !UNITY_EDITOR string format = "jpg"; #else string format = "png"; #endif m_numPersons = maxLabel + 1; personsNames = new string[m_numPersons]; for (int i = 0; i < m_numPersons; ++i) { personsNames[i] = GameManager.instance.personsNames[i]; m_latestFaces.Add(i); preprocessedFaces.Add(Imgcodecs.imread(Path.Combine(loadDirectoryPath, "preprocessedface" + i + "." + format), 0)); if (preprocessedFaces[i].total() == 0) { preprocessedFaces[i] = new Mat(faceHeight, faceWidth, CvType.CV_8UC1, new Scalar(128)); } faceLabels.Add(i); } // go to the recognition mode! m_mode = R_MODES.MODE_RECOGNITION; }