FaceRecognizerExample.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using OpenCVForUnity.CoreModule;
  7. using OpenCVForUnity.ImgcodecsModule;
  8. using OpenCVForUnity.FaceModule;
  9. using OpenCVForUnity.ImgprocModule;
  10. using OpenCVForUnity.UnityUtils;
  11. namespace OpenCVForUnityExample
  12. {
  13. /// <summary>
  14. /// FaceRecognizer Example
  15. /// An example of human face recognition using the face (Face Recognition) module.
  16. /// http://docs.opencv.org/modules/contrib/doc/facerec/facerec_tutorial.html#eigenfaces
  17. /// </summary>
  18. public class FaceRecognizerExample : MonoBehaviour
  19. {
  20. string facerec_0_bmp_filepath;
  21. string facerec_1_bmp_filepath;
  22. string facerec_sample_bmp_filepath;
  23. #if UNITY_WEBGL && !UNITY_EDITOR
  24. IEnumerator getFilePath_Coroutine;
  25. #endif
  26. // Use this for initialization
  27. void Start ()
  28. {
  29. #if UNITY_WEBGL && !UNITY_EDITOR
  30. getFilePath_Coroutine = GetFilePath ();
  31. StartCoroutine (getFilePath_Coroutine);
  32. #else
  33. facerec_0_bmp_filepath = Utils.getFilePath ("facerec/facerec_0.bmp");
  34. facerec_1_bmp_filepath = Utils.getFilePath ("facerec/facerec_1.bmp");
  35. facerec_sample_bmp_filepath = Utils.getFilePath ("facerec/facerec_sample.bmp");
  36. Run ();
  37. #endif
  38. }
  39. #if UNITY_WEBGL && !UNITY_EDITOR
  40. private IEnumerator GetFilePath()
  41. {
  42. var getFilePathAsync_0_Coroutine = Utils.getFilePathAsync ("facerec/facerec_0.bmp", (result) => {
  43. facerec_0_bmp_filepath = result;
  44. });
  45. yield return getFilePathAsync_0_Coroutine;
  46. var getFilePathAsync_1_Coroutine = Utils.getFilePathAsync ("facerec/facerec_1.bmp", (result) => {
  47. facerec_1_bmp_filepath = result;
  48. });
  49. yield return getFilePathAsync_1_Coroutine;
  50. var getFilePathAsync_sample_Coroutine = Utils.getFilePathAsync ("facerec/facerec_sample.bmp", (result) => {
  51. facerec_sample_bmp_filepath = result;
  52. });
  53. yield return getFilePathAsync_sample_Coroutine;
  54. getFilePath_Coroutine = null;
  55. Run ();
  56. }
  57. #endif
  58. private void Run ()
  59. {
  60. List<Mat> images = new List<Mat> ();
  61. List<int> labelsList = new List<int> ();
  62. MatOfInt labels = new MatOfInt ();
  63. images.Add (Imgcodecs.imread (facerec_0_bmp_filepath, 0));
  64. images.Add (Imgcodecs.imread (facerec_1_bmp_filepath, 0));
  65. labelsList.Add (0);
  66. labelsList.Add (1);
  67. labels.fromList (labelsList);
  68. Mat testSampleMat = Imgcodecs.imread (facerec_sample_bmp_filepath, 0);
  69. int testSampleLabel = 0;
  70. // foreach (Mat item in images) {
  71. // Debug.Log ("images.ToString " + item.ToString ());
  72. // }
  73. // foreach (int item in labelsList) {
  74. // Debug.Log ("labels.ToString " + item.ToString ());
  75. // }
  76. int[] predictedLabel = new int[1];
  77. double[] predictedConfidence = new double[1];
  78. BasicFaceRecognizer faceRecognizer = EigenFaceRecognizer.create ();
  79. faceRecognizer.train (images, labels);
  80. faceRecognizer.predict (testSampleMat, predictedLabel, predictedConfidence);
  81. Debug.Log ("Predicted class: " + predictedLabel [0] + " / " + "Actual class: " + testSampleLabel);
  82. Debug.Log ("Confidence: " + predictedConfidence [0]);
  83. Mat predictedMat = images [predictedLabel [0]];
  84. Mat baseMat = new Mat (testSampleMat.rows (), predictedMat.cols () + testSampleMat.cols (), CvType.CV_8UC1);
  85. predictedMat.copyTo (baseMat.submat (new OpenCVForUnity.CoreModule.Rect (0, 0, predictedMat.cols (), predictedMat.rows ())));
  86. testSampleMat.copyTo (baseMat.submat (new OpenCVForUnity.CoreModule.Rect (predictedMat.cols (), 0, testSampleMat.cols (), testSampleMat.rows ())));
  87. Imgproc.putText (baseMat, "Predicted", new Point (10, 15), Imgproc.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar (255), 1, Imgproc.LINE_AA, false);
  88. Imgproc.putText (baseMat, "Confidence:", new Point (5, 25), Imgproc.FONT_HERSHEY_SIMPLEX, 0.2, new Scalar (255), 1, Imgproc.LINE_AA, false);
  89. Imgproc.putText (baseMat, " " + predictedConfidence [0], new Point (5, 33), Imgproc.FONT_HERSHEY_SIMPLEX, 0.2, new Scalar (255), 1, Imgproc.LINE_AA, false);
  90. Imgproc.putText (baseMat, "TestSample", new Point (predictedMat.cols () + 10, 15), Imgproc.FONT_HERSHEY_SIMPLEX, 0.4, new Scalar (255), 1, Imgproc.LINE_AA, false);
  91. Texture2D texture = new Texture2D (baseMat.cols (), baseMat.rows (), TextureFormat.RGBA32, false);
  92. Utils.matToTexture2D (baseMat, texture);
  93. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  94. }
  95. // Update is called once per frame
  96. void Update ()
  97. {
  98. }
  99. /// <summary>
  100. /// Raises the destroy event.
  101. /// </summary>
  102. void OnDestroy ()
  103. {
  104. #if UNITY_WEBGL && !UNITY_EDITOR
  105. if (getFilePath_Coroutine != null) {
  106. StopCoroutine (getFilePath_Coroutine);
  107. ((IDisposable)getFilePath_Coroutine).Dispose ();
  108. }
  109. #endif
  110. }
  111. /// <summary>
  112. /// Raises the back button click event.
  113. /// </summary>
  114. public void OnBackButtonClick ()
  115. {
  116. SceneManager.LoadScene ("OpenCVForUnityExample");
  117. }
  118. }
  119. }