HOGDescriptorExample.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.VideoioModule;
  8. using OpenCVForUnity.ObjdetectModule;
  9. using OpenCVForUnity.ImgprocModule;
  10. using OpenCVForUnity.UnityUtils;
  11. namespace OpenCVForUnityExample
  12. {
  13. /// <summary>
  14. /// HOGDescriptor Example
  15. /// An example of people detection using the HOGDescriptor class.
  16. /// </summary>
  17. public class HOGDescriptorExample : MonoBehaviour
  18. {
  19. /// <summary>
  20. /// The videoCapture.
  21. /// </summary>
  22. VideoCapture capture;
  23. /// <summary>
  24. /// The rgb mat.
  25. /// </summary>
  26. Mat rgbMat;
  27. /// <summary>
  28. /// The texture.
  29. /// </summary>
  30. Texture2D texture;
  31. /// <summary>
  32. /// The HOGDescriptor.
  33. /// </summary>
  34. HOGDescriptor des;
  35. #if UNITY_WEBGL && !UNITY_EDITOR
  36. IEnumerator getFilePath_Coroutine;
  37. #endif
  38. // Use this for initialization
  39. void Start ()
  40. {
  41. capture = new VideoCapture ();
  42. #if UNITY_WEBGL && !UNITY_EDITOR
  43. getFilePath_Coroutine = Utils.getFilePathAsync("768x576_mjpeg.mjpeg", (result) => {
  44. getFilePath_Coroutine = null;
  45. capture.open (result);
  46. Init();
  47. });
  48. StartCoroutine (getFilePath_Coroutine);
  49. #else
  50. capture.open (Utils.getFilePath ("768x576_mjpeg.mjpeg"));
  51. Init ();
  52. #endif
  53. }
  54. private void Init ()
  55. {
  56. rgbMat = new Mat ();
  57. if (!capture.isOpened ()) {
  58. Debug.LogError ("capture.isOpened() is true. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  59. }
  60. Debug.Log ("CAP_PROP_FORMAT: " + capture.get (Videoio.CAP_PROP_FORMAT));
  61. Debug.Log ("CAP_PROP_POS_MSEC: " + capture.get (Videoio.CAP_PROP_POS_MSEC));
  62. Debug.Log ("CAP_PROP_POS_FRAMES: " + capture.get (Videoio.CAP_PROP_POS_FRAMES));
  63. Debug.Log ("CAP_PROP_POS_AVI_RATIO: " + capture.get (Videoio.CAP_PROP_POS_AVI_RATIO));
  64. Debug.Log ("CAP_PROP_FRAME_COUNT: " + capture.get (Videoio.CAP_PROP_FRAME_COUNT));
  65. Debug.Log ("CAP_PROP_FPS: " + capture.get (Videoio.CAP_PROP_FPS));
  66. Debug.Log ("CAP_PROP_FRAME_WIDTH: " + capture.get (Videoio.CAP_PROP_FRAME_WIDTH));
  67. Debug.Log ("CAP_PROP_FRAME_HEIGHT: " + capture.get (Videoio.CAP_PROP_FRAME_HEIGHT));
  68. capture.grab ();
  69. capture.retrieve (rgbMat, 0);
  70. int frameWidth = rgbMat.cols ();
  71. int frameHeight = rgbMat.rows ();
  72. texture = new Texture2D (frameWidth, frameHeight, TextureFormat.RGB24, false);
  73. gameObject.transform.localScale = new Vector3 ((float)frameWidth, (float)frameHeight, 1);
  74. float widthScale = (float)Screen.width / (float)frameWidth;
  75. float heightScale = (float)Screen.height / (float)frameHeight;
  76. if (widthScale < heightScale) {
  77. Camera.main.orthographicSize = ((float)frameWidth * (float)Screen.height / (float)Screen.width) / 2;
  78. } else {
  79. Camera.main.orthographicSize = (float)frameHeight / 2;
  80. }
  81. capture.set (Videoio.CAP_PROP_POS_FRAMES, 0);
  82. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  83. des = new HOGDescriptor ();
  84. }
  85. // Update is called once per frame
  86. void Update ()
  87. {
  88. //Loop play
  89. if (capture.get (Videoio.CAP_PROP_POS_FRAMES) >= capture.get (Videoio.CAP_PROP_FRAME_COUNT))
  90. capture.set (Videoio.CAP_PROP_POS_FRAMES, 0);
  91. //error PlayerLoop called recursively! on iOS.reccomend WebCamTexture.
  92. if (capture.grab ()) {
  93. capture.retrieve (rgbMat, 0);
  94. Imgproc.cvtColor (rgbMat, rgbMat, Imgproc.COLOR_BGR2RGB);
  95. //Debug.Log ("Mat toString " + rgbMat.ToString ());
  96. using (MatOfRect locations = new MatOfRect ())
  97. using (MatOfDouble weights = new MatOfDouble ()) {
  98. des.setSVMDetector (HOGDescriptor.getDefaultPeopleDetector ());
  99. des.detectMultiScale (rgbMat, locations, weights);
  100. OpenCVForUnity.CoreModule.Rect[] rects = locations.toArray ();
  101. for (int i = 0; i < rects.Length; i++) {
  102. //Debug.Log ("detected person " + rects [i]);
  103. Imgproc.rectangle (rgbMat, 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), 2);
  104. }
  105. //Debug.Log (locations.ToString ());
  106. //Debug.Log (weights.ToString ());
  107. }
  108. Utils.fastMatToTexture2D (rgbMat, texture);
  109. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  110. }
  111. }
  112. /// <summary>
  113. /// Raises the destroy event.
  114. /// </summary>
  115. void OnDestroy ()
  116. {
  117. capture.release ();
  118. if (rgbMat != null)
  119. rgbMat.Dispose ();
  120. if (texture != null) {
  121. Texture2D.Destroy (texture);
  122. texture = null;
  123. }
  124. if (des != null)
  125. des.Dispose ();
  126. #if UNITY_WEBGL && !UNITY_EDITOR
  127. if (getFilePath_Coroutine != null) {
  128. StopCoroutine (getFilePath_Coroutine);
  129. ((IDisposable)getFilePath_Coroutine).Dispose ();
  130. }
  131. #endif
  132. }
  133. /// <summary>
  134. /// Raises the back button click event.
  135. /// </summary>
  136. public void OnBackButtonClick ()
  137. {
  138. SceneManager.LoadScene ("OpenCVForUnityExample");
  139. }
  140. }
  141. }