InputHandler.cs 9.0 KB

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