VideoHandle.cs 7.8 KB

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