IisuInputProvider.cs 13 KB

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