VideoCaptureExample.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.ImgprocModule;
  9. using OpenCVForUnity.UnityUtils;
  10. namespace OpenCVForUnityExample
  11. {
  12. /// <summary>
  13. /// VideoCapture Example
  14. /// An example of playing a video file using the VideoCapture class.
  15. /// http://docs.opencv.org/3.2.0/dd/d43/tutorial_py_video_display.html
  16. /// </summary>
  17. public class VideoCaptureExample : 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. /// Indicates whether the video frame needs updating.
  33. /// </summary>
  34. bool shouldUpdateVideoFrame = false;
  35. /// <summary>
  36. /// The prev frame tick count.
  37. /// </summary>
  38. long prevFrameTickCount;
  39. /// <summary>
  40. /// The current frame tick count.
  41. /// </summary>
  42. long currentFrameTickCount;
  43. /// <summary>
  44. /// The FPS monitor.
  45. /// </summary>
  46. FpsMonitor fpsMonitor;
  47. #if UNITY_WEBGL && !UNITY_EDITOR
  48. IEnumerator getFilePath_Coroutine;
  49. #endif
  50. // Use this for initialization
  51. void Start ()
  52. {
  53. fpsMonitor = GetComponent<FpsMonitor> ();
  54. capture = new VideoCapture ();
  55. #if UNITY_WEBGL && !UNITY_EDITOR
  56. getFilePath_Coroutine = Utils.getFilePathAsync("768x576_mjpeg.mjpeg", (result) => {
  57. getFilePath_Coroutine = null;
  58. capture.open (result);
  59. Initialize ();
  60. });
  61. StartCoroutine (getFilePath_Coroutine);
  62. #else
  63. capture.open (Utils.getFilePath ("768x576_mjpeg.mjpeg"));
  64. Initialize ();
  65. #endif
  66. }
  67. private void Initialize ()
  68. {
  69. rgbMat = new Mat ();
  70. if (!capture.isOpened ()) {
  71. Debug.LogError ("capture.isOpened() is false. Please copy from “OpenCVForUnity/StreamingAssets/” to “Assets/StreamingAssets/” folder. ");
  72. }
  73. Debug.Log ("CAP_PROP_FORMAT: " + capture.get (Videoio.CAP_PROP_FORMAT));
  74. Debug.Log ("CAP_PROP_POS_MSEC: " + capture.get (Videoio.CAP_PROP_POS_MSEC));
  75. Debug.Log ("CAP_PROP_POS_FRAMES: " + capture.get (Videoio.CAP_PROP_POS_FRAMES));
  76. Debug.Log ("CAP_PROP_POS_AVI_RATIO: " + capture.get (Videoio.CAP_PROP_POS_AVI_RATIO));
  77. Debug.Log ("CAP_PROP_FRAME_COUNT: " + capture.get (Videoio.CAP_PROP_FRAME_COUNT));
  78. Debug.Log ("CAP_PROP_FPS: " + capture.get (Videoio.CAP_PROP_FPS));
  79. Debug.Log ("CAP_PROP_FRAME_WIDTH: " + capture.get (Videoio.CAP_PROP_FRAME_WIDTH));
  80. Debug.Log ("CAP_PROP_FRAME_HEIGHT: " + capture.get (Videoio.CAP_PROP_FRAME_HEIGHT));
  81. double ext = capture.get (Videoio.CAP_PROP_FOURCC);
  82. Debug.Log ("CAP_PROP_FOURCC: " + (char)((int)ext & 0XFF) + (char)(((int)ext & 0XFF00) >> 8) + (char)(((int)ext & 0XFF0000) >> 16) + (char)(((int)ext & 0XFF000000) >> 24));
  83. if (fpsMonitor != null) {
  84. fpsMonitor.Add ("CAP_PROP_FORMAT", capture.get (Videoio.CAP_PROP_FORMAT).ToString ());
  85. fpsMonitor.Add ("CAP_PROP_POS_MSEC", capture.get (Videoio.CAP_PROP_POS_MSEC).ToString ());
  86. fpsMonitor.Add ("CAP_PROP_POS_FRAMES", capture.get (Videoio.CAP_PROP_POS_FRAMES).ToString ());
  87. fpsMonitor.Add ("CAP_PROP_POS_AVI_RATIO", capture.get (Videoio.CAP_PROP_POS_AVI_RATIO).ToString ());
  88. fpsMonitor.Add ("CAP_PROP_FRAME_COUNT", capture.get (Videoio.CAP_PROP_FRAME_COUNT).ToString ());
  89. fpsMonitor.Add ("CAP_PROP_FPS", capture.get (Videoio.CAP_PROP_FPS).ToString ());
  90. fpsMonitor.Add ("CAP_PROP_FRAME_WIDTH", capture.get (Videoio.CAP_PROP_FRAME_WIDTH).ToString ());
  91. fpsMonitor.Add ("CAP_PROP_FRAME_HEIGHT", capture.get (Videoio.CAP_PROP_FRAME_HEIGHT).ToString ());
  92. fpsMonitor.Add ("CAP_PROP_FOURCC", ((char)((int)ext & 0XFF) + (char)(((int)ext & 0XFF00) >> 8) + (char)(((int)ext & 0XFF0000) >> 16) + (char)(((int)ext & 0XFF000000) >> 24)).ToString ());
  93. fpsMonitor.Add ("STATE", "");
  94. }
  95. capture.grab ();
  96. capture.retrieve (rgbMat, 0);
  97. int frameWidth = rgbMat.cols ();
  98. int frameHeight = rgbMat.rows ();
  99. texture = new Texture2D (frameWidth, frameHeight, TextureFormat.RGB24, false);
  100. gameObject.transform.localScale = new Vector3 ((float)frameWidth, (float)frameHeight, 1);
  101. float widthScale = (float)Screen.width / (float)frameWidth;
  102. float heightScale = (float)Screen.height / (float)frameHeight;
  103. if (widthScale < heightScale) {
  104. Camera.main.orthographicSize = ((float)frameWidth * (float)Screen.height / (float)Screen.width) / 2;
  105. } else {
  106. Camera.main.orthographicSize = (float)frameHeight / 2;
  107. }
  108. capture.set (Videoio.CAP_PROP_POS_FRAMES, 0);
  109. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  110. StartCoroutine ("WaitFrameTime");
  111. }
  112. // Update is called once per frame
  113. void Update ()
  114. {
  115. if (shouldUpdateVideoFrame) {
  116. shouldUpdateVideoFrame = false;
  117. //Loop play
  118. if (capture.get (Videoio.CAP_PROP_POS_FRAMES) >= capture.get (Videoio.CAP_PROP_FRAME_COUNT))
  119. capture.set (Videoio.CAP_PROP_POS_FRAMES, 0);
  120. if (capture.grab ()) {
  121. capture.retrieve (rgbMat, 0);
  122. Imgproc.cvtColor (rgbMat, rgbMat, Imgproc.COLOR_BGR2RGB);
  123. if (fpsMonitor != null) {
  124. fpsMonitor.Add ("CAP_PROP_POS_MSEC", capture.get (Videoio.CAP_PROP_POS_MSEC).ToString ());
  125. fpsMonitor.Add ("CAP_PROP_POS_FRAMES", capture.get (Videoio.CAP_PROP_POS_FRAMES).ToString ());
  126. fpsMonitor.Add ("CAP_PROP_POS_AVI_RATIO", capture.get (Videoio.CAP_PROP_POS_AVI_RATIO).ToString ());
  127. fpsMonitor.Add ("CAP_PROP_FRAME_COUNT", capture.get (Videoio.CAP_PROP_FRAME_COUNT).ToString ());
  128. int msec = (int)Math.Round (1000.0 * (currentFrameTickCount - prevFrameTickCount) / Core.getTickFrequency ());
  129. int fps = (int)Math.Round (1000.0 / msec);
  130. fpsMonitor.Add ("STATE", msec + "ms " + " (" + fps + "fps)");
  131. }
  132. Utils.fastMatToTexture2D (rgbMat, texture);
  133. }
  134. }
  135. }
  136. private IEnumerator WaitFrameTime ()
  137. {
  138. double videoFPS = (capture.get (Videoio.CAP_PROP_FPS) <= 0) ? 10.0 : capture.get (Videoio.CAP_PROP_FPS);
  139. int frameTime_msec = (int)Math.Round (1000.0 / videoFPS);
  140. while (true) {
  141. shouldUpdateVideoFrame = true;
  142. prevFrameTickCount = currentFrameTickCount;
  143. currentFrameTickCount = Core.getTickCount ();
  144. yield return new WaitForSeconds (frameTime_msec / 1000f);
  145. }
  146. }
  147. /// <summary>
  148. /// Raises the destroy event.
  149. /// </summary>
  150. void OnDestroy ()
  151. {
  152. StopCoroutine ("WaitFrameTime");
  153. capture.release ();
  154. if (rgbMat != null)
  155. rgbMat.Dispose ();
  156. #if UNITY_WEBGL && !UNITY_EDITOR
  157. if (getFilePath_Coroutine != null) {
  158. StopCoroutine (getFilePath_Coroutine);
  159. ((IDisposable)getFilePath_Coroutine).Dispose ();
  160. }
  161. #endif
  162. }
  163. /// <summary>
  164. /// Raises the back button click event.
  165. /// </summary>
  166. public void OnBackButtonClick ()
  167. {
  168. SceneManager.LoadScene ("OpenCVForUnityExample");
  169. }
  170. }
  171. }