InputHandler.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using BBIWARG.Images;
  2. using BBIWARG.Input.InputProviding;
  3. using BBIWARG.Recognition.FingerRecognition;
  4. using BBIWARG.Recognition.HandRecognition;
  5. using BBIWARG.Recognition.PalmRecognition;
  6. using BBIWARG.Recognition.TouchRecognition;
  7. using BBIWARG.Utility;
  8. using System;
  9. namespace BBIWARG.Input.InputHandling
  10. {
  11. /// <summary>
  12. /// signature for the event that a new frame is finished processing
  13. /// </summary>
  14. /// <param name="sender">sender of the event</param>
  15. /// <param name="e">event parameters</param>
  16. public delegate void NewProcessedFrameEventHandler(object sender, NewProcessedFrameEventArgs e);
  17. /// <summary>
  18. /// Handles new frames by coordinating and delegating the work to the image, detector and tracker classes.
  19. /// Also provides an event and the data for the tuio server and the graphical output.
  20. /// </summary>
  21. public class InputHandler
  22. {
  23. /// <summary>
  24. /// the finger detector
  25. /// </summary>
  26. private FingerDetector fingerDetector;
  27. /// <summary>
  28. /// the finger tracker
  29. /// </summary>
  30. private FingerTracker fingerTracker;
  31. /// <summary>
  32. /// the hand detector
  33. /// </summary>
  34. private HandDetector handDetector;
  35. /// <summary>
  36. /// the hand tracker
  37. /// </summary>
  38. private HandTracker handTracker;
  39. /// <summary>
  40. /// the input provider which provides the raw data
  41. /// </summary>
  42. private IInputProvider inputProvider;
  43. /// <summary>
  44. /// the palm detector
  45. /// </summary>
  46. private PalmDetector palmDetector;
  47. /// <summary>
  48. /// the palm tracker
  49. /// </summary>
  50. private PalmTracker palmTracker;
  51. /// <summary>
  52. /// set iff the source is a movie which is in the first frame again
  53. /// </summary>
  54. private bool resetFlag;
  55. /// <summary>
  56. /// the touch detector
  57. /// </summary>
  58. private TouchDetector touchDetector;
  59. /// <summary>
  60. /// the touch tracker
  61. /// </summary>
  62. private TouchTracker touchTracker;
  63. /// <summary>
  64. /// converts 2d and 3d coordinates into each other
  65. /// </summary>
  66. public CoordinateConverter CoordinateConverter { get; private set; }
  67. /// <summary>
  68. /// data for the current frame
  69. /// </summary>
  70. public FrameData FrameData { get; private set; }
  71. /// <summary>
  72. /// the size of all images
  73. /// </summary>
  74. public ImageSize ImageSize { get; private set; }
  75. /// <summary>
  76. /// event which occurs, when a new frame is finished processing
  77. /// </summary>
  78. public event NewProcessedFrameEventHandler NewProcessedFrameEvent;
  79. /// <summary>
  80. /// Constructs an InputHandler with an <see cref="InputProviderIisu"/>.
  81. /// </summary>
  82. /// <param name="inputProvider">the input provider</param>
  83. public InputHandler(IInputProvider inputProvider)
  84. {
  85. this.inputProvider = inputProvider;
  86. initialize();
  87. inputProvider.NewFrameEvent += handleNewFrame;
  88. VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
  89. if (videoInputProvider != null)
  90. videoInputProvider.MovieRestartedEvent += handleMovieRestart;
  91. }
  92. /// <summary>
  93. /// Handles a new frame event by processing the new frame using the image, detector and tracker classes.
  94. /// </summary>
  95. /// <param name="sender">the sender of the event</param>
  96. /// <param name="e">the arguments for the new frame event</param>
  97. public void handleNewFrame(object sender, NewFrameEventArgs e)
  98. {
  99. Timer.start("InputHandler.handleNewFrame");
  100. FrameData frameData = new FrameData();
  101. frameData.FrameID = e.FrameID;
  102. frameData.ImageSize = ImageSize;
  103. // reset flag
  104. frameData.ResetFlag = resetFlag;
  105. resetFlag = false;
  106. // depth image
  107. Timer.start("InputHandler.handleNewFrame::createDepthImage");
  108. frameData.DepthImage = new DepthImage(e.DepthImageRaw);
  109. Timer.stop("InputHandler.handleNewFrame::createDepthImage");
  110. // edge image
  111. Timer.start("InputHandler.handleNewFrame::createEdgeImage");
  112. frameData.EdgeImage = new EdgeImage(frameData.DepthImage);
  113. Timer.stop("InputHandler.handleNewFrame::createEdgeImage");
  114. // detect fingers
  115. Timer.start("InputHandler.handleNewFrame::detectFingers");
  116. fingerDetector.detectFingers(frameData);
  117. Timer.stop("InputHandler.handleNewFrame::detectFingers");
  118. // track fingers
  119. Timer.start("InputHandler.handleNewFrame::trackFingers");
  120. fingerTracker.trackFingers(frameData);
  121. Timer.stop("InputHandler.handleNewFrame::trackFingers");
  122. // detect hands
  123. Timer.start("InputHandler.handleNewFrame::detectHands");
  124. handDetector.detectHands(frameData);
  125. Timer.stop("InputHandler.handleNewFrame::detectHands");
  126. // track hands
  127. Timer.start("InputHandler.handleNewFrame::trackHands");
  128. handTracker.trackHands(frameData);
  129. Timer.stop("InputHandler.handleNewFrame::trackHands");
  130. // detect palms
  131. Timer.start("InputHandler.handleNewFrame::detectPalms");
  132. palmDetector.detectPalms(frameData);
  133. Timer.stop("InputHandler.handleNewFrame::detectPalms");
  134. // track palms
  135. Timer.start("InputHandler.handleNewFrame::trackPalms");
  136. palmTracker.trackPalms(frameData);
  137. Timer.stop("InputHandler.handleNewFrame::trackPalms");
  138. // detect touches
  139. Timer.start("InputHandler.handleNewFrame::detectTouches");
  140. touchDetector.detectTouches(frameData);
  141. Timer.stop("InputHandler.handleNewFrame::detectTouches");
  142. // track touches
  143. Timer.start("InputHandler.handleNewFrame::trackTouches");
  144. touchTracker.trackTouches(frameData);
  145. Timer.stop("InputHandler.handleNewFrame::trackTouches");
  146. Timer.start("InputHandler.handleNewFrame::exportResults");
  147. FrameData = frameData;
  148. if (NewProcessedFrameEvent != null)
  149. NewProcessedFrameEvent(this, new NewProcessedFrameEventArgs(frameData));
  150. Timer.stop("InputHandler.handleNewFrame::exportResults");
  151. Timer.stop("InputHandler.handleNewFrame");
  152. if (Parameters.LoggerTimerOutputEnabled)
  153. Timer.outputAll();
  154. }
  155. /// <summary>
  156. /// Resets the trackers.
  157. /// </summary>
  158. public void reset()
  159. {
  160. touchTracker.reset();
  161. handTracker.reset();
  162. palmTracker.reset();
  163. fingerTracker.reset();
  164. }
  165. /// <summary>
  166. /// Handles the event of a restart of the movie.
  167. /// </summary>
  168. /// <param name="sender">the sender of the event</param>
  169. /// <param name="e">the event arguments</param>
  170. private void handleMovieRestart(object sender, EventArgs e)
  171. {
  172. reset();
  173. resetFlag = true;
  174. }
  175. /// <summary>
  176. /// Initializes all components.
  177. /// </summary>
  178. private void initialize()
  179. {
  180. ImageSize = new ImageSize(inputProvider.ImageWidth, inputProvider.ImageHeight);
  181. CoordinateConverter = new CoordinateConverter(ImageSize, inputProvider.FieldOfViewHorizontal, inputProvider.FieldOfViewVertical);
  182. resetFlag = false;
  183. fingerDetector = new FingerDetector(CoordinateConverter);
  184. handDetector = new HandDetector();
  185. palmDetector = new PalmDetector();
  186. touchDetector = new TouchDetector();
  187. fingerTracker = new FingerTracker(ImageSize);
  188. handTracker = new HandTracker(ImageSize);
  189. palmTracker = new PalmTracker(ImageSize);
  190. touchTracker = new TouchTracker(ImageSize);
  191. }
  192. }
  193. }