// 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;
        }
        // Use this for initialization
        void Start()
        {
            List <Mat> images     = new List <Mat> ();
            List <int> labelsList = new List <int> ();
            MatOfInt   labels     = new MatOfInt();

            images.Add(Highgui.imread(Utils.getFilePath("facerec/facerec_0.bmp"), 0));
            images.Add(Highgui.imread(Utils.getFilePath("facerec/facerec_1.bmp"), 0));

            labelsList.Add(0);
            labelsList.Add(1);
            labels.fromList(labelsList);

            Mat testSampleMat = Highgui.imread(Utils.getFilePath("facerec/facerec_sample.bmp"), 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];


            FaceRecognizer faceRecognizer = FaceRecognizer.createEigenFaceRecognizer();

            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.Rect(0, 0, predictedMat.cols(), predictedMat.rows())));
            testSampleMat.copyTo(baseMat.submat(new OpenCVForUnity.Rect(predictedMat.cols(), 0, testSampleMat.cols(), testSampleMat.rows())));

            Core.putText(baseMat, "Predicted", new Point(10, baseMat.rows() - 5), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(255), 1, Core.LINE_AA, false);
            Core.putText(baseMat, "TestSample", new Point(predictedMat.cols() + 10, baseMat.rows() - 5), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(255), 1, Core.LINE_AA, false);


            Texture2D texture = new Texture2D(baseMat.cols(), baseMat.rows(), TextureFormat.RGBA32, false);

            Utils.matToTexture2D(baseMat, texture);

            gameObject.GetComponent <Renderer> ().material.mainTexture = texture;
        }
				// Use this for initialization
				void Start ()
				{

						List<Mat> images = new List<Mat> ();
						List<int> labelsList = new List<int> ();
						MatOfInt labels = new MatOfInt ();
						images.Add (Highgui.imread (Utils.getFilePath ("facerec/facerec_0.bmp"), 0));
						images.Add (Highgui.imread (Utils.getFilePath ("facerec/facerec_1.bmp"), 0));

						labelsList.Add (0);
						labelsList.Add (1);
						labels.fromList (labelsList);

						Mat testSampleMat = Highgui.imread (Utils.getFilePath ("facerec/facerec_sample.bmp"), 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];

			
						FaceRecognizer faceRecognizer = FaceRecognizer.createEigenFaceRecognizer ();

						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.Rect (0, 0, predictedMat.cols (), predictedMat.rows ())));
						testSampleMat.copyTo (baseMat.submat (new OpenCVForUnity.Rect (predictedMat.cols (), 0, testSampleMat.cols (), testSampleMat.rows ())));

						Core.putText (baseMat, "Predicted", new Point (10, baseMat.rows () - 5), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar (255), 1, Core.LINE_AA, false);
						Core.putText (baseMat, "TestSample", new Point (predictedMat.cols () + 10, baseMat.rows () - 5), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar (255), 1, Core.LINE_AA, false);

			
						Texture2D texture = new Texture2D (baseMat.cols (), baseMat.rows (), TextureFormat.RGBA32, false);

						Utils.matToTexture2D (baseMat, texture);

						gameObject.GetComponent<Renderer> ().material.mainTexture = texture;

				}
Пример #5
0
    private void Run()
    {
        List <Mat> images     = new List <Mat>();
        List <int> labelsList = new List <int>();
        MatOfInt   labels     = new MatOfInt();

        images.Add(Imgcodecs.imread(facerec_0_bmp_filepath, 0));
        images.Add(Imgcodecs.imread(facerec_1_bmp_filepath, 0));

        labelsList.Add(0); //积极
        labelsList.Add(1); //消极
        labels.fromList(labelsList);

        Mat testSampleMat = Imgcodecs.imread(facerec_sample_bmp_filepath, 0);

        int testSampleLabel = 0;

        int[]    predictedLabel      = new int[1];
        double[] predictedConfidence = new double[1];

        BasicFaceRecognizer faceRecognizer = EigenFaceRecognizer.create();

        //faceRecognizer.train(images, labels); //会清空之前模型
        faceRecognizer.update(images, labels); //不会清空load的模型
        faceRecognizer.predict(testSampleMat, predictedLabel, predictedConfidence);
        //faceRecognizer.save(Application.dataPath + "/Cascades/train_face.txt"); //生成一个txt

        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.Rect(0, 0, predictedMat.cols(), predictedMat.rows())));
        testSampleMat.copyTo(baseMat.submat(new OpenCVForUnity.Rect(predictedMat.cols(), 0, testSampleMat.cols(), testSampleMat.rows())));

        Imgproc.putText(baseMat, "Predicted", new Point(10, 15), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(255), 1, Imgproc.LINE_AA, false);
        Imgproc.putText(baseMat, "Confidence:", new Point(5, 25), Core.FONT_HERSHEY_SIMPLEX, 0.2, new Scalar(255), 1, Imgproc.LINE_AA, false);
        Imgproc.putText(baseMat, "   " + predictedConfidence[0], new Point(5, 33), Core.FONT_HERSHEY_SIMPLEX, 0.2, new Scalar(255), 1, Imgproc.LINE_AA, false);
        Imgproc.putText(baseMat, "TestSample", new Point(predictedMat.cols() + 10, 15), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar(255), 1, Imgproc.LINE_AA, false);

        Texture2D t2d = new Texture2D(baseMat.width(), baseMat.height());

        Utils.matToTexture2D(baseMat, t2d);
        Sprite sp = Sprite.Create(t2d, new UnityEngine.Rect(0, 0, t2d.width, t2d.height), Vector2.zero);

        m_srcImage.sprite         = sp;
        m_srcImage.preserveAspect = true;
    }