MatToTextureInRenderThreadExample.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.SceneManagement;
  4. using System;
  5. using System.Collections;
  6. using OpenCVForUnity.CoreModule;
  7. using OpenCVForUnity.UnityUtils;
  8. using OpenCVForUnity.UnityUtils.Helper;
  9. using OpenCVForUnity.ImgprocModule;
  10. namespace OpenCVForUnityExample
  11. {
  12. /// <summary>
  13. /// Mat To Texture In RenderThread Example
  14. /// </summary>
  15. [RequireComponent (typeof(WebCamTextureToMatHelper))]
  16. public class MatToTextureInRenderThreadExample : MonoBehaviour
  17. {
  18. /// <summary>
  19. /// The requested resolution dropdown.
  20. /// </summary>
  21. public Dropdown requestedResolutionDropdown;
  22. /// <summary>
  23. /// The requested resolution.
  24. /// </summary>
  25. public ResolutionPreset requestedResolution = ResolutionPreset._640x480;
  26. /// <summary>
  27. /// The requestedFPS dropdown.
  28. /// </summary>
  29. public Dropdown requestedFPSDropdown;
  30. /// <summary>
  31. /// The requestedFPS.
  32. /// </summary>
  33. public FPSPreset requestedFPS = FPSPreset._30;
  34. /// <summary>
  35. /// The rotate 90 degree toggle.
  36. /// </summary>
  37. public Toggle rotate90DegreeToggle;
  38. /// <summary>
  39. /// The flip vertical toggle.
  40. /// </summary>
  41. public Toggle flipVerticalToggle;
  42. /// <summary>
  43. /// The flip horizontal toggle.
  44. /// </summary>
  45. public Toggle flipHorizontalToggle;
  46. /// <summary>
  47. /// The texture.
  48. /// </summary>
  49. Texture2D texture;
  50. /// <summary>
  51. /// The webcam texture to mat helper.
  52. /// </summary>
  53. WebCamTextureToMatHelper webCamTextureToMatHelper;
  54. /// <summary>
  55. /// The FPS monitor.
  56. /// </summary>
  57. FpsMonitor fpsMonitor;
  58. /// <summary>
  59. /// The rgba mat.
  60. /// </summary>
  61. Mat rgbaMat;
  62. /// <summary>
  63. /// The render thread coroutine.
  64. /// </summary>
  65. IEnumerator renderThreadCoroutine;
  66. // Use this for initialization
  67. void Start ()
  68. {
  69. rgbaMat = new Mat(text.height, text.width, CvType.CV_8UC4, new Scalar(0, 0, 0, 255));
  70. Mat yuv = new Mat(text.height, text.width, CvType.CV_8UC4, new Scalar(0, 0, 0, 255));
  71. Utils.texture2DToMat(text, yuv);
  72. Imgproc.cvtColor(yuv, rgbaMat, Imgproc.COLOR_RGBA2BGRA);
  73. texture = new Texture2D(rgbaMat.cols(), rgbaMat.rows(), TextureFormat.BGRA32, false);
  74. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  75. renderThreadCoroutine = CallAtEndOfFrames();
  76. StartCoroutine(renderThreadCoroutine);
  77. return;
  78. Debug.Log(rgbaMat.width() + "");
  79. Debug.Log(rgbaMat.height() + "");
  80. Debug.Log(text.width + "");
  81. Debug.Log(text.height + "");
  82. #if UNITY_WEBGL && !UNITY_EDITOR
  83. Utils.registerWebGLPlugin();
  84. #endif
  85. renderThreadCoroutine = CallAtEndOfFrames ();
  86. fpsMonitor = GetComponent<FpsMonitor> ();
  87. webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper> ();
  88. int width, height;
  89. Dimensions (requestedResolution, out width, out height);
  90. webCamTextureToMatHelper.requestedWidth = width;
  91. webCamTextureToMatHelper.requestedHeight = height;
  92. webCamTextureToMatHelper.requestedFPS = (int)requestedFPS;
  93. webCamTextureToMatHelper.Initialize ();
  94. // Update GUI state
  95. requestedResolutionDropdown.value = (int)requestedResolution;
  96. string[] enumNames = System.Enum.GetNames (typeof(FPSPreset));
  97. int index = Array.IndexOf (enumNames, requestedFPS.ToString ());
  98. requestedFPSDropdown.value = index;
  99. rotate90DegreeToggle.isOn = webCamTextureToMatHelper.rotate90Degree;
  100. flipVerticalToggle.isOn = webCamTextureToMatHelper.flipVertical;
  101. flipHorizontalToggle.isOn = webCamTextureToMatHelper.flipHorizontal;
  102. }
  103. /// <summary>
  104. /// Raises the webcam texture to mat helper initialized event.
  105. /// </summary>
  106. public void OnWebCamTextureToMatHelperInitialized ()
  107. {
  108. Debug.Log ("OnWebCamTextureToMatHelperInitialized");
  109. Mat webCamTextureMat = webCamTextureToMatHelper.GetMat ();
  110. texture = new Texture2D (rgbaMat.cols (), rgbaMat.rows (), TextureFormat.RGBA32, false);
  111. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  112. gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1);
  113. Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  114. if (fpsMonitor != null) {
  115. fpsMonitor.Add ("deviceName", webCamTextureToMatHelper.GetDeviceName ().ToString ());
  116. fpsMonitor.Add ("width", webCamTextureToMatHelper.GetWidth ().ToString ());
  117. fpsMonitor.Add ("height", webCamTextureToMatHelper.GetHeight ().ToString ());
  118. fpsMonitor.Add ("videoRotationAngle", webCamTextureToMatHelper.GetWebCamTexture ().videoRotationAngle.ToString ());
  119. fpsMonitor.Add ("videoVerticallyMirrored", webCamTextureToMatHelper.GetWebCamTexture ().videoVerticallyMirrored.ToString ());
  120. fpsMonitor.Add ("isFrontFacing", webCamTextureToMatHelper.IsFrontFacing ().ToString ());
  121. fpsMonitor.Add ("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString ());
  122. fpsMonitor.Add ("flipVertical", webCamTextureToMatHelper.flipVertical.ToString ());
  123. fpsMonitor.Add ("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString ());
  124. fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
  125. }
  126. float width = webCamTextureMat.width ();
  127. float height = webCamTextureMat.height ();
  128. float widthScale = (float)Screen.width / width;
  129. float heightScale = (float)Screen.height / height;
  130. if (widthScale < heightScale) {
  131. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  132. } else {
  133. Camera.main.orthographicSize = height / 2;
  134. }
  135. StartCoroutine (renderThreadCoroutine);
  136. }
  137. /// <summary>
  138. /// Raises the webcam texture to mat helper disposed event.
  139. /// </summary>
  140. public void OnWebCamTextureToMatHelperDisposed ()
  141. {
  142. Debug.Log ("OnWebCamTextureToMatHelperDisposed");
  143. StopCoroutine (renderThreadCoroutine);
  144. rgbaMat = null;
  145. if (texture != null) {
  146. Texture2D.Destroy (texture);
  147. texture = null;
  148. }
  149. }
  150. /// <summary>
  151. /// Raises the webcam texture to mat helper error occurred event.
  152. /// </summary>
  153. /// <param name="errorCode">Error code.</param>
  154. public void OnWebCamTextureToMatHelperErrorOccurred (WebCamTextureToMatHelper.ErrorCode errorCode)
  155. {
  156. Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
  157. }
  158. public Texture2D text;
  159. // Update is called once per frame
  160. void Update ()
  161. {
  162. // if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) {
  163. //rgbaMat = webCamTextureToMatHelper.GetMat ();
  164. // Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  165. // Utils.fastMatToTexture2D (rgbaMat, texture);
  166. // }
  167. }
  168. /// <summary>
  169. /// Calls at end of frames.
  170. /// </summary>
  171. private IEnumerator CallAtEndOfFrames ()
  172. {
  173. while (true) {
  174. // Wait until all frame rendering is done
  175. yield return new WaitForEndOfFrame ();
  176. if (rgbaMat != null) {
  177. Utils.matToTextureInRenderThread (rgbaMat, texture);
  178. }
  179. }
  180. }
  181. /// <summary>
  182. /// Raises the destroy event.
  183. /// </summary>
  184. void OnDestroy ()
  185. {
  186. webCamTextureToMatHelper.Dispose ();
  187. }
  188. /// <summary>
  189. /// Raises the back button click event.
  190. /// </summary>
  191. public void OnBackButtonClick ()
  192. {
  193. SceneManager.LoadScene ("OpenCVForUnityExample");
  194. }
  195. /// <summary>
  196. /// Raises the play button click event.
  197. /// </summary>
  198. public void OnPlayButtonClick ()
  199. {
  200. webCamTextureToMatHelper.Play ();
  201. }
  202. /// <summary>
  203. /// Raises the pause button click event.
  204. /// </summary>
  205. public void OnPauseButtonClick ()
  206. {
  207. webCamTextureToMatHelper.Pause ();
  208. }
  209. /// <summary>
  210. /// Raises the stop button click event.
  211. /// </summary>
  212. public void OnStopButtonClick ()
  213. {
  214. webCamTextureToMatHelper.Stop ();
  215. }
  216. /// <summary>
  217. /// Raises the change camera button click event.
  218. /// </summary>
  219. public void OnChangeCameraButtonClick ()
  220. {
  221. webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing ();
  222. }
  223. /// <summary>
  224. /// Raises the requested resolution dropdown value changed event.
  225. /// </summary>
  226. public void OnRequestedResolutionDropdownValueChanged (int result)
  227. {
  228. if ((int)requestedResolution != result) {
  229. requestedResolution = (ResolutionPreset)result;
  230. int width, height;
  231. Dimensions (requestedResolution, out width, out height);
  232. webCamTextureToMatHelper.Initialize (width, height);
  233. }
  234. }
  235. /// <summary>
  236. /// Raises the requestedFPS dropdown value changed event.
  237. /// </summary>
  238. public void OnRequestedFPSDropdownValueChanged (int result)
  239. {
  240. string[] enumNames = Enum.GetNames (typeof(FPSPreset));
  241. int value = (int)System.Enum.Parse (typeof(FPSPreset), enumNames [result], true);
  242. if ((int)requestedFPS != value) {
  243. requestedFPS = (FPSPreset)value;
  244. webCamTextureToMatHelper.requestedFPS = (int)requestedFPS;
  245. }
  246. }
  247. /// <summary>
  248. /// Raises the rotate 90 degree toggle value changed event.
  249. /// </summary>
  250. public void OnRotate90DegreeToggleValueChanged ()
  251. {
  252. if (rotate90DegreeToggle.isOn != webCamTextureToMatHelper.rotate90Degree) {
  253. webCamTextureToMatHelper.rotate90Degree = rotate90DegreeToggle.isOn;
  254. }
  255. if (fpsMonitor != null)
  256. fpsMonitor.Add ("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString ());
  257. }
  258. /// <summary>
  259. /// Raises the flip vertical toggle value changed event.
  260. /// </summary>
  261. public void OnFlipVerticalToggleValueChanged ()
  262. {
  263. if (flipVerticalToggle.isOn != webCamTextureToMatHelper.flipVertical) {
  264. webCamTextureToMatHelper.flipVertical = flipVerticalToggle.isOn;
  265. }
  266. if (fpsMonitor != null)
  267. fpsMonitor.Add ("flipVertical", webCamTextureToMatHelper.flipVertical.ToString ());
  268. }
  269. /// <summary>
  270. /// Raises the flip horizontal toggle value changed event.
  271. /// </summary>
  272. public void OnFlipHorizontalToggleValueChanged ()
  273. {
  274. if (flipHorizontalToggle.isOn != webCamTextureToMatHelper.flipHorizontal) {
  275. webCamTextureToMatHelper.flipHorizontal = flipHorizontalToggle.isOn;
  276. }
  277. if (fpsMonitor != null)
  278. fpsMonitor.Add ("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString ());
  279. }
  280. public enum FPSPreset : int
  281. {
  282. _0 = 0,
  283. _1 = 1,
  284. _5 = 5,
  285. _10 = 10,
  286. _15 = 15,
  287. _30 = 30,
  288. _60 = 60,
  289. }
  290. public enum ResolutionPreset : byte
  291. {
  292. _50x50 = 0,
  293. _640x480,
  294. _1280x720,
  295. _1920x1080,
  296. _9999x9999,
  297. }
  298. private void Dimensions (ResolutionPreset preset, out int width, out int height)
  299. {
  300. switch (preset) {
  301. case ResolutionPreset._50x50:
  302. width = 50;
  303. height = 50;
  304. break;
  305. case ResolutionPreset._640x480:
  306. width = 640;
  307. height = 480;
  308. break;
  309. case ResolutionPreset._1280x720:
  310. width = 1280;
  311. height = 720;
  312. break;
  313. case ResolutionPreset._1920x1080:
  314. width = 1920;
  315. height = 1080;
  316. break;
  317. case ResolutionPreset._9999x9999:
  318. width = 9999;
  319. height = 9999;
  320. break;
  321. default:
  322. width = height = 0;
  323. break;
  324. }
  325. }
  326. }
  327. }