// 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); }
private void Run() { List <Mat> images = new List <Mat> (); List <int> labelsList = new List <int> (); MatOfInt labels = new MatOfInt(); images.Add(Imgcodecs.imread(image_0_filepath, 0)); images.Add(Imgcodecs.imread(image_1_filepath, 0)); labelsList.Add(0); labelsList.Add(1); labels.fromList(labelsList); Mat testSampleMat = Imgcodecs.imread(sample_image_filepath, 0); int testSampleLabel = 0; // foreach (Mat item in images) { // Debug.Log ("images.ToString " + item.ToString ()); // } // foreach (int item in labelsList) { // Debug.Log ("labels.ToString " + item.ToString ()); // } int[] predictedLabel = new int[1]; double[] predictedConfidence = new double[1]; BasicFaceRecognizer faceRecognizer = EigenFaceRecognizer.create(); faceRecognizer.train(images, labels); faceRecognizer.predict(testSampleMat, predictedLabel, predictedConfidence); Debug.Log("Predicted class: " + predictedLabel [0] + " / " + "Actual class: " + testSampleLabel); Debug.Log("Confidence: " + predictedConfidence [0]); Mat predictedMat = images [predictedLabel [0]]; Mat baseMat = new Mat(testSampleMat.rows(), predictedMat.cols() + testSampleMat.cols(), CvType.CV_8UC1); predictedMat.copyTo(baseMat.submat(new OpenCVForUnity.CoreModule.Rect(0, 0, predictedMat.cols(), predictedMat.rows()))); testSampleMat.copyTo(baseMat.submat(new OpenCVForUnity.CoreModule.Rect(predictedMat.cols(), 0, testSampleMat.cols(), testSampleMat.rows()))); Imgproc.putText(baseMat, "Predicted", new Point(10, 15), Imgproc.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(255), 1, Imgproc.LINE_AA, false); Imgproc.putText(baseMat, "Confidence:", new Point(5, 25), Imgproc.FONT_HERSHEY_SIMPLEX, 0.2, new Scalar(255), 1, Imgproc.LINE_AA, false); Imgproc.putText(baseMat, " " + predictedConfidence [0], new Point(5, 33), Imgproc.FONT_HERSHEY_SIMPLEX, 0.2, new Scalar(255), 1, Imgproc.LINE_AA, false); Imgproc.putText(baseMat, "TestSample", new Point(predictedMat.cols() + 10, 15), Imgproc.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(255), 1, Imgproc.LINE_AA, false); Texture2D texture = new Texture2D(baseMat.cols(), baseMat.rows(), TextureFormat.RGBA32, false); Utils.matToTexture2D(baseMat, texture); gameObject.GetComponent <Renderer> ().material.mainTexture = texture; }