IisuDataSource.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Iisu;
  4. using MathNet.Numerics.LinearAlgebra.Single;
  5. namespace bbiwarg.DataSource
  6. {
  7. class IIsuDataSource: IVideoDataSource
  8. {
  9. private IHandle handle;
  10. private IDevice device;
  11. private string moviePath;
  12. bool active;
  13. // parameters
  14. private IParameterHandle<float> frameRate;
  15. // data
  16. private IDataHandle<bool>[] handOpen = new IDataHandle<bool>[2];
  17. private IDataHandle<Iisu.Data.Vector3>[] palmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  18. private IDataHandle<Iisu.Data.Vector2>[] palmPositions2D = new IDataHandle<Iisu.Data.Vector2>[2];
  19. private IDataHandle<Iisu.Data.Vector3>[] tipPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  20. private IDataHandle<Iisu.Data.Vector3>[] forearmPositions3D = new IDataHandle<Iisu.Data.Vector3>[2];
  21. private IDataHandle<Iisu.Data.Vector3>[] palmNormals3D = new IDataHandle<Iisu.Data.Vector3>[2];
  22. private IDataHandle<int[]>[] fingerStatus = new IDataHandle<int[]>[2];
  23. private IDataHandle<int>[] handStatus = new IDataHandle<int>[2];
  24. private IDataHandle<Iisu.Data.Vector3[]>[] fingerTipPositions3D = new IDataHandle<Iisu.Data.Vector3[]>[2];
  25. private IDataHandle<Iisu.Data.Vector2[]>[] fingerTipPositions2D = new IDataHandle<Iisu.Data.Vector2[]>[2];
  26. private IDataHandle<int>[] handSides = new IDataHandle<int>[2];
  27. private IDataHandle<Iisu.Data.IImageData> depthImage;
  28. private IDataHandle<Iisu.Data.IImageData> colorImage;
  29. private IDataHandle<Iisu.Data.IImageData> confidenceImage;
  30. private IDataHandle<Iisu.Data.IImageData> uvImage;
  31. private IParameterHandle<float> hfov;
  32. private IParameterHandle<float> vfov;
  33. /*
  34. * Creates an Iisu data source.
  35. * params:
  36. * moviePath: path to movie to be used as source
  37. * if empty the camera is used
  38. */
  39. public IIsuDataSource(string moviePath = "")
  40. {
  41. this.moviePath = moviePath;
  42. active = false;
  43. }
  44. public void init()
  45. {
  46. handle = Iisu.Iisu.Context.CreateHandle();
  47. IDeviceConfiguration conf = handle.CreateDeviceConfiguration();
  48. if (moviePath.Length != 0)
  49. conf.MoviePath = moviePath;
  50. device = handle.InitializeDevice(conf);
  51. // parameters
  52. if (moviePath.Length != 0)
  53. device.RegisterParameterHandle<int>("SOURCE.MOVIE.PlayMode").Value = 0; // playMode = once
  54. else
  55. device.RegisterParameterHandle<int>("SOURCE.DEPTHSENSE.AmplitudeThreshold").Value = 100; // confidence-threshhold
  56. frameRate = device.RegisterParameterHandle<float>("SOURCE.FrameRate");
  57. hfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.HFOV");
  58. vfov = device.RegisterParameterHandle<float>("SOURCE.CAMERA.DEPTH.VFOV");
  59. // events
  60. device.EventManager.RegisterEventListener("DEVICE.Status", new Iisu.EventDelegates.Device.Status(onDeviceStatusChanged));
  61. // data
  62. depthImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.DEPTH.Image");
  63. colorImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.Image");
  64. confidenceImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.CONFIDENCE.Image");
  65. uvImage = device.RegisterDataHandle<Iisu.Data.IImageData>("SOURCE.CAMERA.COLOR.REGISTRATION.UV.Image");
  66. handOpen[0] = device.RegisterDataHandle<bool>("CI.HAND1.IsOpen");
  67. handOpen[1] = device.RegisterDataHandle<bool>("CI.HAND2.IsOpen");
  68. palmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmPosition3D");
  69. palmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmPosition3D");
  70. palmPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND1.PalmPosition2D");
  71. palmPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2>("CI.HAND2.PalmPosition2D");
  72. tipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.TipPosition3D");
  73. tipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.TipPosition3D");
  74. forearmPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.ForearmPosition3D");
  75. forearmPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.ForearmPosition3D");
  76. palmNormals3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND1.PalmNormal3D");
  77. palmNormals3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3>("CI.HAND2.PalmNormal3D");
  78. fingerStatus[0] = device.RegisterDataHandle<int[]>("CI.HAND1.FingerStatus");
  79. fingerStatus[1] = device.RegisterDataHandle<int[]>("CI.HAND2.FingerStatus");
  80. handStatus[0] = device.RegisterDataHandle<int>("CI.HAND1.Status");
  81. handStatus[1] = device.RegisterDataHandle<int>("CI.HAND2.Status");
  82. fingerTipPositions3D[0] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND1.FingerTipPositions3D");
  83. fingerTipPositions3D[1] = device.RegisterDataHandle<Iisu.Data.Vector3[]>("CI.HAND2.FingerTipPositions3D");
  84. fingerTipPositions2D[0] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND1.FingerTipPositions2D");
  85. fingerTipPositions2D[1] = device.RegisterDataHandle<Iisu.Data.Vector2[]>("CI.HAND2.FingerTipPositions2D");
  86. handSides[0] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  87. handSides[1] = device.RegisterDataHandle<int>("CI.HAND1.Side");
  88. }
  89. private void onDeviceStatusChanged(string eventName, DeviceStatus status)
  90. {
  91. active = status.HasFlag(Iisu.DeviceStatus.Playing);
  92. }
  93. public void start()
  94. {
  95. device.Start();
  96. }
  97. public void stop()
  98. {
  99. device.Stop(true);
  100. }
  101. public void updateFrame()
  102. {
  103. device.UpdateFrame(true);
  104. }
  105. public void releaseFrame()
  106. {
  107. device.ReleaseFrame();
  108. }
  109. public bool isActive()
  110. {
  111. return active;
  112. }
  113. public int getFrameRate()
  114. {
  115. return (int) frameRate.Value;
  116. }
  117. public float getHFOV() {
  118. return hfov.Value;
  119. }
  120. public float getVFOV() {
  121. return vfov.Value;
  122. }
  123. public DepthImage getDepthImage()
  124. {
  125. Iisu.Data.IImageInfos imageInfos = depthImage.Value.ImageInfos;
  126. int width = (int) imageInfos.Width;
  127. int height = (int) imageInfos.Height;
  128. int numBytes = (int) imageInfos.BytesRaw;
  129. IntPtr imageData = depthImage.Value.Raw;
  130. short[] depthData = new short[width * height];
  131. Marshal.Copy(imageData, depthData, 0, width * height);
  132. return new DepthImage(width, height, depthData);
  133. }
  134. public ColorImage getColorImage()
  135. {
  136. Iisu.Data.IImageInfos imageInfos = colorImage.Value.ImageInfos;
  137. int width = (int)imageInfos.Width;
  138. int height = (int)imageInfos.Height;
  139. int numBytes = (int)imageInfos.BytesRaw;
  140. IntPtr imageData = colorImage.Value.Raw;
  141. byte[] colorData = new byte[numBytes];
  142. Marshal.Copy(imageData, colorData, 0, numBytes);
  143. return new ColorImage(width, height, colorData);
  144. }
  145. public ConfidenceImage getConfidenceImage()
  146. {
  147. Iisu.Data.IImageInfos imageInfos = confidenceImage.Value.ImageInfos;
  148. int width = (int)imageInfos.Width;
  149. int height = (int)imageInfos.Height;
  150. int numBytes = (int)imageInfos.BytesRaw;
  151. IntPtr imageData = confidenceImage.Value.Raw;
  152. short[] confidenceData = new short[width * height];
  153. Marshal.Copy(imageData, confidenceData, 0, width * height);
  154. return new ConfidenceImage(width, height, confidenceData);
  155. }
  156. public UVImage getUVImage()
  157. {
  158. Iisu.Data.IImageInfos imageInfos = uvImage.Value.ImageInfos;
  159. int width = (int)imageInfos.Width;
  160. int height = (int)imageInfos.Height;
  161. int numBytes = (int)imageInfos.BytesRaw;
  162. IntPtr imageData = uvImage.Value.Raw;
  163. float[] uvData = new float[2 * width * height];
  164. Marshal.Copy(imageData, uvData, 0, 2 * width * height);
  165. return new UVImage(width, height, uvData);
  166. }
  167. public ImageData getImageData()
  168. {
  169. return new ImageData(getDepthImage(), getConfidenceImage(), getColorImage(), getUVImage());
  170. }
  171. private void checkHandIndex(uint handIndex)
  172. {
  173. if (handIndex < 1 || handIndex > 2)
  174. throw new ArgumentOutOfRangeException("handIndex is out of range [0,1]");
  175. }
  176. public bool isHandOpen(uint handIndex)
  177. {
  178. checkHandIndex(handIndex);
  179. return handOpen[handIndex - 1].Value;
  180. }
  181. public Vector getPalmPosition3D(uint handIndex)
  182. {
  183. checkHandIndex(handIndex);
  184. return new DenseVector(palmPositions3D[handIndex - 1].Value.ToArray());
  185. }
  186. public Vector getPalmPosition2D(uint handIndex)
  187. {
  188. checkHandIndex(handIndex);
  189. return new DenseVector(palmPositions2D[handIndex - 1].Value.ToArray());
  190. }
  191. public Vector getTipPosition3D(uint handIndex)
  192. {
  193. checkHandIndex(handIndex);
  194. return new DenseVector(tipPositions3D[handIndex - 1].Value.ToArray());
  195. }
  196. public Vector getForearmPosition3D(uint handIndex)
  197. {
  198. checkHandIndex(handIndex);
  199. return new DenseVector(forearmPositions3D[handIndex - 1].Value.ToArray());
  200. }
  201. public Vector getPalmNormal3D(uint handIndex)
  202. {
  203. checkHandIndex(handIndex);
  204. return new DenseVector(palmNormals3D[handIndex - 1].Value.ToArray());
  205. }
  206. public DetectionStatus[] getFingerStatus(uint handIndex)
  207. {
  208. checkHandIndex(handIndex);
  209. int[] status = fingerStatus[handIndex - 1].Value;
  210. DetectionStatus[] result = new DetectionStatus[status.Length];
  211. for (int i = 0; i < status.Length; ++i)
  212. {
  213. switch (status[i])
  214. {
  215. case 0:
  216. result[i] = DetectionStatus.Inactive;
  217. break;
  218. case 1:
  219. result[i] = DetectionStatus.Detected;
  220. break;
  221. case 2:
  222. result[i] = DetectionStatus.Tracked;
  223. break;
  224. }
  225. }
  226. return result;
  227. }
  228. public DetectionStatus getHandStatus(uint handIndex)
  229. {
  230. checkHandIndex(handIndex);
  231. int status = handStatus[handIndex - 1].Value;
  232. DetectionStatus result = new DetectionStatus();
  233. switch (status)
  234. {
  235. case 0:
  236. result = DetectionStatus.Inactive;
  237. break;
  238. case 1:
  239. result = DetectionStatus.Detected;
  240. break;
  241. case 2:
  242. result = DetectionStatus.Tracked;
  243. break;
  244. }
  245. return result;
  246. }
  247. public Vector[] getFingerTipPositions3D(uint handIndex)
  248. {
  249. checkHandIndex(handIndex);
  250. Iisu.Data.Vector3[] positions = fingerTipPositions3D[handIndex - 1].Value;
  251. Vector[] results = new DenseVector[positions.Length];
  252. for (int i = 0; i < positions.Length; ++i)
  253. results[i] = new DenseVector(positions[i].ToArray());
  254. return results;
  255. }
  256. public Vector[] getFingerTipPositions2D(uint handIndex)
  257. {
  258. checkHandIndex(handIndex);
  259. Iisu.Data.Vector2[] positions = fingerTipPositions2D[handIndex - 1].Value;
  260. Vector[] results = new DenseVector[positions.Length];
  261. for (int i = 0; i < positions.Length; ++i)
  262. results[i] = new DenseVector(positions[i].ToArray());
  263. return results;
  264. }
  265. public HandSide getHandSide(uint handIndex)
  266. {
  267. checkHandIndex(handIndex);
  268. int side = handSides[handIndex - 1].Value;
  269. switch (side)
  270. {
  271. case 1:
  272. return HandSide.Left;
  273. case 2:
  274. return HandSide.Right;
  275. default:
  276. return HandSide.Unknown;
  277. }
  278. }
  279. }
  280. }