IisuDataSource.cs 9.3 KB

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