TrackedFinger.cs 3.3 KB

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