NRTrackingImageDatabase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /****************************************************************************
  2. * Copyright 2019 Nreal Techonology Limited. All rights reserved.
  3. *
  4. * This file is part of NRSDK.
  5. *
  6. * https://www.nreal.ai/
  7. *
  8. *****************************************************************************/
  9. namespace NRKernal
  10. {
  11. using System;
  12. using System.Collections.Generic;
  13. using UnityEngine;
  14. #if UNITY_EDITOR
  15. using System.IO;
  16. using UnityEditor;
  17. using LitJson;
  18. using System.Text;
  19. #endif
  20. /// <summary>
  21. /// A database storing a list of images to be detected and tracked by NRSDK. An image database
  22. /// supports up to 1000 images.Only one image database can be in use at any given time. </summary>
  23. public class NRTrackingImageDatabase : ScriptableObject
  24. {
  25. /// <summary> The images. </summary>
  26. [SerializeField]
  27. private List<NRTrackingImageDatabaseEntry> m_Images = new List<NRTrackingImageDatabaseEntry>();
  28. /// <summary> Information describing the raw. </summary>
  29. [SerializeField]
  30. private byte[] m_RawData = null;
  31. /// <summary> Gets information describing the raw. </summary>
  32. /// <value> Information describing the raw. </value>
  33. public byte[] RawData
  34. {
  35. get
  36. {
  37. return m_RawData;
  38. }
  39. }
  40. /// <summary> Unique identifier. </summary>
  41. public string GUID;
  42. /// <summary> Gets the full pathname of the tracking image data file. </summary>
  43. /// <value> The full pathname of the tracking image data file. </value>
  44. public string TrackingImageDataPath
  45. {
  46. get
  47. {
  48. return NRTools.GetTrackingImageDataGenPath() + GUID + "/";
  49. }
  50. }
  51. /// <summary> Gets the full pathname of the tracking image data out put file. </summary>
  52. /// <value> The full pathname of the tracking image data out put file. </value>
  53. public string TrackingImageDataOutPutPath
  54. {
  55. get
  56. {
  57. return NRTools.GetTrackingImageDataGenPath();
  58. }
  59. }
  60. #if UNITY_EDITOR
  61. /// <summary> True if is raw data dirty, false if not. </summary>
  62. [SerializeField]
  63. private bool m_IsRawDataDirty = true;
  64. /// <summary> The CLI version. </summary>
  65. [SerializeField]
  66. private string m_CliVersion = string.Empty;
  67. /// <summary> Gets a value indicating whether this object is CLI updated. </summary>
  68. /// <value> True if this object is CLI updated, false if not. </value>
  69. public bool isCliUpdated
  70. {
  71. get
  72. {
  73. string cliBinaryPath;
  74. if (!FindCliBinaryPath(out cliBinaryPath))
  75. {
  76. return false;
  77. }
  78. string currentCliVersion;
  79. {
  80. string error;
  81. ShellHelper.RunCommand(cliBinaryPath, "-version", out currentCliVersion, out error);
  82. }
  83. //NRDebugger.Info("current version:{0} old version:{1}", currentCliVersion, m_CliVersion);
  84. bool cliUpdated = m_CliVersion != currentCliVersion;
  85. return cliUpdated;
  86. }
  87. }
  88. #endif
  89. /// <summary> Constructs a new <c>TrackingImageDatabase</c>. </summary>
  90. public NRTrackingImageDatabase()
  91. {
  92. #if UNITY_EDITOR
  93. GUID = Guid.NewGuid().ToString();
  94. #endif
  95. }
  96. /// <summary> Gets the number of images in the database. </summary>
  97. /// <value> The count. </value>
  98. public int Count
  99. {
  100. get
  101. {
  102. lock (m_Images)
  103. {
  104. return m_Images.Count;
  105. }
  106. }
  107. }
  108. /// <summary>
  109. /// Gets or sets the image at the specified <c>index</c>. You can only modify the database in the
  110. /// Unity editor. </summary>
  111. /// <param name="index"> The zero-based index of the image entry to get or set.</param>
  112. /// <returns> The image entry at <c>index</c>. </returns>
  113. public NRTrackingImageDatabaseEntry this[int index]
  114. {
  115. get
  116. {
  117. lock (m_Images)
  118. {
  119. return m_Images[index];
  120. }
  121. }
  122. #if UNITY_EDITOR
  123. set
  124. {
  125. var oldValue = m_Images[index];
  126. m_Images[index] = value;
  127. if (oldValue.TextureGUID != m_Images[index].TextureGUID
  128. || oldValue.Name != m_Images[index].Name
  129. || oldValue.Width != m_Images[index].Width
  130. || oldValue.Height != m_Images[index].Height)
  131. {
  132. m_IsRawDataDirty = true;
  133. }
  134. EditorUtility.SetDirty(this);
  135. }
  136. #endif
  137. }
  138. #if UNITY_EDITOR
  139. /// <summary> Adds an image entry to the end of the database. </summary>
  140. /// <param name="entry"> The image entry to add.</param>
  141. public void Add(NRTrackingImageDatabaseEntry entry)
  142. {
  143. m_Images.Add(entry);
  144. EditorUtility.SetDirty(this);
  145. }
  146. /// <summary> Removes an image entry at a specified zero-based index. </summary>
  147. /// <param name="index"> The index of the image entry to remove.</param>
  148. public void RemoveAt(int index)
  149. {
  150. m_Images.RemoveAt(index);
  151. EditorUtility.SetDirty(this);
  152. }
  153. /// <summary> Rebuilds the database asset, if needed. </summary>
  154. public void BuildIfNeeded()
  155. {
  156. if (!m_IsRawDataDirty)
  157. {
  158. return;
  159. }
  160. m_IsRawDataDirty = false;
  161. string directory = NRTools.GetTrackingImageDataGenPath() + GUID;
  162. if (!Directory.Exists(directory))
  163. {
  164. ZipUtility.UnzipFile(RawData, NRTools.GetTrackingImageDataGenPath(), NativeConstants.ZipKey);
  165. }
  166. //Generate marker data by json file
  167. var result_json = TrackingImageDataPath + "markers.json";
  168. if (File.Exists(result_json))
  169. {
  170. var json_data = File.ReadAllText(result_json);
  171. StringBuilder str = new StringBuilder();
  172. str.AppendLine("# Number of markers");
  173. str.AppendLine(Count.ToString());
  174. DirectoryInfo dir = new DirectoryInfo(directory);
  175. var fsinfos = dir.GetDirectories();
  176. string dataPathName = "Data";
  177. if (fsinfos != null && fsinfos.Length > 0)
  178. {
  179. Array.Sort(fsinfos, (dir1, dir2) => dir1.CreationTime.CompareTo(dir2.CreationTime));
  180. dataPathName = fsinfos[0].Name;
  181. }
  182. for (int i = 0; i < Count; i++)
  183. {
  184. var obj = JsonMapper.ToObject(json_data);
  185. if (obj != null)
  186. {
  187. var image_info = obj[this[i].Name];
  188. if (image_info != null)
  189. {
  190. str.AppendLine();
  191. str.AppendLine(string.Format("./{0}/{1}", dataPathName, this[i].Name));
  192. str.AppendLine("NFT");
  193. str.AppendLine(string.Format("FILTER {0}", image_info["filter"]));
  194. str.AppendLine(string.Format("MARKER_WIDTH {0}", image_info["physical_width"]));
  195. str.AppendLine(string.Format("MARKER_HEIGHT {0}", image_info["physical_height"]));
  196. }
  197. }
  198. }
  199. File.WriteAllText(TrackingImageDataPath + "markers.dat", str.ToString());
  200. }
  201. string file_path = directory + "_zipFile";
  202. // Generate zip file
  203. ZipUtility.Zip(new string[1] { directory }, file_path, NativeConstants.ZipKey, new ZipUtility.ZipCallback(_result =>
  204. {
  205. m_IsRawDataDirty = _result ? false : true;
  206. if (!string.IsNullOrEmpty(file_path) && File.Exists(file_path))
  207. {
  208. // Read the zip bytes
  209. m_RawData = File.ReadAllBytes(file_path);
  210. //NRDebugger.Info("Generate raw data success!" + file_path);
  211. //m_IsNeedLoadRawData = false;
  212. EditorUtility.SetDirty(this);
  213. // Force a save to make certain build process will get updated asset.
  214. AssetDatabase.SaveAssets();
  215. }
  216. }));
  217. UpdateClipVersion();
  218. }
  219. /// <summary> Updates the clip version. </summary>
  220. private void UpdateClipVersion()
  221. {
  222. string cliBinaryPath;
  223. if (!FindCliBinaryPath(out cliBinaryPath))
  224. {
  225. return;
  226. }
  227. string currentCliVersion;
  228. {
  229. string error;
  230. ShellHelper.RunCommand(cliBinaryPath, "-version", out currentCliVersion, out error);
  231. }
  232. //NRDebugger.Info("current version:{0} old version:{1}", currentCliVersion, m_CliVersion);
  233. m_CliVersion = currentCliVersion;
  234. }
  235. /// <summary> Gets the image entries that require updating of the image quality score. </summary>
  236. /// <returns> A list of image entries that require updating of the image quality score. </returns>
  237. public List<NRTrackingImageDatabaseEntry> GetDirtyQualityEntries()
  238. {
  239. var dirtyEntries = new List<NRTrackingImageDatabaseEntry>();
  240. for (int i = 0; i < m_Images.Count; ++i)
  241. {
  242. if (!string.IsNullOrEmpty(m_Images[i].Quality))
  243. {
  244. continue;
  245. }
  246. dirtyEntries.Add(m_Images[i]);
  247. }
  248. if (dirtyEntries.Count == 0)
  249. {
  250. BuildIfNeeded();
  251. }
  252. return dirtyEntries;
  253. }
  254. /// <summary> Gets all entries. </summary>
  255. /// <returns> all entries. </returns>
  256. public List<NRTrackingImageDatabaseEntry> GetAllEntries()
  257. {
  258. var allEntries = new List<NRTrackingImageDatabaseEntry>();
  259. for (int i = 0; i < m_Images.Count; ++i)
  260. {
  261. allEntries.Add(m_Images[i]);
  262. }
  263. return allEntries;
  264. }
  265. /// <summary> Finds the path to the command-line tool used to generate a database. </summary>
  266. /// <param name="path"> [out] The path to the command-line tool that will be set if a valid path
  267. /// was found.</param>
  268. /// <returns> <c>true</c> if a valid path was found, <c>false</c> otherwise. </returns>
  269. public static bool FindCliBinaryPath(out string path)
  270. {
  271. var binaryName = NativeConstants.TrackingImageCliBinary;
  272. string[] cliBinaryGuid = AssetDatabase.FindAssets(binaryName);
  273. if (cliBinaryGuid.Length == 0)
  274. {
  275. NRDebugger.Info("Could not find required tool for building TrackingImageDatabase: {0}. " +
  276. "Was it removed from the NRSDK?", binaryName);
  277. path = string.Empty;
  278. return false;
  279. }
  280. // Remove the '/Assets' from the project path since it will be added in the path below.
  281. string projectPath = Application.dataPath.Substring(0, Application.dataPath.Length - 6);
  282. path = Path.Combine(projectPath, AssetDatabase.GUIDToAssetPath(cliBinaryGuid[0]));
  283. return !string.IsNullOrEmpty(path);
  284. }
  285. #endif
  286. }
  287. }