AI-powered real-time workout tracking enhances fitness routines by analyzing movement, counting repetitions, and providing instant feedback. Today, we’ll implement real-time exercise tracking using pose detection, rep counting, and AI-powered corrections.
1. Implement Pose Tracking for Exercise Recognition
We’ll use TensorFlow.js MoveNet for real-time body movement analysis.
Install Dependencies
npm install @tensorflow/tfjs @tensorflow-models/pose-detection @mediapipe/pose
Load Pose Detection Model
import * as posedetection from "@tensorflow-models/pose-detection";
let detector;
export const loadPoseModel = async () => {
detector = await posedetection.createDetector(
posedetection.SupportedModels.MoveNet,
{ modelType: "Lightning" }
);
};
2. Count Repetitions Using Keypoint Tracking
To count squats, we detect knee position changes.
let squatCount = 0;
let isSquatting = false;
export const countSquats = async (videoElement) => {
const poses = await detector.estimatePoses(videoElement);
if (!poses.length) return;
const knee = poses[0].keypoints.find(p => p.name === 'left_knee');
if (knee.y > 300 && !isSquatting) {
isSquatting = true;
} else if (knee.y < 300 && isSquatting) {
isSquatting = false;
squatCount++;
return squatCount;
}
};
3. Provide Real-Time AI Feedback
Use Expo Speech API for voice coaching.
import * as Speech from 'expo-speech';
export const giveFeedback = (count) => {
if (count % 5 === 0) {
Speech.speak(`Great job! You've completed ${count} squats.`);
}
};
4. Display Live Tracking in React Native
Use Expo Camera for real-time tracking.
import { Camera } from 'expo-camera';
import { useRef, useEffect, useState } from 'react';
import { View, Text, Button } from 'react-native';
import { loadPoseModel, countSquats, giveFeedback } from './poseDetection';
export default function LiveWorkout() {
const cameraRef = useRef(null);
const [hasPermission, setHasPermission] = useState(null);
const [count, setCount] = useState(0);
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setHasPermission(status === 'granted');
await loadPoseModel();
})();
}, []);
const trackWorkout = async () => {
const newCount = await countSquats(cameraRef.current);
if (newCount) {
setCount(newCount);
giveFeedback(newCount);
}
};
return (
<View>
<Camera ref={cameraRef} style={{ width: '100%', height: 400 }} />
<Text>Squat Count: {count}</Text>
<Button title="Track Workout" onPress={trackWorkout} />
</View>
);
}
Final Thoughts
This feature enables real-time tracking, automatic rep counting, and AI-powered feedback, making workouts more interactive and effective.