forked from EnoxSoftware/OpenCVForUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceRecognizerSample.cs
More file actions
91 lines (65 loc) · 3.04 KB
/
Copy pathFaceRecognizerSample.cs
File metadata and controls
91 lines (65 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
using UnityEngine.SceneManagement;
#endif
using OpenCVForUnity;
namespace OpenCVForUnitySample
{
/// <summary>
/// FaceRecognizer sample.
/// http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html#eigenfaces
/// </summary>
public class FaceRecognizerSample : MonoBehaviour
{
// Use this for initialization
void Start ()
{
List<Mat> images = new List<Mat> ();
List<int> labelsList = new List<int> ();
MatOfInt labels = new MatOfInt ();
images.Add (Imgcodecs.imread (Utils.getFilePath ("facerec/facerec_0.bmp"), 0));
images.Add (Imgcodecs.imread (Utils.getFilePath ("facerec/facerec_1.bmp"), 0));
labelsList.Add (0);
labelsList.Add (1);
labels.fromList (labelsList);
Mat testSampleMat = Imgcodecs.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];
BasicFaceRecognizer faceRecognizer = Face.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 ())));
Imgproc.putText (baseMat, "Predicted", new Point (10, baseMat.rows () - 5), Core.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar (255), 1, Imgproc.LINE_AA, false);
Imgproc.putText (baseMat, "TestSample", new Point (predictedMat.cols () + 10, baseMat.rows () - 5), Core.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;
}
// Update is called once per frame
void Update ()
{
}
public void OnBackButton ()
{
#if UNITY_5_3 || UNITY_5_3_OR_NEWER
SceneManager.LoadScene ("OpenCVForUnitySample");
#else
Application.LoadLevel ("OpenCVForUnitySample");
#endif
}
}
}