FaceDetectionExample.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #if UNITY_WEBGL && !UNITY_EDITOR
  21. IEnumerator getFilePath_Coroutine;
  22. #endif
  23. // Use this for initialization
  24. void Start ()
  25. {
  26. #if UNITY_WEBGL && !UNITY_EDITOR
  27. getFilePath_Coroutine = Utils.getFilePathAsync("haarcascade_frontalface_alt.xml",
  28. (result) => {
  29. getFilePath_Coroutine = null;
  30. cascade = new CascadeClassifier ();
  31. cascade.load(result);
  32. if (cascade.empty ()) {
  33. Debug.LogError ("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  34. }
  35. Run ();
  36. },
  37. (result, progress) => {
  38. Debug.Log ("getFilePathAsync() progress : " + result + " " + Mathf.CeilToInt (progress * 100) + "%");
  39. });
  40. StartCoroutine (getFilePath_Coroutine);
  41. #else
  42. //cascade = new CascadeClassifier (Utils.getFilePath ("lbpcascade_frontalface.xml"));
  43. cascade = new CascadeClassifier ();
  44. cascade.load (Utils.getFilePath ("haarcascade_frontalface_alt.xml"));
  45. #if !UNITY_WSA_10_0
  46. if (cascade.empty ()) {
  47. Debug.LogError ("cascade file is not loaded. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  48. }
  49. #endif
  50. Run ();
  51. #endif
  52. }
  53. private void Run ()
  54. {
  55. Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
  56. Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC4);
  57. Utils.texture2DToMat (imgTexture, imgMat);
  58. Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
  59. Mat grayMat = new Mat ();
  60. Imgproc.cvtColor (imgMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
  61. Imgproc.equalizeHist (grayMat, grayMat);
  62. MatOfRect faces = new MatOfRect ();
  63. if (cascade != null)
  64. cascade.detectMultiScale (grayMat, faces, 1.1, 2, 2,
  65. new Size (20, 20), new Size ());
  66. OpenCVForUnity.CoreModule.Rect[] rects = faces.toArray ();
  67. for (int i = 0; i < rects.Length; i++) {
  68. Debug.Log ("detect faces " + rects [i]);
  69. 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);
  70. }
  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. }
  75. // Update is called once per frame
  76. void Update ()
  77. {
  78. }
  79. /// <summary>
  80. /// Raises the destroy event.
  81. /// </summary>
  82. void OnDestroy ()
  83. {
  84. #if UNITY_WEBGL && !UNITY_EDITOR
  85. if (getFilePath_Coroutine != null) {
  86. StopCoroutine (getFilePath_Coroutine);
  87. ((IDisposable)getFilePath_Coroutine).Dispose ();
  88. }
  89. #endif
  90. }
  91. /// <summary>
  92. /// Raises the back button click event.
  93. /// </summary>
  94. public void OnBackButtonClick ()
  95. {
  96. SceneManager.LoadScene ("OpenCVForUnityExample");
  97. }
  98. }
  99. }