TrackingExample.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.ImgprocModule;
  3. using OpenCVForUnity.TrackingModule;
  4. using OpenCVForUnity.UnityUtils;
  5. using OpenCVForUnity.UnityUtils.Helper;
  6. using OpenCVForUnity.VideoModule;
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using UnityEngine;
  11. using UnityEngine.EventSystems;
  12. using UnityEngine.SceneManagement;
  13. using UnityEngine.UI;
  14. using Rect = OpenCVForUnity.CoreModule.Rect;
  15. namespace OpenCVForUnityExample
  16. {
  17. /// <summary>
  18. /// Tracking Example
  19. /// An example of object tracking using the tracking (Tracking API) module.
  20. /// http://docs.opencv.org/trunk/d5/d07/tutorial_multitracker.html
  21. /// </summary>
  22. [RequireComponent(typeof(VideoCaptureToMatHelper))]
  23. public class TrackingExample : MonoBehaviour
  24. {
  25. /// <summary>
  26. /// The trackerKFC Toggle.
  27. /// </summary>
  28. public Toggle trackerKCFToggle;
  29. /// <summary>
  30. /// The trackerCSRT Toggle.
  31. /// </summary>
  32. public Toggle trackerCSRTToggle;
  33. /// <summary>
  34. /// The trackerMIL Toggle.
  35. /// </summary>
  36. public Toggle trackerMILToggle;
  37. /// <summary>
  38. /// The trackerGOTURN Toggle.
  39. /// </summary>
  40. public Toggle trackerGOTURNToggle;
  41. /// <summary>
  42. /// The trackerDaSiamRPN Toggle.
  43. /// </summary>
  44. public Toggle trackerDaSiamRPNToggle;
  45. /// <summary>
  46. /// The trackerNano Toggle.
  47. /// </summary>
  48. public Toggle trackerNanoToggle;
  49. /// <summary>
  50. /// GOTURN_MODELTXT_FILENAME
  51. /// </summary>
  52. protected static readonly string GOTURN_MODELTXT_FILENAME = "OpenCVForUnity/tracking/goturn.prototxt";
  53. /// <summary>
  54. /// The GOTURN modelTxt filepath.
  55. /// </summary>
  56. string GOTURN_modelTxt_filepath;
  57. /// <summary>
  58. /// GOTURN_MODELBIN_FILENAME
  59. /// </summary>
  60. protected static readonly string GOTURN_MODELBIN_FILENAME = "OpenCVForUnity/tracking/goturn.caffemodel";
  61. /// <summary>
  62. /// The GOTURN modelBin filepath.
  63. /// </summary>
  64. string GOTURN_modelBin_filepath;
  65. /// <summary>
  66. /// DaSiamRPN_MODEL_FILENAME
  67. /// </summary>
  68. protected static readonly string DaSiamRPN_MODEL_FILENAME = "OpenCVForUnity/tracking/dasiamrpn_model.onnx";
  69. /// <summary>
  70. /// The DaSiamRPN model filepath.
  71. /// </summary>
  72. string DaSiamRPN_model_filepath;
  73. /// <summary>
  74. /// DaSiamRPN_KERNEL_R1_FILENAME
  75. /// </summary>
  76. protected static readonly string DaSiamRPN_KERNEL_R1_FILENAME = "OpenCVForUnity/tracking/dasiamrpn_kernel_r1.onnx";
  77. /// <summary>
  78. /// The DaSiamRPN kernel_r1 filepath.
  79. /// </summary>
  80. string DaSiamRPN_kernel_r1_filepath;
  81. /// <summary>
  82. /// DaSiamRPN_KERNEL_CLS1_FILENAME
  83. /// </summary>
  84. protected static readonly string DaSiamRPN_KERNEL_CLS1_FILENAME = "OpenCVForUnity/tracking/dasiamrpn_kernel_cls1.onnx";
  85. /// <summary>
  86. /// The DaSiamRPN kernel_cls1 filepath.
  87. /// </summary>
  88. string DaSiamRPN_kernel_cls1_filepath;
  89. /// <summary>
  90. /// NANOTRACK_BACKBONE_SIM_FILENAME
  91. /// </summary>
  92. protected static readonly string NANOTRACK_BACKBONE_SIM_FILENAME = "OpenCVForUnity/tracking/nanotrack_backbone_sim.onnx";
  93. /// <summary>
  94. /// The NANOTRACK_backbone_sim filepath.
  95. /// </summary>
  96. string NANOTRACK_backbone_sim_filepath;
  97. /// <summary>
  98. /// NANOTRACK_HEAD_SIM_FILENAME
  99. /// </summary>
  100. protected static readonly string NANOTRACK_HEAD_SIM_FILENAME = "OpenCVForUnity/tracking/nanotrack_head_sim.onnx";
  101. /// <summary>
  102. /// The NANOTRACK_head_sim filepath.
  103. /// </summary>
  104. string NANOTRACK_head_sim_filepath;
  105. bool disableTrackerGOTURN = false;
  106. bool disableTrackerDaSiamRPN = false;
  107. bool disableTrackerNano = false;
  108. #if UNITY_WEBGL
  109. IEnumerator getFilePath_Coroutine;
  110. #endif
  111. /// <summary>
  112. /// The texture.
  113. /// </summary>
  114. Texture2D texture;
  115. /// <summary>
  116. /// The trackers.
  117. /// </summary>
  118. List<TrackerSetting> trackers;
  119. /// <summary>
  120. /// The selected point list.
  121. /// </summary>
  122. List<Point> selectedPointList;
  123. /// <summary>
  124. /// The stored touch point.
  125. /// </summary>
  126. Point storedTouchPoint;
  127. /// <summary>
  128. /// The video capture to mat helper.
  129. /// </summary>
  130. VideoCaptureToMatHelper sourceToMatHelper;
  131. /// <summary>
  132. /// The FPS monitor.
  133. /// </summary>
  134. FpsMonitor fpsMonitor;
  135. /// <summary>
  136. /// VIDEO_FILENAME
  137. /// </summary>
  138. protected static readonly string VIDEO_FILENAME = "OpenCVForUnity/768x576_mjpeg.mjpeg";
  139. // Use this for initialization
  140. void Start()
  141. {
  142. fpsMonitor = GetComponent<FpsMonitor>();
  143. sourceToMatHelper = gameObject.GetComponent<VideoCaptureToMatHelper>();
  144. #if UNITY_WSA_10_0
  145. // Disable the DNN module-dependent Tracker on UWP platforms, as it cannot be used.
  146. trackerGOTURNToggle.isOn = trackerGOTURNToggle.interactable = false;
  147. disableTrackerGOTURN = true;
  148. trackerDaSiamRPNToggle.isOn = trackerDaSiamRPNToggle.interactable = false;
  149. disableTrackerDaSiamRPN = true;
  150. trackerNanoToggle.isOn = trackerNanoToggle.interactable = false;
  151. disableTrackerNano = true;
  152. Run();
  153. #elif UNITY_WEBGL
  154. getFilePath_Coroutine = GetFilePath();
  155. StartCoroutine(getFilePath_Coroutine);
  156. #else
  157. GOTURN_modelTxt_filepath = Utils.getFilePath(GOTURN_MODELTXT_FILENAME);
  158. GOTURN_modelBin_filepath = Utils.getFilePath(GOTURN_MODELBIN_FILENAME);
  159. DaSiamRPN_model_filepath = Utils.getFilePath(DaSiamRPN_MODEL_FILENAME);
  160. DaSiamRPN_kernel_r1_filepath = Utils.getFilePath(DaSiamRPN_KERNEL_R1_FILENAME);
  161. DaSiamRPN_kernel_cls1_filepath = Utils.getFilePath(DaSiamRPN_KERNEL_CLS1_FILENAME);
  162. NANOTRACK_backbone_sim_filepath = Utils.getFilePath(NANOTRACK_BACKBONE_SIM_FILENAME);
  163. NANOTRACK_head_sim_filepath = Utils.getFilePath(NANOTRACK_HEAD_SIM_FILENAME);
  164. CheckFilePaths();
  165. Run();
  166. #endif
  167. }
  168. #if UNITY_WEBGL
  169. private IEnumerator GetFilePath()
  170. {
  171. var getFilePathAsync_0_Coroutine = Utils.getFilePathAsync(GOTURN_MODELTXT_FILENAME, (result) =>
  172. {
  173. GOTURN_modelTxt_filepath = result;
  174. });
  175. yield return getFilePathAsync_0_Coroutine;
  176. var getFilePathAsync_1_Coroutine = Utils.getFilePathAsync(GOTURN_MODELBIN_FILENAME, (result) =>
  177. {
  178. GOTURN_modelBin_filepath = result;
  179. });
  180. yield return getFilePathAsync_1_Coroutine;
  181. var getFilePathAsync_2_Coroutine = Utils.getFilePathAsync(DaSiamRPN_MODEL_FILENAME, (result) =>
  182. {
  183. DaSiamRPN_model_filepath = result;
  184. });
  185. yield return getFilePathAsync_2_Coroutine;
  186. var getFilePathAsync_3_Coroutine = Utils.getFilePathAsync(DaSiamRPN_KERNEL_R1_FILENAME, (result) =>
  187. {
  188. DaSiamRPN_kernel_r1_filepath = result;
  189. });
  190. yield return getFilePathAsync_3_Coroutine;
  191. var getFilePathAsync_4_Coroutine = Utils.getFilePathAsync(DaSiamRPN_KERNEL_CLS1_FILENAME, (result) =>
  192. {
  193. DaSiamRPN_kernel_cls1_filepath = result;
  194. });
  195. yield return getFilePathAsync_4_Coroutine;
  196. var getFilePathAsync_5_Coroutine = Utils.getFilePathAsync(NANOTRACK_BACKBONE_SIM_FILENAME, (result) =>
  197. {
  198. NANOTRACK_backbone_sim_filepath = result;
  199. });
  200. yield return getFilePathAsync_5_Coroutine;
  201. var getFilePathAsync_6_Coroutine = Utils.getFilePathAsync(NANOTRACK_HEAD_SIM_FILENAME, (result) =>
  202. {
  203. NANOTRACK_head_sim_filepath = result;
  204. });
  205. yield return getFilePathAsync_6_Coroutine;
  206. getFilePath_Coroutine = null;
  207. CheckFilePaths();
  208. Run();
  209. }
  210. #endif
  211. void CheckFilePaths()
  212. {
  213. if (string.IsNullOrEmpty(GOTURN_modelTxt_filepath) || string.IsNullOrEmpty(GOTURN_modelBin_filepath))
  214. {
  215. Debug.LogError(GOTURN_MODELTXT_FILENAME + " or " + GOTURN_MODELBIN_FILENAME + " is not loaded. Please read “StreamingAssets/OpenCVForUnity/tracking/setup_tracking_module.pdf” to make the necessary setup.");
  216. trackerGOTURNToggle.isOn = trackerGOTURNToggle.interactable = false;
  217. disableTrackerGOTURN = true;
  218. }
  219. if (string.IsNullOrEmpty(DaSiamRPN_model_filepath) || string.IsNullOrEmpty(DaSiamRPN_kernel_r1_filepath) || string.IsNullOrEmpty(DaSiamRPN_kernel_cls1_filepath))
  220. {
  221. Debug.LogError(DaSiamRPN_MODEL_FILENAME + " or " + DaSiamRPN_KERNEL_R1_FILENAME + " or " + DaSiamRPN_KERNEL_CLS1_FILENAME + " is not loaded. Please read “StreamingAssets/OpenCVForUnity/tracking/setup_tracking_module.pdf” to make the necessary setup.");
  222. trackerDaSiamRPNToggle.isOn = trackerDaSiamRPNToggle.interactable = false;
  223. disableTrackerDaSiamRPN = true;
  224. }
  225. if (string.IsNullOrEmpty(NANOTRACK_backbone_sim_filepath) || string.IsNullOrEmpty(NANOTRACK_head_sim_filepath))
  226. {
  227. Debug.LogError(NANOTRACK_BACKBONE_SIM_FILENAME + " or " + NANOTRACK_HEAD_SIM_FILENAME + " is not loaded. Please read “StreamingAssets/OpenCVForUnity/tracking/setup_tracking_module.pdf” to make the necessary setup.");
  228. trackerNanoToggle.isOn = trackerNanoToggle.interactable = false;
  229. disableTrackerNano = true;
  230. }
  231. }
  232. void Run()
  233. {
  234. if (string.IsNullOrEmpty(sourceToMatHelper.requestedVideoFilePath))
  235. sourceToMatHelper.requestedVideoFilePath = VIDEO_FILENAME;
  236. sourceToMatHelper.outputColorFormat = VideoCaptureToMatHelper.ColorFormat.RGB; // Tracking API must handle 3 channels Mat image.
  237. sourceToMatHelper.Initialize();
  238. }
  239. /// <summary>
  240. /// Raises the video capture to mat helper initialized event.
  241. /// </summary>
  242. public void OnVideoCaptureToMatHelperInitialized()
  243. {
  244. Debug.Log("OnVideoCaptureToMatHelperInitialized");
  245. Mat rgbMat = sourceToMatHelper.GetMat();
  246. texture = new Texture2D(rgbMat.cols(), rgbMat.rows(), TextureFormat.RGB24, false);
  247. Utils.matToTexture2D(rgbMat, texture);
  248. gameObject.GetComponent<Renderer>().material.mainTexture = texture;
  249. gameObject.transform.localScale = new Vector3(rgbMat.cols(), rgbMat.rows(), 1);
  250. Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
  251. float width = rgbMat.width();
  252. float height = rgbMat.height();
  253. float widthScale = (float)Screen.width / width;
  254. float heightScale = (float)Screen.height / height;
  255. if (widthScale < heightScale)
  256. {
  257. Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
  258. }
  259. else
  260. {
  261. Camera.main.orthographicSize = height / 2;
  262. }
  263. trackers = new List<TrackerSetting>();
  264. selectedPointList = new List<Point>();
  265. }
  266. /// <summary>
  267. /// Raises the video capture to mat helper disposed event.
  268. /// </summary>
  269. public void OnVideoCaptureToMatHelperDisposed()
  270. {
  271. Debug.Log("OnVideoCaptureToMatHelperDisposed");
  272. if (texture != null)
  273. {
  274. Texture2D.Destroy(texture);
  275. texture = null;
  276. }
  277. }
  278. /// <summary>
  279. /// Raises the video capture to mat helper error occurred event.
  280. /// </summary>
  281. /// <param name="errorCode">Error code.</param>
  282. public void OnVideoCaptureToMatHelperErrorOccurred(VideoCaptureToMatHelper.ErrorCode errorCode)
  283. {
  284. Debug.Log("OnVideoCaptureToMatHelperErrorOccurred " + errorCode);
  285. if (fpsMonitor != null)
  286. {
  287. fpsMonitor.consoleText = "ErrorCode: " + errorCode;
  288. }
  289. }
  290. // Update is called once per frame
  291. void Update()
  292. {
  293. if (!sourceToMatHelper.IsInitialized())
  294. return;
  295. #if ((UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR)
  296. //Touch
  297. int touchCount = Input.touchCount;
  298. if (touchCount == 1)
  299. {
  300. Touch t = Input.GetTouch(0);
  301. if(t.phase == TouchPhase.Ended && !EventSystem.current.IsPointerOverGameObject (t.fingerId)) {
  302. storedTouchPoint = new Point (t.position.x, t.position.y);
  303. //Debug.Log ("touch X " + t.position.x);
  304. //Debug.Log ("touch Y " + t.position.y);
  305. }
  306. }
  307. #else
  308. //Mouse
  309. if (Input.GetMouseButtonUp(0) && !EventSystem.current.IsPointerOverGameObject())
  310. {
  311. storedTouchPoint = new Point(Input.mousePosition.x, Input.mousePosition.y);
  312. //Debug.Log ("mouse X " + Input.mousePosition.x);
  313. //Debug.Log ("mouse Y " + Input.mousePosition.y);
  314. }
  315. #endif
  316. if (selectedPointList.Count != 1)
  317. {
  318. if (!sourceToMatHelper.IsPlaying())
  319. sourceToMatHelper.Play();
  320. if (sourceToMatHelper.IsPlaying() && sourceToMatHelper.DidUpdateThisFrame())
  321. {
  322. Mat rgbMat = sourceToMatHelper.GetMat();
  323. if (storedTouchPoint != null)
  324. {
  325. ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, texture.width, texture.height);
  326. OnTouch(storedTouchPoint, texture.width, texture.height);
  327. storedTouchPoint = null;
  328. }
  329. if (selectedPointList.Count == 1)
  330. {
  331. foreach (var point in selectedPointList)
  332. {
  333. Imgproc.circle(rgbMat, point, 6, new Scalar(0, 0, 255), 2);
  334. }
  335. }
  336. else if (selectedPointList.Count == 2)
  337. {
  338. ResetTrackers();
  339. using (MatOfPoint selectedPointMat = new MatOfPoint(selectedPointList.ToArray()))
  340. {
  341. Rect region = Imgproc.boundingRect(selectedPointMat);
  342. // init trackers.
  343. if (trackerKCFToggle.isOn)
  344. {
  345. TrackerKCF trackerKCF = TrackerKCF.create(new TrackerKCF_Params());
  346. trackerKCF.init(rgbMat, region);
  347. trackers.Add(new TrackerSetting(trackerKCF, trackerKCF.GetType().Name.ToString(), new Scalar(255, 0, 0)));
  348. }
  349. if (trackerCSRTToggle.isOn)
  350. {
  351. TrackerCSRT trackerCSRT = TrackerCSRT.create(new TrackerCSRT_Params());
  352. trackerCSRT.init(rgbMat, region);
  353. trackers.Add(new TrackerSetting(trackerCSRT, trackerCSRT.GetType().Name.ToString(), new Scalar(0, 255, 0)));
  354. }
  355. if (trackerMILToggle.isOn)
  356. {
  357. TrackerMIL trackerMIL = TrackerMIL.create(new TrackerMIL_Params());
  358. trackerMIL.init(rgbMat, region);
  359. trackers.Add(new TrackerSetting(trackerMIL, trackerMIL.GetType().Name.ToString(), new Scalar(0, 0, 255)));
  360. }
  361. if (!disableTrackerGOTURN && trackerGOTURNToggle.isOn)
  362. {
  363. var _params = new TrackerGOTURN_Params();
  364. _params.set_modelTxt(GOTURN_modelTxt_filepath);
  365. _params.set_modelBin(GOTURN_modelBin_filepath);
  366. TrackerGOTURN trackerGOTURN = TrackerGOTURN.create(_params);
  367. trackerGOTURN.init(rgbMat, region);
  368. trackers.Add(new TrackerSetting(trackerGOTURN, trackerGOTURN.GetType().Name.ToString(), new Scalar(255, 255, 0)));
  369. }
  370. if (!disableTrackerDaSiamRPN && trackerDaSiamRPNToggle.isOn)
  371. {
  372. var _params = new TrackerDaSiamRPN_Params();
  373. _params.set_model(DaSiamRPN_model_filepath);
  374. _params.set_kernel_r1(DaSiamRPN_kernel_r1_filepath);
  375. _params.set_kernel_cls1(DaSiamRPN_kernel_cls1_filepath);
  376. TrackerDaSiamRPN trackerDaSiamRPN = TrackerDaSiamRPN.create(_params);
  377. trackerDaSiamRPN.init(rgbMat, region);
  378. trackers.Add(new TrackerSetting(trackerDaSiamRPN, trackerDaSiamRPN.GetType().Name.ToString(), new Scalar(255, 0, 255)));
  379. }
  380. if (!disableTrackerNano && trackerNanoToggle.isOn)
  381. {
  382. var _params = new TrackerNano_Params();
  383. _params.set_backbone(NANOTRACK_backbone_sim_filepath);
  384. _params.set_neckhead(NANOTRACK_head_sim_filepath);
  385. TrackerNano trackerNano = TrackerNano.create(_params);
  386. trackerNano.init(rgbMat, region);
  387. trackers.Add(new TrackerSetting(trackerNano, trackerNano.GetType().Name.ToString(), new Scalar(0, 255, 255)));
  388. }
  389. }
  390. selectedPointList.Clear();
  391. if (trackers.Count > 0)
  392. {
  393. if (fpsMonitor != null)
  394. {
  395. fpsMonitor.consoleText = "";
  396. }
  397. trackerKCFToggle.interactable = trackerCSRTToggle.interactable = trackerMILToggle.interactable = false;
  398. if (!disableTrackerGOTURN)
  399. trackerGOTURNToggle.interactable = false;
  400. if (!disableTrackerDaSiamRPN)
  401. trackerDaSiamRPNToggle.interactable = false;
  402. if (!disableTrackerNano)
  403. trackerNanoToggle.interactable = false;
  404. }
  405. }
  406. // update trackers.
  407. for (int i = 0; i < trackers.Count; i++)
  408. {
  409. Tracker tracker = trackers[i].tracker;
  410. string label = trackers[i].label;
  411. Scalar lineColor = trackers[i].lineColor;
  412. Rect boundingBox = trackers[i].boundingBox;
  413. tracker.update(rgbMat, boundingBox);
  414. Imgproc.rectangle(rgbMat, boundingBox.tl(), boundingBox.br(), lineColor, 2, 1, 0);
  415. Imgproc.putText(rgbMat, label, new Point(boundingBox.x, boundingBox.y - 5), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, lineColor, 1, Imgproc.LINE_AA, false);
  416. }
  417. if (trackers.Count == 0)
  418. {
  419. if (selectedPointList.Count != 1)
  420. {
  421. //Imgproc.putText (rgbMat, "Please touch the screen, and select tracking regions.", new Point (5, rgbMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  422. if (fpsMonitor != null)
  423. {
  424. fpsMonitor.consoleText = "Please touch the screen, and select tracking regions.";
  425. }
  426. }
  427. else
  428. {
  429. //Imgproc.putText (rgbMat, "Please select the end point of the new tracking region.", new Point (5, rgbMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.8, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
  430. if (fpsMonitor != null)
  431. {
  432. fpsMonitor.consoleText = "Please select the end point of the new tracking region.";
  433. }
  434. }
  435. }
  436. Utils.matToTexture2D(rgbMat, texture);
  437. }
  438. }
  439. else
  440. {
  441. if (sourceToMatHelper.IsPlaying())
  442. sourceToMatHelper.Pause();
  443. if (storedTouchPoint != null)
  444. {
  445. ConvertScreenPointToTexturePoint(storedTouchPoint, storedTouchPoint, gameObject, texture.width, texture.height);
  446. OnTouch(storedTouchPoint, texture.width, texture.height);
  447. storedTouchPoint = null;
  448. }
  449. }
  450. }
  451. private void ResetTrackers()
  452. {
  453. if (trackers != null)
  454. {
  455. foreach (var t in trackers)
  456. {
  457. t.Dispose();
  458. }
  459. trackers.Clear();
  460. }
  461. trackerKCFToggle.interactable = trackerCSRTToggle.interactable = trackerMILToggle.interactable = true;
  462. if (!disableTrackerGOTURN)
  463. trackerGOTURNToggle.interactable = true;
  464. if (!disableTrackerDaSiamRPN)
  465. trackerDaSiamRPNToggle.interactable = true;
  466. if (!disableTrackerNano)
  467. trackerNanoToggle.interactable = true;
  468. }
  469. private void OnTouch(Point touchPoint, int textureWidth = -1, int textureHeight = -1)
  470. {
  471. if (selectedPointList.Count < 2)
  472. {
  473. selectedPointList.Add(touchPoint);
  474. if (!(new OpenCVForUnity.CoreModule.Rect(0, 0, textureWidth, textureHeight).contains(selectedPointList[selectedPointList.Count - 1])))
  475. {
  476. selectedPointList.RemoveAt(selectedPointList.Count - 1);
  477. }
  478. }
  479. }
  480. /// <summary>
  481. /// Converts the screen point to texture point.
  482. /// </summary>
  483. /// <param name="screenPoint">Screen point.</param>
  484. /// <param name="dstPoint">Dst point.</param>
  485. /// <param name="texturQuad">Texture quad.</param>
  486. /// <param name="textureWidth">Texture width.</param>
  487. /// <param name="textureHeight">Texture height.</param>
  488. /// <param name="camera">Camera.</param>
  489. private void ConvertScreenPointToTexturePoint(Point screenPoint, Point dstPoint, GameObject textureQuad, int textureWidth = -1, int textureHeight = -1, Camera camera = null)
  490. {
  491. if (textureWidth < 0 || textureHeight < 0)
  492. {
  493. Renderer r = textureQuad.GetComponent<Renderer>();
  494. if (r != null && r.material != null && r.material.mainTexture != null)
  495. {
  496. textureWidth = r.material.mainTexture.width;
  497. textureHeight = r.material.mainTexture.height;
  498. }
  499. else
  500. {
  501. textureWidth = (int)textureQuad.transform.localScale.x;
  502. textureHeight = (int)textureQuad.transform.localScale.y;
  503. }
  504. }
  505. if (camera == null)
  506. camera = Camera.main;
  507. Vector3 quadPosition = textureQuad.transform.localPosition;
  508. Vector3 quadScale = textureQuad.transform.localScale;
  509. Vector2 tl = camera.WorldToScreenPoint(new Vector3(quadPosition.x - quadScale.x / 2, quadPosition.y + quadScale.y / 2, quadPosition.z));
  510. Vector2 tr = camera.WorldToScreenPoint(new Vector3(quadPosition.x + quadScale.x / 2, quadPosition.y + quadScale.y / 2, quadPosition.z));
  511. Vector2 br = camera.WorldToScreenPoint(new Vector3(quadPosition.x + quadScale.x / 2, quadPosition.y - quadScale.y / 2, quadPosition.z));
  512. Vector2 bl = camera.WorldToScreenPoint(new Vector3(quadPosition.x - quadScale.x / 2, quadPosition.y - quadScale.y / 2, quadPosition.z));
  513. using (Mat srcRectMat = new Mat(4, 1, CvType.CV_32FC2))
  514. using (Mat dstRectMat = new Mat(4, 1, CvType.CV_32FC2))
  515. {
  516. srcRectMat.put(0, 0, tl.x, tl.y, tr.x, tr.y, br.x, br.y, bl.x, bl.y);
  517. dstRectMat.put(0, 0, 0, 0, quadScale.x, 0, quadScale.x, quadScale.y, 0, quadScale.y);
  518. using (Mat perspectiveTransform = Imgproc.getPerspectiveTransform(srcRectMat, dstRectMat))
  519. using (MatOfPoint2f srcPointMat = new MatOfPoint2f(screenPoint))
  520. using (MatOfPoint2f dstPointMat = new MatOfPoint2f())
  521. {
  522. Core.perspectiveTransform(srcPointMat, dstPointMat, perspectiveTransform);
  523. dstPoint.x = dstPointMat.get(0, 0)[0] * textureWidth / quadScale.x;
  524. dstPoint.y = dstPointMat.get(0, 0)[1] * textureHeight / quadScale.y;
  525. }
  526. }
  527. }
  528. /// <summary>
  529. /// Raises the disable event.
  530. /// </summary>
  531. void OnDisable()
  532. {
  533. #if UNITY_WEBGL
  534. if (getFilePath_Coroutine != null)
  535. {
  536. StopCoroutine(getFilePath_Coroutine);
  537. ((IDisposable)getFilePath_Coroutine).Dispose();
  538. }
  539. #endif
  540. }
  541. /// <summary>
  542. /// Raises the destroy event.
  543. /// </summary>
  544. void OnDestroy()
  545. {
  546. if (sourceToMatHelper != null)
  547. sourceToMatHelper.Dispose();
  548. }
  549. /// <summary>
  550. /// Raises the back button click event.
  551. /// </summary>
  552. public void OnBackButtonClick()
  553. {
  554. SceneManager.LoadScene("OpenCVForUnityExample");
  555. }
  556. /// <summary>
  557. /// Raises the reset trackers button click event.
  558. /// </summary>
  559. public void OnResetTrackersButtonClick()
  560. {
  561. ResetTrackers();
  562. selectedPointList.Clear();
  563. }
  564. class TrackerSetting
  565. {
  566. public Tracker tracker;
  567. public string label;
  568. public Scalar lineColor;
  569. public Rect boundingBox;
  570. public TrackerSetting(Tracker tracker, string label, Scalar lineColor)
  571. {
  572. this.tracker = tracker;
  573. this.label = label;
  574. this.lineColor = lineColor;
  575. this.boundingBox = new Rect();
  576. }
  577. public void Dispose()
  578. {
  579. if (tracker != null)
  580. {
  581. tracker.Dispose();
  582. tracker = null;
  583. }
  584. }
  585. }
  586. }
  587. }