VideoHandle.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.Recognition.FingerRecognition;
  10. using bbiwarg.Recognition.PalmRecognition;
  11. using bbiwarg.Recognition.TouchRecognition;
  12. using bbiwarg.Recognition.HandRecognition;
  13. using bbiwarg.Recognition.Tracking;
  14. using bbiwarg.Images;
  15. using bbiwarg.InputProviders;
  16. using Emgu.CV;
  17. using Emgu.CV.Structure;
  18. using bbiwarg.Graphics;
  19. using bbiwarg.Server;
  20. namespace bbiwarg
  21. {
  22. class VideoHandle
  23. {
  24. private IInputProvider inputProvider;
  25. private InputFrame inputFrame;
  26. public int Width { get; private set; }
  27. public int Height { get; private set; }
  28. public int CurrentFrame
  29. {
  30. get
  31. {
  32. if (sourceIsMovie())
  33. return inputProvider.getCurrentMovieFrame();
  34. else
  35. return videoFrame;
  36. }
  37. }
  38. private int lastFrame = int.MaxValue;
  39. public OutputImage[] OutputImages { get; private set; }
  40. private DepthImage depthImage;
  41. private EdgeImage edgeImage;
  42. private ConfidenceImage confidenceImage;
  43. private FingerDetector fingerDetector;
  44. private HandDetector handDetector;
  45. private PalmDetector palmDetector;
  46. private TouchDetector touchDetector;
  47. private FingerTracker fingerTracker;
  48. private TouchTracker touchTracker;
  49. private TouchEventVisualizer touchEventVisualizer;
  50. private TuioCommunicator tuioCommunicator;
  51. private int videoFrame = 0;
  52. public VideoHandle(IInputProvider inputProvider)
  53. {
  54. this.inputProvider = inputProvider;
  55. }
  56. public void start()
  57. {
  58. inputProvider.init();
  59. inputProvider.start();
  60. inputProvider.updateFrame();
  61. initTrackers();
  62. processFrameUpdate();
  63. }
  64. public void stop()
  65. {
  66. inputProvider.stop();
  67. tuioCommunicator.close();
  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. private void initTrackers()
  100. {
  101. palmDetector = new PalmDetector();
  102. fingerTracker = new FingerTracker();
  103. touchTracker = new TouchTracker();
  104. //register touchEventVisualizer to touchTracker
  105. touchEventVisualizer = new TouchEventVisualizer(Constants.ImageWidth, Constants.ImageHeight);
  106. touchTracker.PalmTouchDown += touchEventVisualizer.touchDown;
  107. touchTracker.PalmTouchMove += touchEventVisualizer.touchMove;
  108. touchTracker.PalmTouchUp += touchEventVisualizer.touchUp;
  109. //register tuiCommunicator to touchTracker
  110. tuioCommunicator = new TuioCommunicator(Constants.TuioIP, Constants.TuioPort);
  111. touchTracker.PalmTouchDown += tuioCommunicator.touchDown;
  112. touchTracker.PalmTouchMove += tuioCommunicator.touchMove;
  113. touchTracker.PalmTouchUp += tuioCommunicator.touchUp;
  114. //register
  115. }
  116. private void processFrameUpdate()
  117. {
  118. Timer.start("processFrameUpdate");
  119. Logger.CurrentFrame = CurrentFrame;
  120. bool newStarted = (lastFrame - CurrentFrame >= 20);
  121. tuioCommunicator.initFrame();
  122. if (newStarted) {
  123. //reset trackers
  124. touchTracker.reset();
  125. fingerTracker.reset();
  126. touchEventVisualizer.reset();
  127. tuioCommunicator.reset();
  128. }
  129. //read data from inputProvider
  130. Timer.start("readInputData");
  131. inputFrame = inputProvider.getInputFrame();
  132. Width = inputFrame.Width;
  133. Height = inputFrame.Height;
  134. Timer.stop("readInputData");
  135. //create output images
  136. Timer.start("createOutputImages");
  137. OutputImages = new OutputImage[Constants.OutputNumImages];
  138. for (int i = 0; i < Constants.OutputNumImages; i++) {
  139. OutputImages[i] = new OutputImage(Width, Height);
  140. }
  141. Timer.stop("createOutputImages");
  142. //create confidenceImage
  143. Timer.start("createConfidenceImage");
  144. Image<Gray, Int16> rawConfidenceImage = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawConfidenceData);
  145. confidenceImage = new ConfidenceImage(rawConfidenceImage);
  146. Timer.stop("createConfidenceImage");
  147. //create depthImage
  148. Timer.start("createDepthImage");
  149. Image<Gray, Int16> rawDepthImage = new Image<Gray, Int16>(Width, Height, Width * 2, inputFrame.RawDepthData);
  150. rawDepthImage = rawDepthImage.Or((1 - confidenceImage.Mask).Convert<Gray, Int16>().Mul(Int16.MaxValue));
  151. depthImage = new DepthImage(rawDepthImage);
  152. OutputImages[0].Image[0] = OutputImages[0].Image[1] = OutputImages[0].Image[2] = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image;
  153. Timer.stop("createDepthImage");
  154. // create edge image
  155. Timer.start("createEdgeImage");
  156. edgeImage = new EdgeImage(depthImage);
  157. OutputImages[1].Image[2] = edgeImage.Image.Mul(255);
  158. Timer.stop("createEdgeImage");
  159. //detect fingers
  160. Timer.start("fingerDetection");
  161. fingerDetector = new FingerDetector(depthImage, edgeImage, OutputImages[1]);
  162. Timer.stop("fingerDetection");
  163. //track fingers
  164. Timer.start("fingerTracking");
  165. fingerTracker.updateFrame(fingerDetector.Fingers);
  166. foreach (Finger f in fingerTracker.Fingers) {
  167. OutputImages[1].drawText(f.MidPoint.IntX, f.MidPoint.IntY, f.TrackID.ToString(), Constants.FingerIDColor);
  168. }
  169. Timer.stop("fingerTracking");
  170. //detect hands
  171. Timer.start("handDetection");
  172. handDetector = new HandDetector(depthImage, edgeImage, fingerDetector.Fingers, OutputImages[2]);
  173. Timer.stop("handDetection");
  174. //remove background noise
  175. Timer.start("removeBackground");
  176. OutputImages[3].Image[0] = OutputImages[3].Image[1] = OutputImages[3].Image[2] = (depthImage.MaxDepth - depthImage.MinDepth) - depthImage.Image.Or(255-handDetector.HandMask);
  177. Timer.stop("removeBackground");
  178. //detect palm
  179. Timer.start("palmDetection");
  180. if (newStarted)
  181. palmDetector.reset();
  182. palmDetector.findPalmQuad(OutputImages[3], handDetector.Hands, handDetector.HandMask);
  183. Timer.stop("palmDetection");
  184. //detect touchEvents
  185. Timer.start("touchDetection");
  186. touchDetector = new TouchDetector(fingerTracker.Fingers, palmDetector.PalmQuad, depthImage, OutputImages[0]);
  187. Timer.stop("touchDetection");
  188. //track touchEvents
  189. Timer.start("touchTracking");
  190. touchTracker.updateFrame(touchDetector.TouchEvents);
  191. foreach(TouchEvent te in touchTracker.TouchEvents)
  192. {
  193. OutputImages[3].fillCircle(te.Position.IntX, te.Position.IntY, 5, Constants.TouchEventTrackedColor);
  194. }
  195. Timer.stop("touchTracking");
  196. //touchEventVisualizer
  197. touchEventVisualizer.updateImage();
  198. OutputImages[4] = touchEventVisualizer.OutputImage;
  199. // add borders
  200. for (int i = 0; i < Constants.OutputNumImages; i++)
  201. {
  202. OutputImages[i].drawRectangle(0, 0, Width - 1, Height - 1, Color.White);
  203. }
  204. lastFrame = CurrentFrame;
  205. tuioCommunicator.commitFrame();
  206. Timer.stop("processFrameUpdate");
  207. }
  208. }
  209. }