On Day 3, we’ll track workout reps (squats, push-ups, lunges) using pose angles and detect incorrect form in real-time.
1. Why AI-Based Rep Counting & Form Correction?
✅ No Need for Manual Counting → AI automatically tracks reps.
✅ Ensures Correct Form → AI prevents injuries by detecting bad posture.
✅ Personalized Feedback → AI alerts users if their form needs adjustments.
✅ Improves Training Efficiency → AI ensures maximum impact per workout.
2. How AI Rep Counting Works
1️⃣ Detects Key Points (shoulders, elbows, knees, ankles).
2️⃣ Calculates Angles (e.g., knee angle in squats).
3️⃣ Identifies Rep Motion (full range of motion triggers a count).
4️⃣ Checks Form (flags bad posture based on body tilt & misalignment).
3. Adding Rep Counting Logic
✅ Step 1: Modify WorkoutScreen.js
to Count Reps
Modify src/screens/WorkoutScreen.js
:
import React, { useRef, useState, useEffect } from 'react';
import { View, Text, StyleSheet } from 'react-native';
import { Camera } from 'expo-camera';
import * as tf from '@tensorflow/tfjs';
import * as poseDetection from '@tensorflow-models/pose-detection';
import { cameraWithTensors } from '@tensorflow/tfjs-react-native';
const TensorCamera = cameraWithTensors(Camera);
export default function WorkoutScreen() {
const [permission, setPermission] = useState(null);
const [poseData, setPoseData] = useState(null);
const [reps, setReps] = useState(0);
const [prevAngle, setPrevAngle] = useState(null);
const [isDown, setIsDown] = useState(false);
const cameraRef = useRef(null);
let detector = null;
useEffect(() => {
(async () => {
const { status } = await Camera.requestPermissionsAsync();
setPermission(status === 'granted');
await tf.ready();
detector = await poseDetection.createDetector(poseDetection.SupportedModels.BlazePose);
})();
}, []);
const calculateAngle = (a, b, c) => {
const radians = Math.atan2(c.y - b.y, c.x - b.x) - Math.atan2(a.y - b.y, a.x - b.x);
let angle = Math.abs((radians * 180.0) / Math.PI);
if (angle > 180.0) angle = 360 - angle;
return angle;
};
const handlePoseDetection = async (imageTensor) => {
if (!detector) return;
const poses = await detector.estimatePoses(imageTensor);
if (poses.length > 0) {
const keypoints = poses[0].keypoints;
setPoseData(keypoints);
// Detect Squat Reps (Using Knee Angle)
const leftHip = keypoints[23];
const leftKnee = keypoints[25];
const leftAnkle = keypoints[27];
const kneeAngle = calculateAngle(leftHip, leftKnee, leftAnkle);
setPrevAngle(kneeAngle);
if (kneeAngle < 90 && !isDown) {
setIsDown(true);
} else if (kneeAngle > 160 && isDown) {
setIsDown(false);
setReps((prev) => prev + 1);
}
}
};
if (permission === null) return <Text>Requesting camera permission...</Text>;
if (permission === false) return <Text>No access to camera</Text>;
return (
<View style={styles.container}>
<TensorCamera
ref={cameraRef}
style={styles.camera}
type={Camera.Constants.Type.front}
onReady={handlePoseDetection}
autorender={true}
/>
<Text style={styles.counter}>Reps: {reps}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, justifyContent: 'center', alignItems: 'center' },
camera: { width: '100%', height: '70%' },
counter: { fontSize: 24, fontWeight: 'bold', marginTop: 10 },
});
4. Adding Real-Time Form Correction
✅ Step 1: Detect Bad Posture
Modify handlePoseDetection
to detect incorrect squat form:
if (kneeAngle < 60) {
alert('Squat is too shallow! Lower your hips.');
}
if (kneeAngle > 160 && isDown) {
alert('Good form! Keep going.');
}
5. Testing AI Rep Counting
Run:
npx expo start
- The app counts reps automatically as you perform squats.
- The app alerts if form is incorrect (e.g., shallow squat).
6. Preparing for Tomorrow: Voice-Controlled Workouts
Tomorrow, we’ll:
- Enable hands-free AI coaching with voice commands.
- Use React Native Voice to start, pause, and switch workouts.
7. Key Concepts Covered
✅ Tracked squats using AI pose detection.
✅ Implemented automatic rep counting based on knee angles.
✅ Added real-time form correction alerts.
8. Next Steps: Adding Voice-Controlled AI Coaching
Tomorrow, we’ll:
- Enable users to say “Start Squats” to begin workouts.
- Detect “Pause Workout” & “Next Exercise” using voice commands.
9. References & Learning Resources
10. SEO Keywords:
AI rep counter, React Native fitness app, AI workout tracker, TensorFlow pose tracking, AI-powered form correction.