using BBIWARG.Input.InputHandling;
using BBIWARG.Recognition.Tracking;
using BBIWARG.Utility;
using System.Collections.Generic;
namespace BBIWARG.Recognition.PalmRecognition
{
///
/// Keeps track of palms over a period of time.
///
internal class PalmTracker : Tracker
{
///
/// Initializes a new instance of the PalmTracker class.
///
/// Size of the input image.
public PalmTracker(ImageSize imageSize)
: base(imageSize)
{
}
///
/// Calculates the similarity [0-1] of a tracked palm and a detected palm
///
/// the tracked palm
/// the detected palm
/// the similarity
public override float calculateSimilarity(TrackedPalm trackedPalm, Palm detectedPalm)
{
float handSimilarity = (detectedPalm.Hand.TrackID == trackedPalm.LastObject.Hand.TrackID) ? 1 : 0;
float wristUpperSimilarity = getPositionSimilarity(trackedPalm.WristUpperPrediction, detectedPalm.WristUpper, Parameters.PalmTrackerMaxWristUpperRelativeMove);
float wristLowerSimilarity = getPositionSimilarity(trackedPalm.WristLowerPrediction, detectedPalm.WristLower, Parameters.PalmTrackerMaxWristLowerRelativeMove);
float fingersUpperSimilarity = getPositionSimilarity(trackedPalm.FingersUpperPrediction, detectedPalm.FingersUpper, Parameters.PalmTrackerMaxFingersUpperRelativeMove);
float fingersLowerSimilarity = getPositionSimilarity(trackedPalm.FingersLowerPrediction, detectedPalm.FingersLower, Parameters.PalmTrackerMaxFingersLowerRelativeMove);
return handSimilarity * wristUpperSimilarity * wristLowerSimilarity * fingersUpperSimilarity * fingersLowerSimilarity;
}
///
/// Updates the tracked palms with the detected palms in the current frame and stores the (optimized) results in frameData.trackedPalms
///
/// the current frame
public void trackPalms(FrameData frameData)
{
trackObjects(frameData.DetectedPalms);
frameData.TrackedPalms = getOptimizedPalms();
}
///
/// Creates a TrackedPalm
///
/// the detected palm
/// a new TrackedPalm
protected override TrackedPalm createTrackedObject(Palm detectedPalm)
{
return new TrackedPalm(idPool.getNextUnusedID(), detectedPalm, Parameters.PalmTrackerNumFramesDetectedUntilTracked, Parameters.PalmTrackerNumFramesLostUntilDeleted);
}
///
/// Gets all optimized representations of all tracked palms.
///
/// all optimized tracked palms
private List getOptimizedPalms()
{
List optimizedPalms = new List();
foreach (TrackedPalm tp in TrackedObjects)
{
if (tp.CurrentState == TrackingState.Tracked)
optimizedPalms.Add(tp.OptimizedPalm);
}
return optimizedPalms;
}
}
}