RecognizeScript.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Nxr.Internal
  5. {
  6. public class RecognizeScript : MonoBehaviour
  7. {
  8. public GameObject cameraPreviewPanel;
  9. public TextMesh statusText;
  10. Transform cameraPreviewPanelTransform;
  11. private Vector2 previewPanelTL;
  12. private Vector2 previewPanelSize;
  13. CameraPreviewScript cameraPreviewScript;
  14. RecoginizeApi recoginizeApi;
  15. Transform cacheTransform;
  16. private bool isRecognizing = false;
  17. // Use this for initialization
  18. void Start()
  19. {
  20. cacheTransform = transform;
  21. recoginizeApi = RecoginizeApi.Instance;
  22. recoginizeApi.Init();
  23. cameraPreviewScript = cameraPreviewPanel.GetComponent<CameraPreviewScript>();
  24. cameraPreviewScript.AutoCameraFocus = false;
  25. cameraPreviewPanelTransform = cameraPreviewPanel.transform;
  26. previewPanelTL.x = cameraPreviewPanelTransform.localPosition.x - cameraPreviewPanelTransform.localScale.x / 2;
  27. previewPanelTL.y = cameraPreviewPanelTransform.localPosition.y + cameraPreviewPanelTransform.localScale.y / 2;
  28. previewPanelSize.x = cameraPreviewPanelTransform.localScale.x;
  29. previewPanelSize.y = cameraPreviewPanelTransform.localScale.y;
  30. // Debug.LogError(previewPanelTL.ToString());
  31. // 隐藏白点
  32. NxrViewer.Instance.GazeApi(GazeTag.Hide);
  33. if (statusText != null)
  34. {
  35. statusText.text = "Loading...";
  36. }
  37. }
  38. public bool AddTestFrame;
  39. private void test()
  40. {
  41. //for test // id=0,title=keyboard,confidence=0.64,location.(x:434.00, y:11.58, width:204.65, height:199.89), width=640, height=480
  42. List<Recognition> testlist = new List<Recognition>();
  43. Recognition recognition = new Recognition();
  44. recognition.id = "0";
  45. recognition.title = "keyboard";
  46. recognition.confidence = 0.94f;
  47. recognition.frameWidth = 640;
  48. recognition.frameHeight = 480;
  49. recognition.location = new Rect(434, 11.58f, 204.63f, 199.89f);
  50. testlist.Add(recognition);
  51. OnRecognizeSuccess(testlist);
  52. // for test
  53. }
  54. // Update is called once per frame
  55. void Update()
  56. {
  57. if(AddTestFrame)
  58. {
  59. AddTestFrame = false;
  60. test();
  61. }
  62. if (!isRecognizing && cameraPreviewScript.IsTextureUpdated())
  63. {
  64. //
  65. isRecognizing = true;
  66. ClearRecognitionFrame();
  67. recoginizeApi.StartRecognize(OnRecognizeSuccess, OnRecognizeFailed);
  68. if (statusText != null)
  69. {
  70. statusText.text = "Start recognizing...";
  71. }
  72. }
  73. }
  74. List<GameObject> frameGoList = new List<GameObject>(5);
  75. void OnRecognizeSuccess(List<Recognition> dataList)
  76. {
  77. ClearRecognitionFrame();
  78. if (!isRecognizing) return;
  79. for (int i = 0, size = dataList.Count; i < size; i++)
  80. {
  81. Recognition recog = dataList[i];
  82. float offsetX = recog.location.x / recog.frameWidth;
  83. float offsetY = recog.location.y / recog.frameHeight;
  84. float scaleX = recog.location.width / recog.frameWidth;
  85. float scaleY = recog.location.height / recog.frameHeight;
  86. float newScaleX = scaleX * previewPanelSize.x;
  87. float newScaleY = scaleY * previewPanelSize.y;
  88. float newCenterX = previewPanelTL.x + offsetX * previewPanelSize.x + newScaleX / 2;
  89. float newCenterY = previewPanelTL.y - offsetY * previewPanelSize.y - newScaleY / 2;
  90. //Debug.Log("Recognition==>" + recog.location.width + "/" + recog.frameWidth + "," +
  91. // +previewPanelSize.x + "," + newCenterX + ","
  92. // + offsetX + "," + newScaleX + "," + previewPanelTL.x);
  93. //recog.PrintInfo();
  94. if(cacheTransform == null)
  95. {
  96. return;
  97. }
  98. GameObject frameGo = Instantiate(Resources.Load("Prefabs/RecognitionFrame")) as GameObject;
  99. frameGo.transform.SetParent(cacheTransform);
  100. frameGo.transform.localPosition = new Vector3(newCenterX, newCenterY, cameraPreviewPanelTransform.localPosition.z - 0.05f * i);
  101. frameGo.transform.localRotation = Quaternion.identity;
  102. frameGo.transform.Find("Frame").localScale = new Vector3(newScaleX, newScaleY, 1);
  103. Transform descTransform = frameGo.transform.Find("Desc");
  104. descTransform.localPosition = new Vector3(0.2f - newScaleX / 2, newScaleY / 2 - 0.2f, 0);
  105. TextMesh descTextMesh = descTransform.GetComponent<TextMesh>();
  106. descTextMesh.text = recog.title + "(" + recog.confidence + ")";
  107. frameGoList.Add(frameGo);
  108. }
  109. }
  110. void ClearRecognitionFrame()
  111. {
  112. for (int i = 0, size = frameGoList.Count; i < size; i++)
  113. {
  114. Destroy(frameGoList[i]);
  115. }
  116. frameGoList.Clear();
  117. }
  118. void OnRecognizeFailed(string message)
  119. {
  120. ClearRecognitionFrame();
  121. if (statusText != null)
  122. {
  123. statusText.text = "Recognize failed..." + message;
  124. }
  125. }
  126. private void OnApplicationPause(bool pause)
  127. {
  128. if (pause)
  129. {
  130. recoginizeApi.StopRecognize();
  131. }
  132. else
  133. {
  134. isRecognizing = false;
  135. }
  136. }
  137. private void OnApplicationQuit()
  138. {
  139. ClearRecognitionFrame();
  140. recoginizeApi.OnDestroy();
  141. }
  142. private void OnDestroy()
  143. {
  144. ClearRecognitionFrame();
  145. recoginizeApi.OnDestroy();
  146. Debug.Log("RecognizeScript.OnDestroy");
  147. }
  148. }
  149. }