TrackedFinger.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 bool shouldFingerBeReversed(Finger detectedFinger)
  46. {
  47. Vector2D meanDirection = Vector2D.mean(lastRawDirections);
  48. return meanDirection.isInOppositeDirection(detectedFinger.Direction);
  49. }
  50. private void logStateChange()
  51. {
  52. String stateAsString = CurrentState.ToString().ToLower();
  53. Logger.log(String.Format("Finger #{0} {1}", this.ID, stateAsString), LogSubject.FingerTracker);
  54. }
  55. }
  56. }