IisuInputProvider.cs 14 KB

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