πŸ€Έβ€β™€οΈ Pose Perfect AI

Core Source Code & Computer Vision Implementation

Python 3.9 OpenCV MediaPipe Real-time Analysis

πŸ” About This Code Showcase

This curated code snippet demonstrates how Pose Perfect AI performs real-time pose analysis, skeletal tracking, and provides intelligent feedback for fitness and movement optimization.

Full deployment scripts, API integrations, and proprietary details are omitted for clarity and security. This showcase highlights the core computer vision and pose analysis algorithms.

🦴 Core Algorithm: Real-time Pose Detection Engine

The foundation of Pose Perfect AI is its ability to accurately detect human poses in real-time, analyze skeletal movements, and provide intelligent feedback for optimal form and technique:

πŸ“„ pose_detection_engine.py
import cv2 import mediapipe as mp import numpy as np from typing import Dict, List, Tuple, Optional import math class PosePerfectEngine: """ Advanced pose detection and analysis engine that provides real-time feedback for fitness movements, posture correction, and movement optimization. """ def __init__(self): # Initialize MediaPipe pose detection self.mp_pose = mp.solutions.pose self.pose = self.mp_pose.Pose( static_image_mode=False, model_complexity=2, # High accuracy model enable_segmentation=True, min_detection_confidence=0.7, min_tracking_confidence=0.5 ) self.mp_drawing = mp.solutions.drawing_utils # Define key body landmarks for analysis self.key_landmarks = { 'head': [self.mp_pose.PoseLandmark.NOSE, self.mp_pose.PoseLandmark.LEFT_EAR, self.mp_pose.PoseLandmark.RIGHT_EAR], 'shoulders': [self.mp_pose.PoseLandmark.LEFT_SHOULDER, self.mp_pose.PoseLandmark.RIGHT_SHOULDER], 'arms': [self.mp_pose.PoseLandmark.LEFT_ELBOW, self.mp_pose.PoseLandmark.RIGHT_ELBOW, self.mp_pose.PoseLandmark.LEFT_WRIST, self.mp_pose.PoseLandmark.RIGHT_WRIST], 'torso': [self.mp_pose.PoseLandmark.LEFT_HIP, self.mp_pose.PoseLandmark.RIGHT_HIP], 'legs': [self.mp_pose.PoseLandmark.LEFT_KNEE, self.mp_pose.PoseLandmark.RIGHT_KNEE, self.mp_pose.PoseLandmark.LEFT_ANKLE, self.mp_pose.PoseLandmark.RIGHT_ANKLE] } # Exercise templates for form analysis self.exercise_templates = { 'squat': { 'target_angles': {'knee': 90, 'hip': 90, 'ankle': 90}, 'key_points': ['knee_alignment', 'back_straight', 'weight_distribution'] }, 'push_up': { 'target_angles': {'elbow': 90, 'shoulder': 0, 'hip': 180}, 'key_points': ['straight_line', 'elbow_position', 'full_range'] }, 'plank': { 'target_angles': {'hip': 180, 'shoulder': 90, 'ankle': 90}, 'key_points': ['neutral_spine', 'core_engagement', 'steady_position'] } } def analyze_pose_realtime(self, frame: np.ndarray, exercise_type: str = 'general') -> Dict: """ Perform real-time pose analysis on video frame. Detects landmarks, calculates angles, and provides form feedback. Args: frame: Input video frame (BGR format) exercise_type: Type of exercise being performed for specific analysis Returns: Dictionary containing pose analysis results and feedback """ # Convert BGR to RGB for MediaPipe processing rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Detect pose landmarks results = self.pose.process(rgb_frame) if not results.pose_landmarks: return {'status': 'no_pose_detected', 'feedback': 'Please ensure full body is visible'} # Extract landmark coordinates landmarks = self._extract_landmark_coordinates(results.pose_landmarks, frame.shape) # Calculate body angles and measurements body_angles = self._calculate_body_angles(landmarks) # Analyze posture and alignment posture_analysis = self._analyze_posture(landmarks, body_angles) # Generate exercise-specific feedback form_feedback = self._generate_form_feedback(landmarks, body_angles, exercise_type) # Calculate overall form score form_score = self._calculate_form_score(body_angles, posture_analysis, exercise_type) # Create visualization overlay annotated_frame = self._create_pose_visualization(frame, results.pose_landmarks, form_feedback) return { 'status': 'pose_detected', 'landmarks': landmarks, 'body_angles': body_angles, 'posture_analysis': posture_analysis, 'form_feedback': form_feedback, 'form_score': form_score, 'annotated_frame': annotated_frame, 'exercise_type': exercise_type } def _calculate_body_angles(self, landmarks: Dict) -> Dict: """ Calculate important body angles for posture and movement analysis. Computes joint angles using trigonometry for accurate measurements. """ angles = {} # Calculate knee angles (important for squats, lunges) if 'left_hip' in landmarks and 'left_knee' in landmarks and 'left_ankle' in landmarks: angles['left_knee'] = self._calculate_angle( landmarks['left_hip'], landmarks['left_knee'], landmarks['left_ankle'] ) if 'right_hip' in landmarks and 'right_knee' in landmarks and 'right_ankle' in landmarks: angles['right_knee'] = self._calculate_angle( landmarks['right_hip'], landmarks['right_knee'], landmarks['right_ankle'] ) # Calculate elbow angles (important for push-ups, arm exercises) if 'left_shoulder' in landmarks and 'left_elbow' in landmarks and 'left_wrist' in landmarks: angles['left_elbow'] = self._calculate_angle( landmarks['left_shoulder'], landmarks['left_elbow'], landmarks['left_wrist'] ) if 'right_shoulder' in landmarks and 'right_elbow' in landmarks and 'right_wrist' in landmarks: angles['right_elbow'] = self._calculate_angle( landmarks['right_shoulder'], landmarks['right_elbow'], landmarks['right_wrist'] ) # Calculate hip angles (important for squats, deadlifts) if 'left_shoulder' in landmarks and 'left_hip' in landmarks and 'left_knee' in landmarks: angles['left_hip'] = self._calculate_angle( landmarks['left_shoulder'], landmarks['left_hip'], landmarks['left_knee'] ) # Calculate shoulder angles (important for overhead movements) if 'left_hip' in landmarks and 'left_shoulder' in landmarks and 'left_elbow' in landmarks: angles['left_shoulder'] = self._calculate_angle( landmarks['left_hip'], landmarks['left_shoulder'], landmarks['left_elbow'] ) return angles def _calculate_angle(self, point1: Tuple, point2: Tuple, point3: Tuple) -> float: """ Calculate angle between three points using vector mathematics. Point2 is the vertex of the angle. """ # Calculate vectors vector1 = np.array([point1[0] - point2[0], point1[1] - point2[1]]) vector2 = np.array([point3[0] - point2[0], point3[1] - point2[1]]) # Calculate angle using dot product cosine_angle = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2)) # Clamp cosine value to valid range to avoid numerical errors cosine_angle = np.clip(cosine_angle, -1.0, 1.0) # Convert to degrees angle = np.arccos(cosine_angle) return np.degrees(angle) def _analyze_posture(self, landmarks: Dict, angles: Dict) -> Dict: """ Analyze overall posture and body alignment. Detects common posture issues and provides corrective guidance. """ posture_analysis = { 'overall_score': 0, 'issues': [], 'recommendations': [] } # Check shoulder alignment if 'left_shoulder' in landmarks and 'right_shoulder' in landmarks: shoulder_level = abs(landmarks['left_shoulder'][1] - landmarks['right_shoulder'][1]) if shoulder_level > 20: # Threshold for uneven shoulders posture_analysis['issues'].append('Uneven shoulders detected') posture_analysis['recommendations'].append('Focus on shoulder blade alignment') # Check hip alignment if 'left_hip' in landmarks and 'right_hip' in landmarks: hip_level = abs(landmarks['left_hip'][1] - landmarks['right_hip'][1]) if hip_level > 15: # Threshold for uneven hips posture_analysis['issues'].append('Hip misalignment detected') posture_analysis['recommendations'].append('Engage core muscles for stability') # Check spine alignment (head, shoulders, hips in line) if 'nose' in landmarks and 'left_shoulder' in landmarks and 'left_hip' in landmarks: head_forward = landmarks['nose'][0] - landmarks['left_shoulder'][0] if abs(head_forward) > 30: # Forward head posture threshold posture_analysis['issues'].append('Forward head posture') posture_analysis['recommendations'].append('Keep head aligned over shoulders') # Calculate overall posture score max_issues = 5 # Maximum possible issues current_issues = len(posture_analysis['issues']) posture_analysis['overall_score'] = max(0, (100 - (current_issues / max_issues) * 100)) return posture_analysis def _generate_form_feedback(self, landmarks: Dict, angles: Dict, exercise_type: str) -> Dict: """ Generate specific feedback based on exercise type and current form. Provides actionable corrections for optimal performance. """ feedback = { 'exercise': exercise_type, 'corrections': [], 'good_points': [], 'overall_rating': 'Good' } if exercise_type == 'squat': feedback.update(self._analyze_squat_form(landmarks, angles)) elif exercise_type == 'push_up': feedback.update(self._analyze_pushup_form(landmarks, angles)) elif exercise_type == 'plank': feedback.update(self._analyze_plank_form(landmarks, angles)) else: feedback.update(self._analyze_general_form(landmarks, angles)) return feedback def _analyze_squat_form(self, landmarks: Dict, angles: Dict) -> Dict: """ Analyze squat form with specific focus on knee tracking, depth, and balance. """ corrections = [] good_points = [] # Check squat depth using knee angle if 'left_knee' in angles: knee_angle = angles['left_knee'] if knee_angle > 120: corrections.append('Go deeper - thighs should be parallel to ground') elif knee_angle < 70: corrections.append('Avoid going too deep - maintain control') else: good_points.append('Good squat depth achieved') # Check knee alignment (knees should track over toes) if 'left_knee' in landmarks and 'left_ankle' in landmarks: knee_x = landmarks['left_knee'][0] ankle_x = landmarks['left_ankle'][0] if abs(knee_x - ankle_x) > 30: corrections.append('Keep knees aligned over toes') else: good_points.append('Good knee tracking') # Check back alignment if 'left_shoulder' in landmarks and 'left_hip' in landmarks: torso_angle = self._calculate_torso_angle(landmarks) if torso_angle < 45: corrections.append('Keep chest up and maintain neutral spine') else: good_points.append('Good torso position') rating = 'Excellent' if len(corrections) == 0 else 'Good' if len(corrections) <= 2 else 'Needs Work' return { 'corrections': corrections, 'good_points': good_points, 'overall_rating': rating }

βš™οΈ Technical Implementation Notes

Key Algorithms & Innovations

Why This Approach Works