InputProviderIntel.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. initCameraTask.Wait(TimeSpan.FromSeconds(5));
  88. crashed = !initCameraTask.IsCompleted || !initCameraTask.Result;
  89. if (crashed) {
  90. Console.WriteLine("Something went wrong during init. Waiting a few seconds to cool down...");
  91. Thread.Sleep(2000);
  92. }
  93. }
  94. else {
  95. crashed = !tryAcquireAndHandleFrame();
  96. }
  97. }
  98. }
  99. private bool tryAcquireAndHandleFrame()
  100. {
  101. try
  102. {
  103. //try to acquire new frame...
  104. Task<bool> acquireFrameTask = Task.Run(() => acquireNewFrame());
  105. //wait a second...
  106. acquireFrameTask.Wait(TimeSpan.FromSeconds(4));
  107. if (acquireFrameTask.IsCompleted && acquireFrameTask.Result)
  108. {
  109. //that worked, we should have a new frame by now...
  110. handleNewFrame();
  111. senseManager.ReleaseFrame();
  112. return true;
  113. }
  114. else {
  115. return false;
  116. }
  117. }
  118. catch (Exception e)
  119. {
  120. Console.WriteLine("Exception while acquiring frame: " + e);
  121. return false;
  122. }
  123. }
  124. private bool restartCamera()
  125. {
  126. try
  127. {
  128. crashed = false;
  129. senseManager.Dispose();
  130. Thread.Sleep(1000);
  131. return initCameraDevice();
  132. }
  133. catch (Exception)
  134. {
  135. return false;
  136. }
  137. }
  138. public bool IsCrashed()
  139. {
  140. return crashed;
  141. }
  142. protected bool acquireNewFrame() {
  143. var status = senseManager.AcquireFrame(true);
  144. if (status != pxcmStatus.PXCM_STATUS_NO_ERROR) {
  145. Console.WriteLine("Cam crashed with status" + status);
  146. return false;
  147. }
  148. return true;
  149. }
  150. protected void run()
  151. {
  152. while (IsActive) {
  153. if (senseManager.AcquireFrame(true).IsError())
  154. continue;
  155. if (NewFrameEvent != null)
  156. {
  157. handleNewFrame();
  158. }
  159. senseManager.ReleaseFrame();
  160. }
  161. senseManager.Dispose();
  162. }
  163. private void handleNewFrame()
  164. {
  165. PXCMCapture.Sample sample = senseManager.QuerySample();
  166. PXCMImage depthImage = sample.depth;
  167. PXCMImage.ImageInfo info = depthImage.info;
  168. PXCMImage.ImageData imageData;
  169. depthImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out imageData);
  170. ushort[] data = imageData.ToUShortArray(0, info.width * info.height);
  171. Image<Gray, UInt16> dImg;
  172. unsafe
  173. {
  174. fixed (ushort* d = data)
  175. {
  176. IntPtr ptr = (IntPtr)d;
  177. dImg = new Image<Gray, UInt16>(info.width, info.height, info.width * sizeof(ushort), ptr).Copy();
  178. }
  179. }
  180. depthImage.ReleaseAccess(imageData);
  181. depthImage.Dispose();
  182. // confidence filter
  183. Image<Gray, byte> mask = dImg.InRange(new Gray(lowConfidenceValue), new Gray(lowConfidenceValue));
  184. dImg.SetValue(new Gray(Int16.MaxValue), mask);
  185. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, dImg));
  186. CurrentFrameID += 1;
  187. }
  188. }
  189. }