InputProviderIntel.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Emgu.CV;
  7. using Emgu.CV.Structure;
  8. using System.Drawing;
  9. using System.Threading;
  10. namespace BBIWARG.Input.InputProviding
  11. {
  12. class InputProviderIntel : IInputProvider
  13. {
  14. PXCMSenseManager senseManager;
  15. public int CurrentFrameID
  16. {
  17. get;
  18. private set;
  19. }
  20. public float FieldOfViewHorizontal
  21. {
  22. get;
  23. private set;
  24. }
  25. public float FieldOfViewVertical
  26. {
  27. get;
  28. private set;
  29. }
  30. public int ImageWidth
  31. {
  32. get { return 640; }
  33. }
  34. public int ImageHeight
  35. {
  36. get { return 480; }
  37. }
  38. public bool IsActive
  39. {
  40. get;
  41. private set;
  42. }
  43. public event DeviceStartedEventHandler DeviceStartedEvent;
  44. public event NewFrameEventHandler NewFrameEvent;
  45. protected UInt16 lowConfidenceValue;
  46. public void initialize()
  47. {
  48. CurrentFrameID = 0;
  49. }
  50. public void start()
  51. {
  52. initCameraDevice();
  53. IsActive = true;
  54. if (DeviceStartedEvent != null && CurrentFrameID == 0)
  55. DeviceStartedEvent(this, new EventArgs());
  56. keepActive();
  57. }
  58. private bool initCameraDevice()
  59. {
  60. senseManager = PXCMSenseManager.CreateInstance();
  61. PXCMVideoModule.DataDesc ddesc = new PXCMVideoModule.DataDesc();
  62. ddesc.deviceInfo.streams = PXCMCapture.StreamType.STREAM_TYPE_DEPTH;
  63. senseManager.EnableStreams(ddesc);
  64. var initStatus = senseManager.Init();
  65. if (initStatus != pxcmStatus.PXCM_STATUS_NO_ERROR) {
  66. Console.WriteLine("Could not init camera. Result: " + initStatus);
  67. return false;
  68. }
  69. PXCMPointF32 fov = senseManager.captureManager.device.QueryDepthFieldOfView();
  70. FieldOfViewHorizontal = fov.x;
  71. FieldOfViewVertical = fov.y;
  72. lowConfidenceValue = senseManager.captureManager.device.QueryDepthLowConfidenceValue();
  73. senseManager.captureManager.device.SetDepthConfidenceThreshold((UInt16)Parameters.ConfidenceImageMinThreshold);
  74. return true;
  75. }
  76. public void stop()
  77. {
  78. IsActive = false;
  79. }
  80. bool crashed = false;
  81. protected void keepActive() {
  82. while (IsActive) {
  83. if (crashed)
  84. {
  85. Console.WriteLine("Trying to reinitialize the camera...");
  86. Task<bool> initCameraTask = Task.Run(() => restartCamera());
  87. crashed = !initCameraTask.Wait(TimeSpan.FromSeconds(5)) || !initCameraTask.Result;
  88. if (crashed) {
  89. Console.WriteLine("Something went wrong during init. Waiting a few seconds to cool down...");
  90. Thread.Sleep(2000);
  91. }
  92. }
  93. else {
  94. crashed = !tryAcquireAndHandleFrame();
  95. }
  96. }
  97. }
  98. private bool tryAcquireAndHandleFrame()
  99. {
  100. try
  101. {
  102. //try to acquire new frame...
  103. Task<bool> acquireFrameTask = Task.Run(() => acquireNewFrame());
  104. //wait a second...
  105. if (acquireFrameTask.Wait(TimeSpan.FromSeconds(4)) && acquireFrameTask.Result)
  106. {
  107. //that worked, we should have a new frame by now...
  108. handleNewFrame();
  109. senseManager.ReleaseFrame();
  110. return true;
  111. }
  112. else {
  113. return false;
  114. }
  115. }
  116. catch (Exception e)
  117. {
  118. Console.WriteLine("Exception while acquiring frame: " + e);
  119. return false;
  120. }
  121. }
  122. private bool restartCamera()
  123. {
  124. try
  125. {
  126. crashed = false;
  127. senseManager.Dispose();
  128. return initCameraDevice();
  129. }
  130. catch (Exception)
  131. {
  132. return false;
  133. }
  134. }
  135. public bool IsCrashed()
  136. {
  137. return crashed;
  138. }
  139. protected bool acquireNewFrame() {
  140. var status = senseManager.AcquireFrame(true);
  141. if (status != pxcmStatus.PXCM_STATUS_NO_ERROR) {
  142. Console.WriteLine("Cam crashed with status" + status);
  143. return false;
  144. }
  145. return true;
  146. }
  147. protected void run()
  148. {
  149. while (IsActive) {
  150. if (senseManager.AcquireFrame(true).IsError())
  151. continue;
  152. if (NewFrameEvent != null)
  153. {
  154. handleNewFrame();
  155. }
  156. senseManager.ReleaseFrame();
  157. }
  158. senseManager.Dispose();
  159. }
  160. private void handleNewFrame()
  161. {
  162. PXCMCapture.Sample sample = senseManager.QuerySample();
  163. PXCMImage depthImage = sample.depth;
  164. PXCMImage.ImageInfo info = depthImage.info;
  165. PXCMImage.ImageData imageData;
  166. depthImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out imageData);
  167. ushort[] data = imageData.ToUShortArray(0, info.width * info.height);
  168. Image<Gray, UInt16> dImg;
  169. unsafe
  170. {
  171. fixed (ushort* d = data)
  172. {
  173. IntPtr ptr = (IntPtr)d;
  174. dImg = new Image<Gray, UInt16>(info.width, info.height, info.width * sizeof(ushort), ptr).Copy();
  175. }
  176. }
  177. depthImage.ReleaseAccess(imageData);
  178. depthImage.Dispose();
  179. // confidence filter
  180. Image<Gray, byte> mask = dImg.InRange(new Gray(lowConfidenceValue), new Gray(lowConfidenceValue));
  181. dImg.SetValue(new Gray(Int16.MaxValue), mask);
  182. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, dImg));
  183. CurrentFrameID += 1;
  184. }
  185. }
  186. }