AI-driven workout recommendations enhance training efficiency by tailoring exercises based on user performance, fitness level, and real-time data. We’ll use machine learning models to suggest customized workouts and dynamically adjust routines.
1. Collect User Workout Data
Tracking workout performance is essential. Store data like:
- Repetitions & Sets: Number of times an exercise is performed.
- Form Accuracy: Captured via pose detection (from Day 5).
- Heart Rate: Use Apple Health or Google Fit APIs (Google Fit API)
- Workout Duration: Time spent on each exercise.
For React Native, fetch heart rate data using Google Fit API:
import GoogleFit, { Scopes } from 'react-native-google-fit';
const options = {
scopes: [Scopes.FITNESS_ACTIVITY_READ, Scopes.FITNESS_BODY_READ],
};
GoogleFit.authorize(options)
.then(() => {
GoogleFit.getHeartRateSamples({ startDate: '2024-01-01', endDate: new Date().toISOString() })
.then((res) => console.log(res))
.catch((err) => console.log(err));
})
.catch((err) => console.log(err));
2. Train a Machine Learning Model for Workout Suggestions
Use TensorFlow.js to predict recommended exercises based on past performance.
1. Install TensorFlow.js
npm install @tensorflow/tfjs
2. Train the AI Model
import * as tf from '@tensorflow/tfjs';
// Sample workout dataset (reps, duration, accuracy) => next recommended workout type
const trainingData = tf.tensor2d([
[20, 30, 90], // Squats, 30 seconds, 90% accuracy
[15, 25, 80], // Push-ups, 25 seconds, 80% accuracy
[10, 20, 70], // Lunges, 20 seconds, 70% accuracy
]);
const workoutLabels = tf.tensor2d([
[1, 0, 0], // Suggest Squats
[0, 1, 0], // Suggest Push-ups
[0, 0, 1], // Suggest Lunges
]);
const model = tf.sequential();
model.add(tf.layers.dense({ inputShape: [3], units: 10, activation: 'relu' }));
model.add(tf.layers.dense({ units: 3, activation: 'softmax' }));
model.compile({ optimizer: 'adam', loss: 'categoricalCrossentropy' });
async function trainModel() {
await model.fit(trainingData, workoutLabels, { epochs: 100 });
console.log('Training complete');
}
trainModel();
3. Get AI-Based Workout Recommendations
Once trained, use real-time data to suggest exercises dynamically.
async function getWorkoutSuggestion(reps, duration, accuracy) {
const inputTensor = tf.tensor2d([[reps, duration, accuracy]]);
const prediction = model.predict(inputTensor);
const result = await prediction.data();
const workoutTypes = ['Squats', 'Push-ups', 'Lunges'];
return workoutTypes[result.indexOf(Math.max(...result))];
}
getWorkoutSuggestion(18, 28, 85).then(console.log);
4. Provide AI-Generated Voice Feedback
Use Expo Speech API to announce the next workout.
import * as Speech from 'expo-speech';
getWorkoutSuggestion(18, 28, 85).then((exercise) => {
Speech.speak(`Next, do ${exercise} for 30 seconds.`);
});
This AI-driven recommendation system analyzes user progress and dynamically adjusts workouts, mimicking a real personal trainer.