IisuInputProvider.cs 14 KB

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