IisuInputProvider.cs 13 KB

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