123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- using bbiwarg.Images;
- using bbiwarg.Input.InputProviding;
- using bbiwarg.Recognition.FingerRecognition;
- using bbiwarg.Recognition.HandRecognition;
- using bbiwarg.Recognition.PalmRecognition;
- using bbiwarg.Recognition.TouchRecognition;
- using bbiwarg.Utility;
- using System;
- namespace bbiwarg.Input.InputHandling
- {
- /// <summary>
- /// signature for the event that a new frame is finished processing
- /// </summary>
- /// <param name="sender">sender of the event</param>
- /// <param name="e">event parameters</param>
- public delegate void NewProcessedFrameEventHandler(object sender, NewProcessedFrameEventArgs e);
- /// <summary>
- /// Encapsulates the arguments for the event of finishing to process a new frame.
- /// </summary>
- public class NewProcessedFrameEventArgs: EventArgs
- {
- /// <summary>
- /// the data of the processed frame
- /// </summary>
- public FrameData FrameData { get; private set; }
- /// <summary>
- /// Creates a new NewProcessedFrameEventArgs with given frame data.
- /// </summary>
- /// <param name="frameData">frame data</param>
- public NewProcessedFrameEventArgs(FrameData frameData)
- {
- FrameData = frameData;
- }
- }
- /// <summary>
- /// Handles new frames by coordinating and delegating the work to the image, detector and tracker classes.
- /// Also provides an event and the data for the tuio server and the graphical output.
- /// </summary>
- public class InputHandler
- {
- /// <summary>
- /// the input provider which provides the raw data
- /// </summary>
- private InputProvider inputProvider;
- /// <summary>
- /// set iff the source is a movie which is in the first frame again
- /// </summary>
- private bool resetFlag;
- /// <summary>
- /// the finger detector
- /// </summary>
- private FingerDetector fingerDetector;
- /// <summary>
- /// the hand detector
- /// </summary>
- private HandDetector handDetector;
- /// <summary>
- /// the palm detector
- /// </summary>
- private PalmDetector palmDetector;
- /// <summary>
- /// the touch detector
- /// </summary>
- private TouchDetector touchDetector;
- /// <summary>
- /// the finger tracker
- /// </summary>
- private FingerTracker fingerTracker;
- /// <summary>
- /// the hand tracker
- /// </summary>
- private HandTracker handTracker;
- /// <summary>
- /// the palm tracker
- /// </summary>
- private PalmTracker palmTracker;
- /// <summary>
- /// the touch tracker
- /// </summary>
- private TouchTracker touchTracker;
- /// <summary>
- /// the size of all images
- /// </summary>
- public ImageSize ImageSize { get; private set; }
- /// <summary>
- /// converts 2d and 3d coordinates into each other
- /// </summary>
- public CoordinateConverter CoordinateConverter { get; private set; }
- /// <summary>
- /// data for the current frame
- /// </summary>
- public FrameData FrameData { get; private set; }
- /// <summary>
- /// event which occures, when a new frame is finished processing
- /// </summary>
- public event NewProcessedFrameEventHandler NewProcessedFrameEvent;
- /// <summary>
- /// Constructs an InputHandler with an <see cref="InputProvider"/>.
- /// </summary>
- /// <param name="inputProvider">the input provider</param>
- public InputHandler(InputProvider inputProvider)
- {
- this.inputProvider = inputProvider;
- initialize();
- inputProvider.NewFrameEvent += handleNewFrame;
- VideoInputProvider videoInputProvider = inputProvider as VideoInputProvider;
- if (videoInputProvider != null)
- videoInputProvider.MovieRestartedEvent += handleMovieRestart;
- }
- /// <summary>
- /// Initializes all components.
- /// </summary>
- private void initialize()
- {
- ImageSize = new ImageSize(inputProvider.ImageWidth, inputProvider.ImageHeight);
- CoordinateConverter = new CoordinateConverter(ImageSize, inputProvider.HFOV, inputProvider.VFOV);
- resetFlag = false;
- fingerDetector = new FingerDetector(CoordinateConverter);
- handDetector = new HandDetector();
- palmDetector = new PalmDetector();
- touchDetector = new TouchDetector();
- fingerTracker = new FingerTracker(ImageSize);
- handTracker = new HandTracker(ImageSize);
- palmTracker = new PalmTracker(ImageSize);
- touchTracker = new TouchTracker(ImageSize);
- }
- /// <summary>
- /// Resets the trackers.
- /// </summary>
- public void reset()
- {
- touchTracker.reset();
- handTracker.reset();
- palmTracker.reset();
- fingerTracker.reset();
- }
- /// <summary>
- /// Handles a new frame event by processing the new frame using the image, detector and tracker classes.
- /// </summary>
- /// <param name="sender">the sender of the event</param>
- /// <param name="e">the arguments for the new frame event</param>
- public void handleNewFrame(object sender, NewFrameEventArgs e)
- {
- Timer.start("InputHandler.handleNewFrame");
- FrameData frameData = new FrameData();
- frameData.FrameID = e.FrameID;
- frameData.ImageSize = ImageSize;
- // reset flag
- frameData.ResetFlag = resetFlag;
- resetFlag = false;
- // confidence image
- Timer.start("InputHandler.handleNewFrame::createConfidenceImage");
- frameData.ConfidenceImage = new ConfidenceImage(e.RawConfidenceData, ImageSize);
- Timer.stop("InputHandler.handleNewFrame::createConfidenceImage");
- // depth image
- Timer.start("InputHandler.handleNewFrame::createDepthImage");
- frameData.DepthImage = new DepthImage(e.RawDepthData, ImageSize, frameData.ConfidenceImage);
- Timer.stop("InputHandler.handleNewFrame::createDepthImage");
- // edge image
- Timer.start("InputHandler.handleNewFrame::createEdgeImage");
- frameData.EdgeImage = new EdgeImage(frameData.DepthImage);
- Timer.stop("InputHandler.handleNewFrame::createEdgeImage");
- // detect fingers
- Timer.start("InputHandler.handleNewFrame::detectFingers");
- fingerDetector.detectFingers(frameData);
- Timer.stop("InputHandler.handleNewFrame::detectFingers");
- // track fingers
- Timer.start("InputHandler.handleNewFrame::trackFingers");
- fingerTracker.trackFingers(frameData);
- Timer.stop("InputHandler.handleNewFrame::trackFingers");
- // detect hands
- Timer.start("InputHandler.handleNewFrame::detectHands");
- handDetector.detectHands(frameData);
- Timer.stop("InputHandler.handleNewFrame::detectHands");
- // track hands
- Timer.start("InputHandler.handleNewFrame::trackHands");
- handTracker.trackHands(frameData);
- Timer.stop("InputHandler.handleNewFrame::trackHands");
- // detect palms
- Timer.start("InputHandler.handleNewFrame::detectPalms");
- palmDetector.detectPalms(frameData);
- Timer.stop("InputHandler.handleNewFrame::detectPalms");
- // track palms
- Timer.start("InputHandler.handleNewFrame::trackPalms");
- palmTracker.trackPalms(frameData);
- Timer.stop("InputHandler.handleNewFrame::trackPalms");
- // detect touches
- Timer.start("InputHandler.handleNewFrame::detectTouches");
- touchDetector.detectTouches(frameData);
- Timer.stop("InputHandler.handleNewFrame::detectTouches");
- //track touches
- Timer.start("InputHandler.handleNewFrame::trackTouches");
- touchTracker.trackTouches(frameData);
- Timer.stop("InputHandler.handleNewFrame::trackTouches");
- Timer.start("InputHandler.handleNewFrame::exportResults");
- FrameData = frameData;
- if (NewProcessedFrameEvent != null)
- NewProcessedFrameEvent(this, new NewProcessedFrameEventArgs(frameData));
- Timer.stop("InputHandler.handleNewFrame::exportResults");
- Timer.stop("InputHandler.handleNewFrame");
- if (Parameters.LoggerTimerOutputEnabled)
- Timer.outputAll();
- }
- /// <summary>
- /// Handles the event of a restart of the movie.
- /// </summary>
- /// <param name="sender">the sender of the event</param>
- /// <param name="e">the event arguments</param>
- private void handleMovieRestart(object sender, EventArgs e)
- {
- reset();
- resetFlag = true;
- }
- }
- }
|