RecognizeApi.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Nxr.Internal
  5. {
  6. public class Recognition
  7. {
  8. public string id;
  9. public string title;
  10. public float confidence;
  11. public Rect location;
  12. public int frameWidth;
  13. public int frameHeight;
  14. public void PrintInfo()
  15. {
  16. Debug.Log("Recognition : id=" + id + ",title=" + title + ",confidence=" + confidence + ",location." + location.ToString()
  17. + ", width="+frameWidth+", height="+frameHeight);
  18. }
  19. }
  20. public class RecoginizeApi
  21. {
  22. public enum ErrorType
  23. {
  24. CAMERA_BUSY, MODEL_LOAD_ERROR, NOT_DECLARE_OBJECT_RECOGINIZE_PLUGIN_ID, NOT_SUPPORT_OBJECT_RECOGINIZE_PLUGIN_ID,
  25. SURFACETEXTURE_IS_NULL, BITMAP_CONFIG_ERROR
  26. }
  27. string managerClassName = "com.nibiru.tensorflow.core.NibiruTensorManager";
  28. string unityHelperClassName = "com.nibiru.lib.vr.NibiruUnityHelper";
  29. private static readonly object locker = new object();
  30. private RecoginizeApi()
  31. {
  32. destroyed = false;
  33. }
  34. private static RecoginizeApi _current;
  35. public static RecoginizeApi Instance
  36. {
  37. get
  38. {
  39. lock (locker)
  40. {
  41. if (_current == null)
  42. {
  43. _current = new RecoginizeApi();
  44. }
  45. return _current;
  46. }
  47. }
  48. }
  49. private AndroidJavaObject androidActivity;
  50. private AndroidJavaClass nibiruTensorManagerClass;
  51. private AndroidJavaObject nibiruTensorManager;
  52. public void Init()
  53. {
  54. #if UNITY_ANDROID && !UNITY_EDITOR
  55. try
  56. {
  57. using (AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
  58. {
  59. androidActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
  60. }
  61. }
  62. catch (AndroidJavaException e)
  63. {
  64. androidActivity = null;
  65. Debug.LogError("Exception while connecting to the Activity: " + e);
  66. return;
  67. }
  68. if(androidActivity != null)
  69. {
  70. AndroidJavaObject contextObject = androidActivity.Call<AndroidJavaObject>("getApplicationContext");
  71. nibiruTensorManagerClass = new AndroidJavaClass(managerClassName);
  72. nibiruTensorManager = nibiruTensorManagerClass.CallStatic<AndroidJavaObject>("getInstance");
  73. nibiruTensorManager.Call("init", contextObject, new NibiruVerifyListener(VerifyStatusCallback));
  74. }
  75. #endif
  76. Debug.Log("RecognizeApi init succ.");
  77. }
  78. public void StartRecognize(OnRecognizeSuccess succ, OnRecognizeFailed failed)
  79. {
  80. if (nibiruTensorManager != null)
  81. {
  82. AndroidJavaClass unityHelperClass = new AndroidJavaClass(unityHelperClassName);
  83. AndroidJavaObject unityHelperObject = unityHelperClass.CallStatic<AndroidJavaObject>("getInstance");
  84. if (unityHelperObject == null)
  85. {
  86. Debug.LogError("StartRecognize failed , UnityHelper.getInstance is null !!!");
  87. return;
  88. }
  89. int status = unityHelperObject.Call<int>("getStatus");
  90. if (status < 1)
  91. {
  92. Debug.LogError("StartRecognize failed , Must be after UnityHelper create SurfaceTexture !!! " + status);
  93. return;
  94. }
  95. AndroidJavaObject surfaceTextureObject = unityHelperObject.Call<AndroidJavaObject>("getSurfaceTexture");
  96. nibiruTensorManager.Call("start", surfaceTextureObject, new NibiruRecognizeCallback(this, succ, failed));
  97. stoped = false;
  98. destroyed = false;
  99. NibiruService nibiruService = NxrViewer.Instance.GetNibiruService();
  100. if(nibiruService != null)
  101. {
  102. nibiruService.SetCameraPreviewing(true);
  103. }
  104. Debug.Log("RecognizeApi start succ.");
  105. }
  106. }
  107. private bool stoped;
  108. public void StopRecognize()
  109. {
  110. if (nibiruTensorManager != null)
  111. {
  112. stoped = true;
  113. nibiruTensorManager.Call("stop");
  114. NibiruService nibiruService = NxrViewer.Instance.GetNibiruService();
  115. if (nibiruService != null)
  116. {
  117. nibiruService.SetCameraPreviewing(false);
  118. }
  119. }
  120. }
  121. private bool destroyed = false;
  122. public void OnDestroy()
  123. {
  124. if (!destroyed && nibiruTensorManager != null)
  125. {
  126. destroyed = true;
  127. nibiruTensorManager.Call("destroy");
  128. }
  129. }
  130. public bool IsRunning()
  131. {
  132. return !stoped && !destroyed;
  133. }
  134. void VerifyStatusCallback(int status)
  135. {
  136. if (status != 0)
  137. {
  138. Debug.LogError("VerifyStatusCallback=" + status);
  139. } else
  140. {
  141. Debug.Log("Verify Success !!!");
  142. }
  143. }
  144. public delegate void OnNVRVerifyListener(int status);
  145. public class NibiruVerifyListener : AndroidJavaProxy
  146. {
  147. OnNVRVerifyListener _OnNVRVerifyListener;
  148. public NibiruVerifyListener(OnNVRVerifyListener onNVRVerifyListener) : base("com.nibiru.lib.vr.listener.NVRVerifyListener")
  149. {
  150. _OnNVRVerifyListener = onNVRVerifyListener;
  151. }
  152. public void onVerifySuccess()
  153. {
  154. if (_OnNVRVerifyListener != null)
  155. {
  156. _OnNVRVerifyListener(0);
  157. }
  158. }
  159. public void onVerifyFailed(int status)
  160. {
  161. if (_OnNVRVerifyListener != null)
  162. {
  163. _OnNVRVerifyListener(-1);
  164. }
  165. }
  166. }
  167. public delegate void OnRecognizeFailed(string message);
  168. public delegate void OnRecognizeSuccess(List<Recognition> recognitionData);
  169. public class NibiruRecognizeCallback : AndroidJavaProxy
  170. {
  171. RecoginizeApi _RecognizeApi;
  172. OnRecognizeSuccess _OnRecognizeSuccess;
  173. OnRecognizeFailed _OnRecognizeFailed;
  174. public NibiruRecognizeCallback(RecoginizeApi recognizeApi, OnRecognizeSuccess succ, OnRecognizeFailed failed) : base("com.nibiru.tensorflow.core.NibiruTensorCallback")
  175. {
  176. _RecognizeApi = recognizeApi;
  177. _OnRecognizeSuccess = succ;
  178. _OnRecognizeFailed = failed;
  179. }
  180. //List<Classifier.Recognition> mappedRecognitions
  181. public void onRecoginizeSuccess(AndroidJavaObject list)
  182. {
  183. if (!_RecognizeApi.IsRunning()) return;
  184. int size = list.Call<int>("size");
  185. List<Recognition> dataList = new List<Recognition>(size);
  186. for (int i = 0; i < size; i++)
  187. {
  188. Recognition rec = new Recognition();
  189. AndroidJavaObject item = list.Call<AndroidJavaObject>("get", i);
  190. rec.id = item.Call<string>("getId");
  191. rec.title = item.Call<string>("getTitle");
  192. AndroidJavaObject confidenceObject = item.Call<AndroidJavaObject>("getConfidence");
  193. rec.confidence = ((int) (confidenceObject.Call<float>("floatValue") * 100)) / 100.00f;
  194. AndroidJavaObject locationObject = item.Call<AndroidJavaObject>("getLocation");
  195. float cx = locationObject.Call<float>("centerX");
  196. float cy = locationObject.Call<float>("centerY");
  197. float width = locationObject.Call<float>("width");
  198. float height = locationObject.Call<float>("height");
  199. rec.frameWidth = item.Call<int>("getPreviewWidth");
  200. rec.frameHeight = item.Call<int>("getPreviewHeight");
  201. Rect rect = new Rect(cx - width / 2, cy - height / 2, width, height);
  202. rec.location = rect;
  203. // LOG
  204. // rec.PrintInfo();
  205. dataList.Add(rec);
  206. Loom.QueueOnMainThread((param) =>
  207. {
  208. if (_OnRecognizeSuccess != null)
  209. {
  210. _OnRecognizeSuccess((List<Recognition>)param);
  211. }
  212. }, dataList);
  213. }
  214. }
  215. public void onRecognizeFailed(AndroidJavaObject errorType, string message)
  216. {
  217. Debug.LogError("onRecognizeFailed." + message);
  218. Loom.QueueOnMainThread((param) =>
  219. {
  220. if (_OnRecognizeFailed != null)
  221. {
  222. _OnRecognizeFailed((string) param);
  223. }
  224. }, message);
  225. }
  226. }
  227. }
  228. }