Skip to content

Commit

Permalink
Camera logic correction
Browse files Browse the repository at this point in the history
  • Loading branch information
MohEsmail143 committed Jul 7, 2023
1 parent 60dc911 commit 6ba7272
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 50 deletions.
37 changes: 28 additions & 9 deletions lib/screens/camera_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class _CameraScreenState extends State<CameraScreen>
/// FormClassifier-related fields
double formCorrectness = 0.0;
bool showCorrectness = false;
late FormClassifier formClassifier;

/// ******************************
Expand Down Expand Up @@ -273,12 +274,22 @@ class _CameraScreenState extends State<CameraScreen>
[widget.exercise.trackingDirection]);
}

/*
if (isNotMoving) {
print("I'm not moving");
} else {
print("I'm moving");
}
*/

/// Counting reps inferences
if (isNotMoving &&
countingRepsMode &&
inferenceResults[widget.exercise.trackedKeypoint][2] >= 0.3) {
repCounter.startCounting(inferenceResults[widget.exercise.trackedKeypoint]
[widget.exercise.trackingDirection]);
repCounter.startCounting(
inferenceResults[widget.exercise.trackedKeypoint]
[widget.exercise.trackingDirection],
widget.exercise.fullRepPosition);
if (repCounter.currentRepCount >= repCounter.maxRepCount) {
countingRepsMode = false;
}
Expand All @@ -287,13 +298,14 @@ class _CameraScreenState extends State<CameraScreen>
/// Random advice during workout if form correctness < 0.5
if (!adviceCooldown &&
formCorrectness < 0.5 &&
(warmupMode || (countingRepsMode))) {
showCorrectness &&
(warmupMode || countingRepsMode)) {
// print("here");
tts.speak(widget.exercise.correctionAdvice[
currentAdvice % widget.exercise.correctionAdvice.length]);
currentAdvice++;
adviceCooldown = true;
Timer(const Duration(seconds: 15), () {
Timer(const Duration(seconds: 30), () {
adviceCooldown = false;
});
}
Expand Down Expand Up @@ -403,10 +415,8 @@ class _CameraScreenState extends State<CameraScreen>
),
),
Visibility(
visible: warmupMode ||
countingRepsMode &&
!currentlyRestingMode &&
allowRestMode,
visible:
warmupMode || showCorrectness && !currentlyRestingMode,
child: Positioned(
bottom: 24,
left: 24,
Expand Down Expand Up @@ -486,12 +496,14 @@ class _CameraScreenState extends State<CameraScreen>
Timer(
Duration(seconds: widget.session.restTime),
() {
showCorrectness = false;
allowRestMode = false;
currentlyRestingMode = false;
repCounter.resetRepCount();
},
);
} else {
// At this condition, the user will have finished his required sets, and completed his workout
tts.speak(
"Well done! You have completed your workout! Great Job!");
showDialog(
Expand Down Expand Up @@ -532,8 +544,15 @@ class _CameraScreenState extends State<CameraScreen>
}
}
// repCounter.resetRepCount();
countingRepsMode = true;
showCorrectness = true;
allowRestMode = true;

Timer(
const Duration(seconds: 5),
() {
countingRepsMode = true;
},
);
} else if (!warmedUpAtLeastOnce) {
Fluttertoast.showToast(
msg: "You should warm up first",
Expand Down
41 changes: 22 additions & 19 deletions lib/screens/home_view_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -313,27 +313,30 @@ class HomeViewBody extends StatelessWidget {
itemCount: availableExercises.length,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, index) {
return GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ExerciseSelectionScreen(
exercise: availableExercises[index],
return InkWell(
onTap: () {},
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ExerciseSelectionScreen(
exercise: availableExercises[index],
),
),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
);
},
child: Card(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(15.0),
),
color: Colors.black54,
child: Padding(
padding: const EdgeInsets.only(
left: 12, right: 8, top: 8, bottom: 8),
child: CustomListTile(
exercise: availableExercises[index],
color: Colors.black54,
child: Padding(
padding: const EdgeInsets.only(
left: 12, right: 8, top: 8, bottom: 8),
child: CustomListTile(
exercise: availableExercises[index],
),
),
),
),
Expand Down
1 change: 1 addition & 0 deletions lib/screens/welcome_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ class WelcomeScreen extends StatelessWidget {
const EdgeInsets.only(right: 40.0, top: 40, bottom: 40),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Text(
"Skip Intro",
Expand Down
29 changes: 19 additions & 10 deletions lib/utils/camera/rep_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,25 @@ class RepCounter {
/// Running MoveNet to find the max and min altitude
/// of the workout performed, appearing to the user in the
/// form of "Warmup Mode"
void startCounting(int trackedKeypointLocation) {
// Initialize the flag `up = false`
// Comparing the tracked keypoint's location to the midpoint and quartile
if (trackedKeypointLocation > (midpoint + quartile) &&
upPosition == false) {
currentRepCount += 1;
tts.speak(currentRepCount.toString());
upPosition = true;
} else if (trackedKeypointLocation < midpoint) {
upPosition = false;
void startCounting(int trackedKeypointLocation, bool fullRepPosition) {
if (!fullRepPosition) {
if (trackedKeypointLocation > (midpoint + quartile) &&
upPosition == false) {
currentRepCount += 1;
tts.speak(currentRepCount.toString());
upPosition = true;
} else if (trackedKeypointLocation < midpoint) {
upPosition = false;
}
} else {
if (trackedKeypointLocation < (midpoint - quartile) &&
upPosition == false) {
currentRepCount += 1;
tts.speak(currentRepCount.toString());
upPosition = true;
} else if (trackedKeypointLocation > midpoint) {
upPosition = false;
}
}
}

Expand Down
29 changes: 17 additions & 12 deletions lib/utils/exercise.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Exercise {
final String gif;
final int trackedKeypoint;
final int trackingDirection;
final bool fullRepPosition; // up -> true, down -> false
final String formCorrectnessModel;
final List<String> targetedMuscles;
final List<String> cameraInstructions;
Expand All @@ -17,6 +18,7 @@ class Exercise {
required this.gif,
required this.trackedKeypoint,
required this.trackingDirection,
required this.fullRepPosition,
required this.formCorrectnessModel,
required this.targetedMuscles,
required this.cameraInstructions,
Expand All @@ -37,16 +39,17 @@ final List<Exercise> availableExercises = [
gif: 'assets/images/push_up.gif',
trackedKeypoint: kKeypointDict['right_shoulder'] as int,
trackingDirection: 0,
fullRepPosition: true,
formCorrectnessModel: 'models/pushUp_version2.tflite',
targetedMuscles: ['Chest', 'Triceps', 'Shoulders'],
cameraInstructions: [
"For this exercise, you need to place your phone in a landscape orientation.",
"While exercising, your phone needs to be in a stable position (i.e. not move).",
"Your phone's camera should be able to view your entire body's right side, specially your right shoulder, since we use it to track your reps",
"Your phone's camera should be able to view your entire body's right side, specially your right shoulder, since we use it to track your reps.",
"Start a warmup for 15 seconds. This is necessary for our AI to calculate some statistics off your body.",
"Once you're done with your warmup, you should be able to start your workout",
"Once you have started a set, you should perform the required number of reps",
"When you're done, go back to your phone and start you rest period",
"Once you're done with your warmup, you should be able to start your workout.",
"Once you have started a set, you should perform the required number of reps.",
"When you're done, go back to your phone and start your rest period.",
"If you want to, you can always just finish your set early and take your rest. Remember, exercise is supposed to be fun!",
],
correctionAdvice: [
Expand All @@ -69,16 +72,17 @@ final List<Exercise> availableExercises = [
gif: 'assets/images/pull_up.gif',
trackedKeypoint: kKeypointDict['nose'] as int,
trackingDirection: 1,
fullRepPosition: false,
formCorrectnessModel: 'models/pullUp.tflite',
targetedMuscles: ['Back', 'Biceps', 'Shoulders'],
cameraInstructions: [
"For this exercise, you need to place your phone in a portrait orientation.",
"While exercising, your phone needs to be in a stable position (i.e. not move).",
"Your phone's camera should be able to view your entire body's anterior, specially your nose, since we use it to track your reps",
"Your phone's camera should be able to view your entire body's anterior, specially your nose, since we use it to track your reps.",
"Start a warmup for 15 seconds. This is necessary for our AI to calculate some statistics off your body.",
"Once you're done with your warmup, you should be able to start your workout",
"Once you have started a set, you should perform the required number of reps",
"When you're done, go back to your phone and start you rest period",
"Once you're done with your warmup, you should be able to start your workout.",
"Once you have started a set, you should perform the required number of reps.",
"When you're done, go back to your phone and start your rest period.",
"If you want to, you can always just finish your set early and take your rest. Remember, exercise is supposed to be fun!",
],
correctionAdvice: [
Expand All @@ -101,6 +105,7 @@ final List<Exercise> availableExercises = [
gif: 'assets/images/squat.gif',
trackedKeypoint: kKeypointDict['nose'] as int,
trackingDirection: 1,
fullRepPosition: true,
formCorrectnessModel: 'models/squat.tflite',
targetedMuscles: [
'Quads',
Expand All @@ -111,11 +116,11 @@ final List<Exercise> availableExercises = [
cameraInstructions: [
"For this exercise, you need to place your phone in a portrait orientation.",
"While exercising, your phone needs to be in a stable position (i.e. not move).",
"Your phone's camera should be able to view your entire body's anterior, specially your nose, since we use it to track your reps",
"Your phone's camera should be able to view your entire body's anterior, specially your nose, since we use it to track your reps.",
"Start a warmup for 15 seconds. This is necessary for our AI to calculate some statistics off your body.",
"Once you're done with your warmup, you should be able to start your workout",
"Once you have started a set, you should perform the required number of reps",
"When you're done, go back to your phone and start you rest period",
"Once you're done with your warmup, you should be able to start your workout.",
"Once you have started a set, you should perform the required number of reps.",
"When you're done, go back to your phone and start your rest period.",
"If you want to, you can always just finish your set early and take your rest. Remember, exercise is supposed to be fun!",
],
correctionAdvice: [
Expand Down

0 comments on commit 6ba7272

Please sign in to comment.