InputProviderIntel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 void 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. senseManager.Init();
  65. PXCMPointF32 fov = senseManager.captureManager.device.QueryDepthFieldOfView();
  66. FieldOfViewHorizontal = fov.x;
  67. FieldOfViewVertical = fov.y;
  68. lowConfidenceValue = senseManager.captureManager.device.QueryDepthLowConfidenceValue();
  69. senseManager.captureManager.device.SetDepthConfidenceThreshold((UInt16)Parameters.ConfidenceImageMinThreshold);
  70. }
  71. public void stop()
  72. {
  73. IsActive = false;
  74. }
  75. bool crashed = false;
  76. protected void keepActive() {
  77. while (IsActive) {
  78. if (crashed)
  79. {
  80. Console.WriteLine("Trying to reinitialize the camera...");
  81. Task<bool> initCameraTask = Task.Run(() => restartCamera());
  82. crashed = !initCameraTask.Wait(TimeSpan.FromSeconds(5)) || !initCameraTask.Result;
  83. Thread.Yield();
  84. }
  85. else {
  86. crashed = !tryAcquireAndHandleFrame();
  87. }
  88. }
  89. }
  90. private bool tryAcquireAndHandleFrame()
  91. {
  92. try
  93. {
  94. //try to acquire new frame...
  95. Task<bool> acquireFrameTask = Task.Run(() => acquireNewFrame());
  96. //wait a second...
  97. if (acquireFrameTask.Wait(TimeSpan.FromSeconds(4)) && acquireFrameTask.Result)
  98. {
  99. //that worked, we should have a new frame by now...
  100. handleNewFrame();
  101. senseManager.ReleaseFrame();
  102. return true;
  103. }
  104. else {
  105. return false;
  106. }
  107. }
  108. catch (Exception e)
  109. {
  110. Console.WriteLine("Exception while acquiring frame: " + e);
  111. return false;
  112. }
  113. }
  114. private bool restartCamera()
  115. {
  116. try
  117. {
  118. crashed = false;
  119. senseManager.Dispose();
  120. initCameraDevice();
  121. }
  122. catch (Exception)
  123. {
  124. return false;
  125. }
  126. return true;
  127. }
  128. protected bool acquireNewFrame() {
  129. var status = senseManager.AcquireFrame(true);
  130. if (status != pxcmStatus.PXCM_STATUS_NO_ERROR) {
  131. Console.WriteLine("Cam crashed with status" + status);
  132. return false;
  133. }
  134. return true;
  135. }
  136. protected void run()
  137. {
  138. while (IsActive) {
  139. if (senseManager.AcquireFrame(true).IsError())
  140. continue;
  141. if (NewFrameEvent != null)
  142. {
  143. handleNewFrame();
  144. }
  145. senseManager.ReleaseFrame();
  146. }
  147. senseManager.Dispose();
  148. }
  149. private void handleNewFrame()
  150. {
  151. PXCMCapture.Sample sample = senseManager.QuerySample();
  152. PXCMImage depthImage = sample.depth;
  153. PXCMImage.ImageInfo info = depthImage.info;
  154. PXCMImage.ImageData imageData;
  155. depthImage.AcquireAccess(PXCMImage.Access.ACCESS_READ, PXCMImage.PixelFormat.PIXEL_FORMAT_DEPTH, out imageData);
  156. ushort[] data = imageData.ToUShortArray(0, info.width * info.height);
  157. Image<Gray, UInt16> dImg;
  158. unsafe
  159. {
  160. fixed (ushort* d = data)
  161. {
  162. IntPtr ptr = (IntPtr)d;
  163. dImg = new Image<Gray, UInt16>(info.width, info.height, info.width * sizeof(ushort), ptr).Copy();
  164. }
  165. }
  166. depthImage.ReleaseAccess(imageData);
  167. depthImage.Dispose();
  168. // confidence filter
  169. Image<Gray, byte> mask = dImg.InRange(new Gray(lowConfidenceValue), new Gray(lowConfidenceValue));
  170. dImg.SetValue(new Gray(Int16.MaxValue), mask);
  171. NewFrameEvent(this, new NewFrameEventArgs(CurrentFrameID, dImg));
  172. CurrentFrameID += 1;
  173. }
  174. }
  175. }