IisuDataSource.cs 11 KB

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