FingerTracker.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using bbiwarg.Recognition.Tracking;
  7. using bbiwarg.Utility;
  8. using bbiwarg.Input.InputHandling;
  9. namespace bbiwarg.Recognition.FingerRecognition
  10. {
  11. /// <summary>
  12. /// Keeps track of fingers over a period of time.
  13. /// </summary>
  14. class FingerTracker : Tracker<Finger, TrackedFinger>
  15. {
  16. /// <summary>
  17. /// Initializes a new instance of the FingerTracker class.
  18. /// </summary>
  19. /// <param name="imageSize">Size of the input image.</param>
  20. public FingerTracker(ImageSize imageSize)
  21. : base(imageSize)
  22. {
  23. }
  24. /// <summary>
  25. /// Updates the TrackedFingers with the detected fingers in the current frame and stores the tracked fingers.
  26. /// </summary>
  27. /// <param name="frameData">the current frame</param>
  28. public void trackFingers(FrameData frameData) {
  29. trackObjects(frameData.DetectedFingers);
  30. frameData.TrackedFingers = getCurrentObjectsWithState(TrackingState.Tracked);
  31. }
  32. /// <summary>
  33. /// Calculates the similarity [0-1] of a tracked Finger and a detected finger
  34. /// </summary>
  35. /// <param name="trackedFinger">the tracked finger</param>
  36. /// <param name="detectedFinger">the detected finger</param>
  37. /// <returns>the similarity between both fingers</returns>
  38. public override float calculateSimilarity(TrackedFinger trackedFinger, Finger detectedFinger)
  39. {
  40. Vector2D tip, hand;
  41. if (trackedFinger.shouldFingerBeReversed(detectedFinger))
  42. {
  43. tip = detectedFinger.HandPoint;
  44. hand = detectedFinger.TipPoint;
  45. }
  46. else
  47. {
  48. tip = detectedFinger.TipPoint;
  49. hand = detectedFinger.HandPoint;
  50. }
  51. float tipPointSimilarity = getPositionSimilarity(trackedFinger.TipPointPrediction, tip, Parameters.FingerTrackerMaxTipPointRelativeMove);
  52. float handPointSimilarity = getPositionSimilarity(trackedFinger.HandPointPrediction, hand, Parameters.FingerTrackerMaxHandPointRelativeMove);
  53. return tipPointSimilarity * handPointSimilarity;
  54. }
  55. /// <summary>
  56. /// Creates a TrackedFinger.
  57. /// </summary>
  58. /// <param name="detectedObject">The detected finger.</param>
  59. /// <returns>a TrackedFinger</returns>
  60. protected override TrackedFinger createTrackedObject(Finger detectedObject)
  61. {
  62. return new TrackedFinger(idPool.getNextUnusedID(), detectedObject, Parameters.FingerTrackerNumFramesDetectedUntilTracked, Parameters.FingerTrackerNumFramesLostUntilDeleted);
  63. }
  64. }
  65. }