FingerTracker.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. {
  30. trackObjects(frameData.DetectedFingers);
  31. frameData.TrackedFingers = getCurrentObjectsWithState(TrackingState.Tracked);
  32. }
  33. /// <summary>
  34. /// Calculates the similarity [0-1] of a tracked Finger and a detected finger
  35. /// </summary>
  36. /// <param name="trackedFinger">the tracked finger</param>
  37. /// <param name="detectedFinger">the detected finger</param>
  38. /// <returns>the similarity between both fingers</returns>
  39. public override float calculateSimilarity(TrackedFinger trackedFinger, Finger detectedFinger)
  40. {
  41. Vector2D tip, hand;
  42. if (trackedFinger.shouldFingerBeReversed(detectedFinger))
  43. {
  44. tip = detectedFinger.HandPoint;
  45. hand = detectedFinger.TipPoint;
  46. }
  47. else
  48. {
  49. tip = detectedFinger.TipPoint;
  50. hand = detectedFinger.HandPoint;
  51. }
  52. float tipPointSimilarity = getPositionSimilarity(trackedFinger.TipPointPrediction, tip, Parameters.FingerTrackerMaxTipPointRelativeMove);
  53. float handPointSimilarity = getPositionSimilarity(trackedFinger.HandPointPrediction, hand, Parameters.FingerTrackerMaxHandPointRelativeMove);
  54. return tipPointSimilarity * handPointSimilarity;
  55. }
  56. /// <summary>
  57. /// Creates a TrackedFinger.
  58. /// </summary>
  59. /// <param name="detectedObject">The detected finger.</param>
  60. /// <returns>a TrackedFinger</returns>
  61. protected override TrackedFinger createTrackedObject(Finger detectedObject)
  62. {
  63. return new TrackedFinger(idPool.getNextUnusedID(), detectedObject, Parameters.FingerTrackerNumFramesDetectedUntilTracked, Parameters.FingerTrackerNumFramesLostUntilDeleted);
  64. }
  65. }
  66. }