WebCamTextureToMatHelperExample.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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.Helper;
  8. using OpenCVForUnity.UnityUtils;
  9. using OpenCVForUnity.ImgprocModule;
  10. namespace OpenCVForUnityExample
  11. {
  12. /// <summary>
  13. /// WebCamTextureToMatHelper Example
  14. /// </summary>
  15. [RequireComponent (typeof(WebCamTextureToMatHelper))]
  16. public class WebCamTextureToMatHelperExample : 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. // Use this for initialization
  59. void Start ()
  60. {
  61. fpsMonitor = GetComponent<FpsMonitor> ();
  62. webCamTextureToMatHelper = gameObject.GetComponent<WebCamTextureToMatHelper> ();
  63. int width, height;
  64. Dimensions (requestedResolution, out width, out height);
  65. webCamTextureToMatHelper.requestedWidth = width;
  66. webCamTextureToMatHelper.requestedHeight = height;
  67. webCamTextureToMatHelper.requestedFPS = (int)requestedFPS;
  68. webCamTextureToMatHelper.Initialize ();
  69. // Update GUI state
  70. requestedResolutionDropdown.value = (int)requestedResolution;
  71. string[] enumNames = System.Enum.GetNames (typeof(FPSPreset));
  72. int index = Array.IndexOf (enumNames, requestedFPS.ToString ());
  73. requestedFPSDropdown.value = index;
  74. rotate90DegreeToggle.isOn = webCamTextureToMatHelper.rotate90Degree;
  75. flipVerticalToggle.isOn = webCamTextureToMatHelper.flipVertical;
  76. flipHorizontalToggle.isOn = webCamTextureToMatHelper.flipHorizontal;
  77. }
  78. /// <summary>
  79. /// Raises the webcam texture to mat helper initialized event.
  80. /// </summary>
  81. public void OnWebCamTextureToMatHelperInitialized ()
  82. {
  83. Debug.Log ("OnWebCamTextureToMatHelperInitialized");
  84. Mat webCamTextureMat = webCamTextureToMatHelper.GetMat ();
  85. texture = new Texture2D (webCamTextureMat.cols (), webCamTextureMat.rows (), TextureFormat.RGBA32, false);
  86. gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
  87. gameObject.transform.localScale = new Vector3 (webCamTextureMat.cols (), webCamTextureMat.rows (), 1);
  88. Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  89. if (fpsMonitor != null) {
  90. fpsMonitor.Add ("deviceName", webCamTextureToMatHelper.GetDeviceName ().ToString ());
  91. fpsMonitor.Add ("width", webCamTextureToMatHelper.GetWidth ().ToString ());
  92. fpsMonitor.Add ("height", webCamTextureToMatHelper.GetHeight ().ToString ());
  93. fpsMonitor.Add ("videoRotationAngle", webCamTextureToMatHelper.GetWebCamTexture ().videoRotationAngle.ToString ());
  94. fpsMonitor.Add ("videoVerticallyMirrored", webCamTextureToMatHelper.GetWebCamTexture ().videoVerticallyMirrored.ToString ());
  95. fpsMonitor.Add ("camera fps", webCamTextureToMatHelper.GetFPS ().ToString ());
  96. fpsMonitor.Add ("isFrontFacing", webCamTextureToMatHelper.IsFrontFacing ().ToString ());
  97. fpsMonitor.Add ("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString ());
  98. fpsMonitor.Add ("flipVertical", webCamTextureToMatHelper.flipVertical.ToString ());
  99. fpsMonitor.Add ("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString ());
  100. fpsMonitor.Add ("orientation", Screen.orientation.ToString ());
  101. }
  102. float width = webCamTextureMat.width ();
  103. float height = webCamTextureMat.height ();
  104. float widthScale = (float)Screen.width / width;
  105. float heightScale = (float)Screen.height / height;
  106. if (widthScale < heightScale) {
  107. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  108. } else {
  109. Camera.main.orthographicSize = height / 2;
  110. }
  111. }
  112. /// <summary>
  113. /// Raises the webcam texture to mat helper disposed event.
  114. /// </summary>
  115. public void OnWebCamTextureToMatHelperDisposed ()
  116. {
  117. Debug.Log ("OnWebCamTextureToMatHelperDisposed");
  118. if (texture != null) {
  119. Texture2D.Destroy (texture);
  120. texture = null;
  121. }
  122. }
  123. /// <summary>
  124. /// Raises the webcam texture to mat helper error occurred event.
  125. /// </summary>
  126. /// <param name="errorCode">Error code.</param>
  127. public void OnWebCamTextureToMatHelperErrorOccurred (WebCamTextureToMatHelper.ErrorCode errorCode)
  128. {
  129. Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode);
  130. }
  131. // Update is called once per frame
  132. void Update ()
  133. {
  134. if (webCamTextureToMatHelper.IsPlaying () && webCamTextureToMatHelper.DidUpdateThisFrame ()) {
  135. Mat rgbaMat = webCamTextureToMatHelper.GetMat ();
  136. // 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);
  137. Utils.fastMatToTexture2D (rgbaMat, texture);
  138. }
  139. }
  140. /// <summary>
  141. /// Raises the destroy event.
  142. /// </summary>
  143. void OnDestroy ()
  144. {
  145. webCamTextureToMatHelper.Dispose ();
  146. }
  147. /// <summary>
  148. /// Raises the back button click event.
  149. /// </summary>
  150. public void OnBackButtonClick ()
  151. {
  152. SceneManager.LoadScene ("OpenCVForUnityExample");
  153. }
  154. /// <summary>
  155. /// Raises the play button click event.
  156. /// </summary>
  157. public void OnPlayButtonClick ()
  158. {
  159. webCamTextureToMatHelper.Play ();
  160. }
  161. /// <summary>
  162. /// Raises the pause button click event.
  163. /// </summary>
  164. public void OnPauseButtonClick ()
  165. {
  166. webCamTextureToMatHelper.Pause ();
  167. }
  168. /// <summary>
  169. /// Raises the stop button click event.
  170. /// </summary>
  171. public void OnStopButtonClick ()
  172. {
  173. webCamTextureToMatHelper.Stop ();
  174. }
  175. /// <summary>
  176. /// Raises the change camera button click event.
  177. /// </summary>
  178. public void OnChangeCameraButtonClick ()
  179. {
  180. webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.IsFrontFacing ();
  181. }
  182. /// <summary>
  183. /// Raises the requested resolution dropdown value changed event.
  184. /// </summary>
  185. public void OnRequestedResolutionDropdownValueChanged (int result)
  186. {
  187. if ((int)requestedResolution != result) {
  188. requestedResolution = (ResolutionPreset)result;
  189. int width, height;
  190. Dimensions (requestedResolution, out width, out height);
  191. webCamTextureToMatHelper.Initialize (width, height);
  192. }
  193. }
  194. /// <summary>
  195. /// Raises the requestedFPS dropdown value changed event.
  196. /// </summary>
  197. public void OnRequestedFPSDropdownValueChanged (int result)
  198. {
  199. string[] enumNames = Enum.GetNames (typeof(FPSPreset));
  200. int value = (int)System.Enum.Parse (typeof(FPSPreset), enumNames [result], true);
  201. if ((int)requestedFPS != value) {
  202. requestedFPS = (FPSPreset)value;
  203. webCamTextureToMatHelper.requestedFPS = (int)requestedFPS;
  204. }
  205. }
  206. /// <summary>
  207. /// Raises the rotate 90 degree toggle value changed event.
  208. /// </summary>
  209. public void OnRotate90DegreeToggleValueChanged ()
  210. {
  211. if (rotate90DegreeToggle.isOn != webCamTextureToMatHelper.rotate90Degree) {
  212. webCamTextureToMatHelper.rotate90Degree = rotate90DegreeToggle.isOn;
  213. }
  214. if (fpsMonitor != null)
  215. fpsMonitor.Add ("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString ());
  216. }
  217. /// <summary>
  218. /// Raises the flip vertical toggle value changed event.
  219. /// </summary>
  220. public void OnFlipVerticalToggleValueChanged ()
  221. {
  222. if (flipVerticalToggle.isOn != webCamTextureToMatHelper.flipVertical) {
  223. webCamTextureToMatHelper.flipVertical = flipVerticalToggle.isOn;
  224. }
  225. if (fpsMonitor != null)
  226. fpsMonitor.Add ("flipVertical", webCamTextureToMatHelper.flipVertical.ToString ());
  227. }
  228. /// <summary>
  229. /// Raises the flip horizontal toggle value changed event.
  230. /// </summary>
  231. public void OnFlipHorizontalToggleValueChanged ()
  232. {
  233. if (flipHorizontalToggle.isOn != webCamTextureToMatHelper.flipHorizontal) {
  234. webCamTextureToMatHelper.flipHorizontal = flipHorizontalToggle.isOn;
  235. }
  236. if (fpsMonitor != null)
  237. fpsMonitor.Add ("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString ());
  238. }
  239. public enum FPSPreset : int
  240. {
  241. _0 = 0,
  242. _1 = 1,
  243. _5 = 5,
  244. _10 = 10,
  245. _15 = 15,
  246. _30 = 30,
  247. _60 = 60,
  248. }
  249. public enum ResolutionPreset : byte
  250. {
  251. _50x50 = 0,
  252. _640x480,
  253. _1280x720,
  254. _1920x1080,
  255. _9999x9999,
  256. }
  257. private void Dimensions (ResolutionPreset preset, out int width, out int height)
  258. {
  259. switch (preset) {
  260. case ResolutionPreset._50x50:
  261. width = 50;
  262. height = 50;
  263. break;
  264. case ResolutionPreset._640x480:
  265. width = 640;
  266. height = 480;
  267. break;
  268. case ResolutionPreset._1280x720:
  269. width = 1280;
  270. height = 720;
  271. break;
  272. case ResolutionPreset._1920x1080:
  273. width = 1920;
  274. height = 1080;
  275. break;
  276. case ResolutionPreset._9999x9999:
  277. width = 9999;
  278. height = 9999;
  279. break;
  280. default:
  281. width = height = 0;
  282. break;
  283. }
  284. }
  285. }
  286. }