PalmTracker.cs 3.4 KB

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