PalmTracker.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using BBIWARG.Input.InputHandling;
  2. using BBIWARG.Recognition.Tracking;
  3. using BBIWARG.Utility;
  4. using System.Collections.Generic;
  5. namespace BBIWARG.Recognition.PalmRecognition
  6. {
  7. /// <summary>
  8. /// Keeps track of palms over a period of time.
  9. /// </summary>
  10. internal class PalmTracker : Tracker<Palm, TrackedPalm>
  11. {
  12. /// <summary>
  13. /// Initializes a new instance of the PalmTracker class.
  14. /// </summary>
  15. /// <param name="imageSize">Size of the input image.</param>
  16. public PalmTracker(ImageSize imageSize)
  17. : base(imageSize)
  18. {
  19. }
  20. /// <summary>
  21. /// Calculates the similarity [0-1] of a tracked palm and a detected palm
  22. /// </summary>
  23. /// <param name="trackedPalm">the tracked palm</param>
  24. /// <param name="detectedPalm">the detected palm</param>
  25. /// <returns>the similarity</returns>
  26. public override float calculateSimilarity(TrackedPalm trackedPalm, Palm detectedPalm)
  27. {
  28. float handSimilarity = (detectedPalm.Hand.TrackID == trackedPalm.LastObject.Hand.TrackID) ? 1 : 0;
  29. float wristUpperSimilarity = getPositionSimilarity(trackedPalm.WristUpperPrediction, detectedPalm.WristUpper, Parameters.PalmTrackerMaxWristUpperRelativeMove);
  30. float wristLowerSimilarity = getPositionSimilarity(trackedPalm.WristLowerPrediction, detectedPalm.WristLower, Parameters.PalmTrackerMaxWristLowerRelativeMove);
  31. float fingersUpperSimilarity = getPositionSimilarity(trackedPalm.FingersUpperPrediction, detectedPalm.FingersUpper, Parameters.PalmTrackerMaxFingersUpperRelativeMove);
  32. float fingersLowerSimilarity = getPositionSimilarity(trackedPalm.FingersLowerPrediction, detectedPalm.FingersLower, Parameters.PalmTrackerMaxFingersLowerRelativeMove);
  33. return handSimilarity * wristUpperSimilarity * wristLowerSimilarity * fingersUpperSimilarity * fingersLowerSimilarity;
  34. }
  35. /// <summary>
  36. /// Updates the tracked palms with the detected palms in the current frame and stores the (optimized) results in frameData.trackedPalms
  37. /// </summary>
  38. /// <param name="frameData">the current frame</param>
  39. public void trackPalms(FrameData frameData)
  40. {
  41. trackObjects(frameData.DetectedPalms);
  42. frameData.TrackedPalms = getOptimizedPalms();
  43. }
  44. /// <summary>
  45. /// Creates a TrackedPalm
  46. /// </summary>
  47. /// <param name="detectedPalm">the detected palm</param>
  48. /// <returns>a new TrackedPalm</returns>
  49. protected override TrackedPalm createTrackedObject(Palm detectedPalm)
  50. {
  51. return new TrackedPalm(idPool.getNextUnusedID(), detectedPalm, Parameters.PalmTrackerNumFramesDetectedUntilTracked, Parameters.PalmTrackerNumFramesLostUntilDeleted);
  52. }
  53. /// <summary>
  54. /// Gets all optimized representations of all tracked palms.
  55. /// </summary>
  56. /// <returns>all optimized tracked palms</returns>
  57. private List<Palm> getOptimizedPalms()
  58. {
  59. List<Palm> optimizedPalms = new List<Palm>();
  60. foreach (TrackedPalm tp in TrackedObjects)
  61. {
  62. if (tp.CurrentState == TrackingState.Tracked)
  63. optimizedPalms.Add(tp.OptimizedPalm);
  64. }
  65. return optimizedPalms;
  66. }
  67. }
  68. }