FaceDetectionExample.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using OpenCVForUnity.ObjdetectModule;
  7. using OpenCVForUnity.CoreModule;
  8. using OpenCVForUnity.ImgprocModule;
  9. using OpenCVForUnity.UnityUtils;
  10. namespace OpenCVForUnityExample
  11. {
  12. /// <summary>
  13. /// Face Detection Example
  14. /// An example of human face detection using the CascadeClassifier class.
  15. /// http://docs.opencv.org/3.2.0/db/d28/tutorial_cascade_classifier.html
  16. /// </summary>
  17. public class FaceDetectionExample : MonoBehaviour
  18. {
  19. CascadeClassifier cascade;
  20. /// <summary>
  21. /// HAAR_CASCADE_FILENAME
  22. /// </summary>
  23. protected static readonly string HAAR_CASCADE_FILENAME = "OpenCVForUnity/objdetect/haarcascade_frontalface_alt.xml";
  24. #if UNITY_WEBGL
  25. IEnumerator getFilePath_Coroutine;
  26. #endif
  27. // Use this for initialization
  28. void Start()
  29. {
  30. #if UNITY_WEBGL
  31. getFilePath_Coroutine = Utils.getFilePathAsync (HAAR_CASCADE_FILENAME,
  32. (result) => {
  33. getFilePath_Coroutine = null;
  34. if (string.IsNullOrEmpty(result))
  35. {
  36. Debug.LogError(HAAR_CASCADE_FILENAME + " is not loaded. Please move from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/” to “Assets/StreamingAssets/OpenCVForUnity/” folder.");
  37. }
  38. else
  39. {
  40. cascade = new CascadeClassifier(result);
  41. }
  42. Run ();
  43. },
  44. (result, progress) => {
  45. Debug.Log ("getFilePathAsync() progress : " + result + " " + Mathf.CeilToInt (progress * 100) + "%");
  46. });
  47. StartCoroutine (getFilePath_Coroutine);
  48. #else
  49. string cascade_filepath = Utils.getFilePath(HAAR_CASCADE_FILENAME);
  50. if (string.IsNullOrEmpty(cascade_filepath))
  51. {
  52. Debug.LogError(HAAR_CASCADE_FILENAME + " is not loaded. Please move from “OpenCVForUnity/StreamingAssets/OpenCVForUnity/” to “Assets/StreamingAssets/OpenCVForUnity/” folder.");
  53. }
  54. else
  55. {
  56. cascade = new CascadeClassifier(cascade_filepath);
  57. }
  58. Run();
  59. #endif
  60. }
  61. private void Run()
  62. {
  63. Texture2D imgTexture = Resources.Load("face") as Texture2D;
  64. Mat imgMat = new Mat(imgTexture.height, imgTexture.width, CvType.CV_8UC4);
  65. Utils.texture2DToMat(imgTexture, imgMat);
  66. Debug.Log("imgMat.ToString() " + imgMat.ToString());
  67. if (cascade == null)
  68. {
  69. Imgproc.putText(imgMat, "model file is not loaded.", new Point(5, imgMat.rows() - 30), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  70. Imgproc.putText(imgMat, "Please read console message.", new Point(5, imgMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.7, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  71. Texture2D _texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
  72. Utils.matToTexture2D(imgMat, _texture);
  73. gameObject.GetComponent<Renderer>().material.mainTexture = _texture;
  74. return;
  75. }
  76. Mat grayMat = new Mat();
  77. Imgproc.cvtColor(imgMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
  78. Imgproc.equalizeHist(grayMat, grayMat);
  79. MatOfRect faces = new MatOfRect();
  80. if (cascade != null)
  81. cascade.detectMultiScale(grayMat, faces, 1.1, 2, 2,
  82. new Size(20, 20), new Size());
  83. OpenCVForUnity.CoreModule.Rect[] rects = faces.toArray();
  84. for (int i = 0; i < rects.Length; i++)
  85. {
  86. Debug.Log("detect faces " + rects[i]);
  87. Imgproc.rectangle(imgMat, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(255, 0, 0, 255), 2);
  88. }
  89. Texture2D texture = new Texture2D(imgMat.cols(), imgMat.rows(), TextureFormat.RGBA32, false);
  90. Utils.matToTexture2D(imgMat, texture);
  91. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  92. }
  93. // Update is called once per frame
  94. void Update()
  95. {
  96. }
  97. /// <summary>
  98. /// Raises the destroy event.
  99. /// </summary>
  100. void OnDestroy()
  101. {
  102. #if UNITY_WEBGL
  103. if (getFilePath_Coroutine != null) {
  104. StopCoroutine (getFilePath_Coroutine);
  105. ((IDisposable)getFilePath_Coroutine).Dispose ();
  106. }
  107. #endif
  108. }
  109. /// <summary>
  110. /// Raises the back button click event.
  111. /// </summary>
  112. public void OnBackButtonClick()
  113. {
  114. SceneManager.LoadScene("OpenCVForUnityExample");
  115. }
  116. }
  117. }