2 using System.Collections.Generic;
3 using System.Diagnostics;
6 using Tizen.Multimedia.Vision;
8 namespace Fitness.Services
11 /// Class handling the squat exercise detection.
13 public class SquatService : BaseExerciseService, IExerciseService
15 private const float SquatDetectedThreshold = 0.92f;
16 private const int HoldCount = 5;
20 private float currentSquatSimilarity = 0;
21 private int averageScore;
22 private Stopwatch stopwatch = new Stopwatch();
23 private System.Timers.Timer holdTimer;
24 private long holdTimeThreshold;
25 private List<int> allScores = new List<int>();
28 public event EventHandler<ExerciseStateUpdatedEventArgs> ExerciseStateUpdated;
31 public int AverageScore
43 public long HoldTimeThreshold
45 get => holdTimeThreshold;
48 if (value != holdTimeThreshold)
50 holdTimeThreshold = value;
57 public void ResetWorkout()
59 TimerConfigured = false;
65 protected override void DetectExercise(Landmark[,] landmarks)
67 int numberOfBodyParts = landmarks.GetLength(1);
68 var range = Enumerable.Range(0, numberOfBodyParts);
70 var locations = range.Select(x => landmarks[0, x].Location).ToList();
71 var squatSimilarity = SquatDetector.Similarity(locations);
72 if (float.IsNaN(squatSimilarity))
74 squatSimilarity = 0.0f;
77 DetectSquat(squatSimilarity);
79 NUIContext.InvokeOnMainThread(() =>
81 ExerciseStateUpdated?.Invoke(this, new ExerciseStateUpdatedEventArgs()
83 PoseLandmarks = landmarks,
88 averageScore = (int)System.Math.Round((float)(allScores.Count > 0 ? allScores.Average() : 0));
92 private void InitializeTimer()
94 long interval = holdTimeThreshold / HoldCount;
95 holdTimer = new System.Timers.Timer(interval);
96 holdTimer.Elapsed += HoldTimer_Elapsed;
97 holdTimer.AutoReset = true;
98 holdTimer.Enabled = true;
101 private void HoldTimer_Elapsed(object sender, ElapsedEventArgs e)
103 if (hold < HoldCount && currentSquatSimilarity >= SquatDetectedThreshold)
109 private void DetectSquat(float squatSimilarity)
111 var previousSquatSimilarity = currentSquatSimilarity;
112 currentSquatSimilarity = squatSimilarity;
113 score = (int)System.Math.Round((float)(100 * currentSquatSimilarity));
115 if (previousSquatSimilarity < SquatDetectedThreshold)
117 if (currentSquatSimilarity >= SquatDetectedThreshold)
123 else if (currentSquatSimilarity < SquatDetectedThreshold)
128 if (stopwatch.ElapsedMilliseconds >= holdTimeThreshold)
137 if (currentSquatSimilarity >= SquatDetectedThreshold)
139 allScores.Add(score);