VideoHandle.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 int getCurrentMovieFrame()
  53. {
  54. return inputProvider.getCurrentMovieFrame();
  55. }
  56. public bool sourceIsMovie()
  57. {
  58. return inputProvider.sourceIsMovie();
  59. }
  60. public void reversePlay()
  61. {
  62. inputProvider.reversePlay();
  63. }
  64. public void pauseMovie()
  65. {
  66. inputProvider.pauseMovie();
  67. }
  68. public void unpauseMovie()
  69. {
  70. inputProvider.unpauseMovie();
  71. }
  72. public void nextFrame()
  73. {
  74. if (inputProvider.isActive())
  75. {
  76. inputProvider.releaseFrame();
  77. inputProvider.updateFrame();
  78. processFrameUpdate();
  79. }
  80. else
  81. {
  82. inputProvider.stop();
  83. }
  84. }
  85. public Int16 getDepthAt(int x, int y)
  86. {
  87. return depthImage.getDepthAt(x, y);
  88. }
  89. public float getRelativeDepth(int x, int y)
  90. {
  91. return depthImage.getRelativeDepthAt(x, y);
  92. }
  93. public bool isEdgeAt(int x, int y)
  94. {
  95. return edgeImage.isEdgeAt(x, y);
  96. }
  97. public FingerImageState getFingerImageStateAt(int x, int y)
  98. {
  99. return fingerImage.getStateAt(x, y);
  100. }
  101. public PalmImageState getPalmImageStateAt(int x, int y)
  102. {
  103. return palmImage.getStateAt(x, y);
  104. }
  105. public TouchImageState getTouchImageStateAt(int x, int y)
  106. {
  107. return touchImage.getStateAt(x, y);
  108. }
  109. public List<PalmTouchEvent> getPalmTouchEvents()
  110. {
  111. return palmTouchDetector.PalmTouchEvents;
  112. }
  113. private void processFrameUpdate()
  114. {
  115. Timer.start("processFrameUpdate");
  116. //read data from inputProvider
  117. Timer.start("readInputData");
  118. inputFrame = inputProvider.getInputFrame();
  119. Width = inputFrame.Width;
  120. Height = inputFrame.Height;
  121. Timer.stop("readInputData");
  122. //create depthImage
  123. Timer.start("createDepthImage");
  124. Image<Gray, Int16> image = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawDepthData);
  125. depthImage = new DepthImage(image);
  126. Timer.stop("createDepthImage");
  127. //create images
  128. Timer.start("createOtherImages");
  129. edgeImage = new EdgeImage(depthImage);
  130. touchImage = new TouchImage(Width, Height);
  131. fingerImage = new FingerImage(Width, Height);
  132. palmImage = new PalmImage(Width, Height);
  133. Timer.stop("createOtherImages");
  134. //detect fingers
  135. Timer.start("fingerDetection");
  136. fingerDetector = new FingerDetector(depthImage, edgeImage, fingerImage);
  137. Timer.stop("fingerDetection");
  138. //track fingers
  139. Timer.start("fingerTracking");
  140. fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, fingerImage);
  141. Timer.stop("fingerTracking");
  142. //detect palm
  143. Timer.start("palmDetection");
  144. palmDetector = new PalmDetector(depthImage, edgeImage, fingerDetector.Fingers, palmImage);
  145. Timer.stop("palmDetection");
  146. //detect touchEvents
  147. Timer.start("touchDetection");
  148. touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, touchImage);
  149. if (palmDetector.PalmQuad != null)
  150. palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad);
  151. Timer.stop("touchDetection");
  152. //track touchEvents
  153. Timer.start("touchTracking");
  154. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, touchImage);
  155. Timer.stop("touchTracking");
  156. Timer.stop("processFrameUpdate");
  157. }
  158. }
  159. }