VideoHandle.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 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. int numImages = 5;
  114. OutputImages = new OutputImage[numImages];
  115. for (int i = 0; i < numImages; i++) {
  116. OutputImages[i] = new OutputImage(Width, Height);
  117. }
  118. Timer.stop("createOutputImages");
  119. //create depthImage
  120. Timer.start("createDepthImage");
  121. Image<Gray, Int16> image = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawDepthData);
  122. depthImage = new DepthImage(image, OutputImages[0]);
  123. Image<Gray, byte> tmpDepth = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image;
  124. OutputImages[0].Image[0] = OutputImages[0].Image[1] = OutputImages[0].Image[2] = tmpDepth;
  125. Timer.stop("createDepthImage");
  126. // create edge image
  127. Timer.start("createEdgeImage");
  128. edgeImage = new EdgeImage(depthImage);
  129. OutputImages[1].Image[2] = edgeImage.Image.Mul(255);
  130. Timer.stop("createEdgeImage");
  131. //detect fingers
  132. Timer.start("fingerDetection");
  133. fingerDetector = new FingerDetector(depthImage, edgeImage, OutputImages[1]);
  134. Timer.stop("fingerDetection");
  135. //track fingers
  136. Timer.start("fingerTracking");
  137. fingerTracker.setDetectedTouchEventsThisFrame(fingerDetector.Fingers, OutputImages[1]);
  138. Timer.stop("fingerTracking");
  139. //detect hands
  140. Timer.start("handDetection");
  141. handDetector = new HandDetector(depthImage, edgeImage, fingerDetector.Fingers, OutputImages[2]);
  142. Timer.stop("handDetection");
  143. //remove background noise
  144. Timer.start("removeBackground");
  145. depthImage.removeBackground(handDetector.Hands);
  146. edgeImage = new EdgeImage(depthImage);
  147. OutputImages[3].Image[0] = OutputImages[3].Image[1] = OutputImages[3].Image[2] = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image;
  148. Timer.stop("removeBackground");
  149. //detect palm
  150. Timer.start("palmDetection");
  151. if (CurrentFrame == 0)
  152. palmDetector.reset();
  153. palmDetector.findPalmQuad(depthImage, edgeImage, OutputImages[3], fingerDetector.Fingers);
  154. Timer.stop("palmDetection");
  155. //detect touchEvents
  156. Timer.start("touchDetection");
  157. touchDetector = new TouchDetector(fingerTracker.TrackedFingers, depthImage, OutputImages[0]);
  158. if (palmDetector.PalmQuad != null)
  159. palmTouchDetector = new PalmTouchDetector(touchDetector.TouchEvents, palmDetector.PalmQuad);
  160. Timer.stop("touchDetection");
  161. //track touchEvents
  162. Timer.start("touchTracking");
  163. touchTracker.setDetectedTouchEventsThisFrame(touchDetector.TouchEvents, OutputImages[3]);
  164. Timer.stop("touchTracking");
  165. // touch event visualizer
  166. if (touchEventVisualizer == null)
  167. touchEventVisualizer = new TouchEventVisualizer(Width, Height);
  168. if (CurrentFrame == 0)
  169. touchEventVisualizer.Reset();
  170. if (palmTouchDetector != null)
  171. {
  172. foreach (PalmTouchEvent e in palmTouchDetector.PalmTouchEvents)
  173. touchEventVisualizer.addPalmTouchEvent(e, CurrentFrame);
  174. touchEventVisualizer.updateImage();
  175. OutputImages[4] = touchEventVisualizer.OutputImage;
  176. }
  177. // add borders
  178. for (int i = 0; i < numImages; i++)
  179. {
  180. OutputImages[i].drawRectangle(0, 0, Width - 1, Height - 1, Color.White);
  181. }
  182. OutputImages = new OutputImage[] {OutputImages[0], OutputImages[1], OutputImages[2], OutputImages[3], OutputImages[4]};
  183. //palmDetector.i1, palmDetector.i2, palmDetector.i3, palmDetector.i5, palmDetector.i6, palmDetector.i7, palmDetector.i8, palmDetector.i9};
  184. Timer.stop("processFrameUpdate");
  185. }
  186. }
  187. }