123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using bbiwarg.Recognition.Tracking;
- using bbiwarg.Recognition.HandRecognition;
- using bbiwarg.Utility;
- namespace bbiwarg.Recognition.PalmRecognition
- {
- class TrackedPalm : TrackedObject<Palm>
- {
- private Kalman2DPositionFilter wristUpperKalman;
- private Kalman2DPositionFilter wristLowerKalman;
- private Kalman2DPositionFilter fingersUpperKalman;
- private Kalman2DPositionFilter fingersLowerKalman;
- public Vector2D WristUpperPrediction { get { return wristUpperKalman.getPrediction(); } }
- public Vector2D WristLowerPrediction { get { return wristLowerKalman.getPrediction(); } }
- public Vector2D FingersUpperPrediction { get { return fingersUpperKalman.getPrediction(); } }
- public Vector2D FingersLowerPrediction { get { return fingersLowerKalman.getPrediction(); } }
- public Palm OptimizedPalm { get; private set; }
- public TrackedPalm(int id, Palm detectedPalm, int numFramesDetectedUntilTracked, int numFramesLostUntilDeleted)
- : base(id, detectedPalm, numFramesDetectedUntilTracked, numFramesLostUntilDeleted)
- {
- wristUpperKalman = new Kalman2DPositionFilter(Constants.PalmmXX, Constants.PalmmXY, Constants.PalmmYY);
- wristLowerKalman = new Kalman2DPositionFilter(Constants.PalmmXX, Constants.PalmmXY, Constants.PalmmYY);
- fingersUpperKalman = new Kalman2DPositionFilter(Constants.PalmmXX, Constants.PalmmXY, Constants.PalmmYY);
- fingersLowerKalman = new Kalman2DPositionFilter(Constants.PalmmXX, Constants.PalmmXY, Constants.PalmmYY);
- wristUpperKalman.setInitialPosition(detectedPalm.WristUpper);
- wristLowerKalman.setInitialPosition(detectedPalm.WristLower);
- fingersUpperKalman.setInitialPosition(detectedPalm.FingersUpper);
- fingersLowerKalman.setInitialPosition(detectedPalm.FingersLower);
- updateOptimizedPalm();
- logStateChange();
- }
- public override void updateFrame(Palm detectedPalm)
- {
- base.updateFrame(detectedPalm);
- if (NumFramesInCurrentState == 1)
- logStateChange();
- if (detectedPalm != null)
- {
- wristUpperKalman.getCorrectedPosition(detectedPalm.WristUpper);
- wristLowerKalman.getCorrectedPosition(detectedPalm.WristLower);
- fingersUpperKalman.getCorrectedPosition(detectedPalm.FingersUpper);
- fingersLowerKalman.getCorrectedPosition(detectedPalm.FingersLower);
- updateOptimizedPalm();
- }
- }
- private void updateOptimizedPalm() {
- OptimizedPalm = new Palm(LastObject.Hand, WristUpperPrediction, WristLowerPrediction, FingersLowerPrediction, FingersUpperPrediction);
- }
- public override float calculateSimilarity(Palm detectedPalm)
- {
- //hand
- float handSimilarity = (detectedPalm.Hand.TrackID == LastObject.Hand.TrackID) ? 1 : 0;
- //wristUpper
- float wristUpperDistance = WristUpperPrediction.getDistanceTo(detectedPalm.WristUpper);
- float wristUpperSimilarity = Math.Max(0, 1 - wristUpperDistance / Constants.ImageDiagonalLength);
- //wristLower
- float wristLowerDistance = WristLowerPrediction.getDistanceTo(detectedPalm.WristLower);
- float wristLowerSimilarity = Math.Max(0, 1 - wristLowerDistance / Constants.ImageDiagonalLength);
- //fingersUpper
- float fingersUpperDistance = FingersUpperPrediction.getDistanceTo(detectedPalm.FingersUpper);
- float fingersUpperSimilarity = Math.Max(0, 1 - fingersUpperDistance / Constants.ImageDiagonalLength);
- //fingersLower
- float fingersLowerDistance = FingersLowerPrediction.getDistanceTo(detectedPalm.FingersLower);
- float fingersLowerSimilarity = Math.Max(0, 1 - fingersLowerDistance / Constants.ImageDiagonalLength);
- float similarity = handSimilarity * wristUpperSimilarity * wristLowerSimilarity * fingersUpperSimilarity * fingersLowerSimilarity;
- return similarity;
- }
- private void logStateChange()
- {
- String stateAsString = CurrentState.ToString().ToLower();
- Logger.log(String.Format("Palm #{0} {1}", this.ID, stateAsString), LogSubject.PalmTracker);
- }
- }
- }
|