IisuInputProvider.cs 13 KB

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