VideoHandle.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7. using bbiwarg.Utility;
  8. using bbiwarg.Detectors.Fingers;
  9. using bbiwarg.Detectors.Palm;
  10. using bbiwarg.Detectors.Touch;
  11. using bbiwarg.Images;
  12. using bbiwarg.InputProviders;
  13. using Emgu.CV;
  14. using Emgu.CV.Structure;
  15. namespace bbiwarg
  16. {
  17. class VideoHandle
  18. {
  19. private IInputProvider inputProvider;
  20. private InputFrame inputFrame;
  21. public int Width { get; private set; }
  22. public int Height { get; private set; }
  23. private DepthImage depthImage;
  24. private EdgeImage edgeImage;
  25. private PalmImage palmImage;
  26. private TouchImage touchImage;
  27. private FingerImage fingerImage;
  28. private FingerDetector fingerDetector;
  29. private PalmDetector palmDetector;
  30. private TouchDetector touchDetector;
  31. private PalmTouchDetector palmTouchDetector;
  32. private FingerTracker fingerTracker;
  33. private TouchTracker touchTracker;
  34. public VideoHandle(IInputProvider inputProvider)
  35. {
  36. this.inputProvider = inputProvider;
  37. //initialize trackers
  38. touchTracker = new TouchTracker();
  39. fingerTracker = new FingerTracker();
  40. }
  41. public void start()
  42. {
  43. inputProvider.init();
  44. inputProvider.start();
  45. inputProvider.updateFrame();
  46. processFrameUpdate();
  47. }
  48. public void stop()
  49. {
  50. inputProvider.stop();
  51. }
  52. public bool sourceIsMovie()
  53. {
  54. return inputProvider.sourceIsMovie();
  55. }
  56. public void reversePlay()
  57. {
  58. inputProvider.reversePlay();
  59. }
  60. public void pauseMovie()
  61. {
  62. inputProvider.pauseMovie();
  63. }
  64. public void unpauseMovie()
  65. {
  66. inputProvider.unpauseMovie();
  67. }
  68. public void nextFrame()
  69. {
  70. if (inputProvider.isActive())
  71. {
  72. inputProvider.releaseFrame();
  73. inputProvider.updateFrame();
  74. processFrameUpdate();
  75. }
  76. else
  77. {
  78. inputProvider.stop();
  79. }
  80. }
  81. public Int16 getDepthAt(int x, int y)
  82. {
  83. return depthImage.getDepthAt(x, y);
  84. }
  85. public float getRelativeDepth(int x, int y)
  86. {
  87. return depthImage.getRelativeDepthAt(x, y);
  88. }
  89. public bool isEdgeAt(int x, int y)
  90. {
  91. return edgeImage.isEdgeAt(x, y);
  92. }
  93. public FingerImageState getFingerImageStateAt(int x, int y)
  94. {
  95. return fingerImage.getStateAt(x, y);
  96. }
  97. public PalmImageState getPalmImageStateAt(int x, int y)
  98. {
  99. return palmImage.getStateAt(x, y);
  100. }
  101. public TouchImageState getTouchImageStateAt(int x, int y)
  102. {
  103. return touchImage.getStateAt(x, y);
  104. }
  105. public List<PalmTouchEvent> getTouchEvents()
  106. {
  107. return palmTouchDetector.PalmTouchEvents;
  108. }
  109. private void processFrameUpdate()
  110. {
  111. Timer.start("processFrameUpdate");
  112. //read data from inputProvider
  113. Timer.start("readInputData");
  114. inputFrame = inputProvider.getInputFrame();
  115. Width = inputFrame.Width;
  116. Height = inputFrame.Height;
  117. Timer.stop("readInputData");
  118. //create depthImage
  119. Timer.start("createDepthImage");
  120. Int16 minDepth = Int16.MaxValue;
  121. Image<Gray, Int16> image = new Image<Gray, Int16>(Width, Height);
  122. for (int x = 0; x < Width; x++)
  123. {
  124. for (int y = 0; y < Height; y++)
  125. {
  126. Int16 depth = inputFrame.getDepthAt(x, y);
  127. image.Data[y, x, 0] = depth;
  128. if (depth < minDepth)
  129. minDepth = depth;
  130. }
  131. }
  132. depthImage = new DepthImage(image, minDepth);
  133. Timer.stop("createDepthImage");
  134. //create images
  135. Timer.start("createOtherImages");
  136. edgeImage = new EdgeImage(depthImage);
  137. touchImage = new TouchImage(Width, Height);
  138. fingerImage = new FingerImage(Width, Height);
  139. palmImage = new PalmImage(Width, Height);
  140. Timer.stop("createOtherImages");
  141. //detect fingers
  142. Timer.start("fingerDetection");
  143. fingerDetector = new FingerDetector(depthImage, edgeImage, fingerImage);
  144. Timer.stop("fingerDetection");
  145. //track fingers
  146. Timer.start("fingerTracking");
  147. fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, fingerImage);
  148. Timer.stop("fingerTracking");
  149. //detect palm
  150. Timer.start("palmDetection");
  151. palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector.Fingers, palmImage);
  152. Timer.stop("palmDetection");
  153. //detect touchEvents
  154. Timer.start("touchDetection");
  155. touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, touchImage);
  156. if (palmDetector.PalmQuad != null)
  157. palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad);
  158. Timer.stop("touchDetection");
  159. //track touchEvents
  160. Timer.start("touchTracking");
  161. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, touchImage);
  162. Timer.stop("touchTracking");
  163. Timer.stop("processFrameUpdate");
  164. }
  165. }
  166. }