IisuDataSource.cs 12 KB

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