VideoHandle.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Diagnostics;
  8. using bbiwarg.Utility;
  9. using bbiwarg.Detectors.FingerDetection;
  10. using bbiwarg.Detectors.PalmDetection;
  11. using bbiwarg.Detectors.TouchDetection;
  12. using bbiwarg.Detectors.HandDetection;
  13. using bbiwarg.Images;
  14. using bbiwarg.InputProviders;
  15. using Emgu.CV;
  16. using Emgu.CV.Structure;
  17. using bbiwarg.Graphics;
  18. namespace bbiwarg
  19. {
  20. class VideoHandle
  21. {
  22. private IInputProvider inputProvider;
  23. private InputFrame inputFrame;
  24. public int Width { get; private set; }
  25. public int Height { get; private set; }
  26. public int CurrentFrame
  27. {
  28. get
  29. {
  30. if (sourceIsMovie())
  31. return inputProvider.getCurrentMovieFrame();
  32. else
  33. return videoFrame;
  34. }
  35. }
  36. public OutputImage[] OutputImages { get; private set; }
  37. private DepthImage depthImage;
  38. private EdgeImage edgeImage;
  39. private ConfidenceImage confidenceImage;
  40. private FingerDetector fingerDetector;
  41. private HandDetector handDetector;
  42. private PalmDetector palmDetector;
  43. private TouchDetector touchDetector;
  44. private PalmTouchDetector palmTouchDetector;
  45. private FingerTracker fingerTracker;
  46. private TouchTracker touchTracker;
  47. private TouchEventVisualizer touchEventVisualizer;
  48. private int videoFrame = 0;
  49. public VideoHandle(IInputProvider inputProvider)
  50. {
  51. this.inputProvider = inputProvider;
  52. //initialize trackers
  53. touchTracker = new TouchTracker();
  54. fingerTracker = new FingerTracker();
  55. }
  56. public void start()
  57. {
  58. palmDetector = new PalmDetector();
  59. inputProvider.init();
  60. inputProvider.start();
  61. inputProvider.updateFrame();
  62. processFrameUpdate();
  63. }
  64. public void stop()
  65. {
  66. inputProvider.stop();
  67. }
  68. public bool sourceIsMovie()
  69. {
  70. return inputProvider.sourceIsMovie();
  71. }
  72. public void reversePlay()
  73. {
  74. inputProvider.reversePlay();
  75. }
  76. public void pauseMovie()
  77. {
  78. inputProvider.pauseMovie();
  79. }
  80. public void unpauseMovie()
  81. {
  82. inputProvider.unpauseMovie();
  83. }
  84. public void nextFrame()
  85. {
  86. if (inputProvider.isActive())
  87. {
  88. inputProvider.releaseFrame();
  89. inputProvider.updateFrame();
  90. processFrameUpdate();
  91. }
  92. else
  93. {
  94. inputProvider.stop();
  95. }
  96. videoFrame++;
  97. }
  98. public List<PalmTouchEvent> getPalmTouchEvents()
  99. {
  100. return palmTouchDetector.PalmTouchEvents;
  101. }
  102. private void processFrameUpdate()
  103. {
  104. Timer.start("processFrameUpdate");
  105. //read data from inputProvider
  106. Timer.start("readInputData");
  107. inputFrame = inputProvider.getInputFrame();
  108. Width = inputFrame.Width;
  109. Height = inputFrame.Height;
  110. Timer.stop("readInputData");
  111. //create output images
  112. Timer.start("createOutputImages");
  113. OutputImages = new OutputImage[Constants.OutputNumImages];
  114. for (int i = 0; i < Constants.OutputNumImages; i++) {
  115. OutputImages[i] = new OutputImage(Width, Height);
  116. }
  117. Timer.stop("createOutputImages");
  118. //create confidenceImage
  119. Timer.start("createConfidenceImage");
  120. Image<Gray, Int16> rawConfidenceImage = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawConfidenceData);
  121. confidenceImage = new ConfidenceImage(rawConfidenceImage);
  122. Timer.stop("createConfidenceImage");
  123. //create depthImage
  124. Timer.start("createDepthImage");
  125. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawDepthData);
  126. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  127. depthImage = new DepthImage(rawDepthImage);
  128. OutputImages[0].Image[0] = OutputImages[0].Image[1] = OutputImages[0].Image[2] = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image;
  129. Timer.stop("createDepthImage");
  130. // create edge image
  131. Timer.start("createEdgeImage");
  132. edgeImage = new EdgeImage(depthImage);
  133. OutputImages[1].Image[2] = edgeImage.Image.Mul(255);
  134. Timer.stop("createEdgeImage");
  135. //detect fingers
  136. Timer.start("fingerDetection");
  137. fingerDetector = new FingerDetector(depthImage, edgeImage, OutputImages[1]);
  138. Timer.stop("fingerDetection");
  139. //track fingers
  140. Timer.start("fingerTracking");
  141. fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, OutputImages[1]);
  142. Timer.stop("fingerTracking");
  143. //detect hands
  144. Timer.start("handDetection");
  145. handDetector = new HandDetector(depthImage, edgeImage, fingerDetector.Fingers, OutputImages[2]);
  146. Timer.stop("handDetection");
  147. //remove background noise
  148. Timer.start("removeBackground");
  149. OutputImages[3].Image[0] = OutputImages[3].Image[1] = OutputImages[3].Image[2] = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image.Or(255-handDetector.HandMask);
  150. Timer.stop("removeBackground");
  151. //detect palm
  152. Timer.start("palmDetection");
  153. if (CurrentFrame == 0)
  154. palmDetector.reset();
  155. palmDetector.findPalmQuad(OutputImages[3], handDetector.Hands);
  156. Timer.stop("palmDetection");
  157. //detect touchEvents
  158. Timer.start("touchDetection");
  159. touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, OutputImages[0]);
  160. if (palmDetector.PalmQuad != null)
  161. palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad);
  162. Timer.stop("touchDetection");
  163. //track touchEvents
  164. Timer.start("touchTracking");
  165. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, OutputImages[3]);
  166. Timer.stop("touchTracking");
  167. // touch event visualizer
  168. if (touchEventVisualizer == null)
  169. touchEventVisualizer = new TouchEventVisualizer(Width, Height);
  170. if (CurrentFrame == 0)
  171. touchEventVisualizer.Reset();
  172. if (palmTouchDetector != null)
  173. {
  174. foreach (PalmTouchEvent e in palmTouchDetector.PalmTouchEvents)
  175. {
  176. touchEventVisualizer.addPalmTouchEvent(e, CurrentFrame);
  177. }
  178. touchEventVisualizer.updateImage();
  179. OutputImages[4] = touchEventVisualizer.OutputImage;
  180. }
  181. // add borders
  182. for (int i = 0; i < Constants.OutputNumImages; i++)
  183. {
  184. OutputImages[i].drawRectangle(0, 0, Width - 1, Height - 1, Color.White);
  185. }
  186. Timer.stop("processFrameUpdate");
  187. }
  188. }
  189. }