TrackedPalm.cs 4.1 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.Recognition.HandRecognition;
  8. using bbiwarg.Utility;
  9. namespace bbiwarg.Recognition.PalmRecognition
  10. {
  11. class TrackedPalm : TrackedObject<Palm>
  12. {
  13. private Kalman2DPositionFilter wristUpperKalman;
  14. private Kalman2DPositionFilter wristLowerKalman;
  15. private Kalman2DPositionFilter fingersUpperKalman;
  16. private Kalman2DPositionFilter fingersLowerKalman;
  17. public Vector2D WristUpperPrediction { get { return wristUpperKalman.getPrediction(); } }
  18. public Vector2D WristLowerPrediction { get { return wristLowerKalman.getPrediction(); } }
  19. public Vector2D FingersUpperPrediction { get { return fingersUpperKalman.getPrediction(); } }
  20. public Vector2D FingersLowerPrediction { get { return fingersLowerKalman.getPrediction(); } }
  21. public Palm OptimizedPalm { get; private set; }
  22. public TrackedPalm(int id, Palm detectedPalm, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
  23. : base(id, detectedPalm, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
  24. {
  25. wristUpperKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  26. wristLowerKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  27. fingersUpperKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  28. fingersLowerKalman = new Kalman2DPositionFilter(Parameters.PalmmXX, Parameters.PalmmXY, Parameters.PalmmYY);
  29. wristUpperKalman.setInitialPosition(detectedPalm.WristUpper);
  30. wristLowerKalman.setInitialPosition(detectedPalm.WristLower);
  31. fingersUpperKalman.setInitialPosition(detectedPalm.FingersUpper);
  32. fingersLowerKalman.setInitialPosition(detectedPalm.FingersLower);
  33. updateOptimizedPalm();
  34. logStateChange();
  35. }
  36. public override void updateFrame(Palm detectedPalm)
  37. {
  38. base.updateFrame(detectedPalm);
  39. if (NumFramesInCurrentState == 1)
  40. logStateChange();
  41. if (detectedPalm != null)
  42. {
  43. wristUpperKalman.getCorrectedPosition(detectedPalm.WristUpper);
  44. wristLowerKalman.getCorrectedPosition(detectedPalm.WristLower);
  45. fingersUpperKalman.getCorrectedPosition(detectedPalm.FingersUpper);
  46. fingersLowerKalman.getCorrectedPosition(detectedPalm.FingersLower);
  47. updateOptimizedPalm();
  48. }
  49. }
  50. private void updateOptimizedPalm() {
  51. OptimizedPalm = new Palm(LastObject.Hand, WristUpperPrediction, WristLowerPrediction, FingersLowerPrediction, FingersUpperPrediction);
  52. }
  53. public override float calculateSimilarity(Palm detectedPalm)
  54. {
  55. float handSimilarity = (detectedPalm.Hand.TrackID == LastObject.Hand.TrackID) ? 1 : 0;
  56. float wristUpperSimilarity = getPositionSimilarity(WristUpperPrediction, detectedPalm.WristUpper, Parameters.PalmTrackerMaxWristUpperMove);
  57. float wristLowerSimilarity = getPositionSimilarity(WristLowerPrediction, detectedPalm.WristLower, Parameters.PalmTrackerMaxWristLowerMove);
  58. float fingersUpperSimilarity = getPositionSimilarity(FingersUpperPrediction, detectedPalm.FingersUpper, Parameters.PalmTrackerMaxFingersUpperMove);
  59. float fingersLowerSimilarity = getPositionSimilarity(FingersLowerPrediction, detectedPalm.FingersLower, Parameters.PalmTrackerMaxFingersLowerMove);
  60. float similarity = handSimilarity * wristUpperSimilarity * wristLowerSimilarity * fingersUpperSimilarity * fingersLowerSimilarity;
  61. return similarity;
  62. }
  63. private void logStateChange()
  64. {
  65. String stateAsString = CurrentState.ToString().ToLower();
  66. Logger.log(String.Format("Palm #{0} {1}", this.ID, stateAsString), LogSubject.PalmTracker);
  67. }
  68. }
  69. }