TrackedFinger.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. namespace bbiwarg.Recognition.FingerRecognition
  9. {
  10. class TrackedFinger : TrackedObject<Finger>
  11. {
  12. private Kalman2DPositionFilter tipPointKalman;
  13. private Kalman2DPositionFilter handPointKalman;
  14. private List<Vector2D> lastRawDirections;
  15. public Vector2D TipPointPrediction { get { return tipPointKalman.getPrediction(); } }
  16. public Vector2D HandPointPrediction { get { return handPointKalman.getPrediction(); } }
  17. public Vector2D MeanDirection { get { return Vector2D.mean(lastRawDirections); } }
  18. public TrackedFinger(int id, Finger detectedFinger, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  19. : base(id, detectedFinger, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  20. {
  21. tipPointKalman = new Kalman2DPositionFilter(Parameters.FingermXX, Parameters.FingermXY, Parameters.FingermYY);
  22. tipPointKalman.setInitialPosition(detectedFinger.TipPoint);
  23. handPointKalman = new Kalman2DPositionFilter(Parameters.FingermXX, Parameters.FingermXY, Parameters.FingermYY);
  24. handPointKalman.setInitialPosition(detectedFinger.HandPoint);
  25. lastRawDirections = new List<Vector2D>();
  26. logStateChange();
  27. }
  28. public override void updateFrame(Finger detectedFinger)
  29. {
  30. base.updateFrame(detectedFinger);
  31. if (NumFramesInCurrentState == 1)
  32. logStateChange();
  33. if (detectedFinger != null)
  34. {
  35. Vector2D rawDirection = detectedFinger.Direction;
  36. if (shouldFingerBeReversed(detectedFinger))
  37. detectedFinger.reverse();
  38. tipPointKalman.getCorrectedPosition(detectedFinger.TipPoint);
  39. handPointKalman.getCorrectedPosition(detectedFinger.HandPoint);
  40. lastRawDirections.Add(rawDirection);
  41. if (lastRawDirections.Count == Parameters.FingerTrackerNumDirectionsForMeanDirection)
  42. lastRawDirections.RemoveAt(0);
  43. }
  44. }
  45. public override float calculateSimilarity(Finger detectedFinger)
  46. {
  47. Vector2D tip, hand;
  48. if (shouldFingerBeReversed(detectedFinger))
  49. {
  50. tip = detectedFinger.HandPoint;
  51. hand = detectedFinger.TipPoint;
  52. }
  53. else
  54. {
  55. tip = detectedFinger.TipPoint;
  56. hand = detectedFinger.HandPoint;
  57. }
  58. float tipPointSimilarity = getPositionSimilarity(TipPointPrediction, tip, Parameters.FingerTrackerMaxTipPointMove);
  59. float handPointSimilarity = getPositionSimilarity(HandPointPrediction, hand, Parameters.FingerTrackerMaxHandPointMove);
  60. return tipPointSimilarity * handPointSimilarity;
  61. }
  62. private bool shouldFingerBeReversed(Finger detectedFinger)
  63. {
  64. Vector2D meanDirection = Vector2D.mean(lastRawDirections);
  65. return meanDirection.isInOppositeDirection(detectedFinger.Direction);
  66. }
  67. private void logStateChange()
  68. {
  69. String stateAsString = CurrentState.ToString().ToLower();
  70. Logger.log(String.Format("Finger #{0} {1}", this.ID, stateAsString), LogSubject.FingerTracker);
  71. }
  72. }
  73. }