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:
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):
self.mp_pose = mp.solutions.pose
self.pose = self.mp_pose.Pose(
static_image_mode=False,
model_complexity=2,
enable_segmentation=True,
min_detection_confidence=0.7,
min_tracking_confidence=0.5
)
self.mp_drawing = mp.solutions.drawing_utils
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]
}
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
"""
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = self.pose.process(rgb_frame)
if not results.pose_landmarks:
return {'status': 'no_pose_detected', 'feedback': 'Please ensure full body is visible'}
landmarks = self._extract_landmark_coordinates(results.pose_landmarks, frame.shape)
body_angles = self._calculate_body_angles(landmarks)
posture_analysis = self._analyze_posture(landmarks, body_angles)
form_feedback = self._generate_form_feedback(landmarks, body_angles, exercise_type)
form_score = self._calculate_form_score(body_angles, posture_analysis, exercise_type)
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 = {}
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']
)
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']
)
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']
)
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.
"""
vector1 = np.array([point1[0] - point2[0], point1[1] - point2[1]])
vector2 = np.array([point3[0] - point2[0], point3[1] - point2[1]])
cosine_angle = np.dot(vector1, vector2) / (np.linalg.norm(vector1) * np.linalg.norm(vector2))
cosine_angle = np.clip(cosine_angle, -1.0, 1.0)
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': []
}
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:
posture_analysis['issues'].append('Uneven shoulders detected')
posture_analysis['recommendations'].append('Focus on shoulder blade 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:
posture_analysis['issues'].append('Hip misalignment detected')
posture_analysis['recommendations'].append('Engage core muscles for stability')
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:
posture_analysis['issues'].append('Forward head posture')
posture_analysis['recommendations'].append('Keep head aligned over shoulders')
max_issues = 5
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 = []
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')
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')
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
}