Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# Pose-Think: AI-Powered Movement Analysis Suite -
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
| 4 |
import gradio as gr
|
|
@@ -10,33 +10,80 @@ mp_hands = mp.solutions.hands
|
|
| 10 |
mp_drawing = mp.solutions.drawing_utils
|
| 11 |
|
| 12 |
def analyze_posture(image, analysis_type="basic", age=None, height=None, weight=None):
|
| 13 |
-
"""
|
| 14 |
if image is None:
|
| 15 |
-
return None, "β No image / GΓΆrΓΌntΓΌ
|
| 16 |
|
| 17 |
-
# Convert BGR to RGB
|
| 18 |
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 19 |
output_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
|
| 20 |
|
| 21 |
feedback = []
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
if analysis_type == "enhanced" and (age or height or weight):
|
| 25 |
profile_info = []
|
|
|
|
|
|
|
| 26 |
if age:
|
| 27 |
-
profile_info.append(f"Age: {age}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if height and weight:
|
| 29 |
bmi = weight / ((height/100) ** 2)
|
| 30 |
-
profile_info.append(f"BMI: {bmi:.1f}")
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
if profile_info:
|
| 35 |
-
feedback.append(f"π€
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
feedback.append("")
|
| 37 |
|
| 38 |
if analysis_type == "hand":
|
| 39 |
-
# Hand
|
| 40 |
with mp_hands.Hands(
|
| 41 |
static_image_mode=False,
|
| 42 |
max_num_hands=2,
|
|
@@ -47,25 +94,91 @@ def analyze_posture(image, analysis_type="basic", age=None, height=None, weight=
|
|
| 47 |
|
| 48 |
if results.multi_hand_landmarks:
|
| 49 |
hand_count = len(results.multi_hand_landmarks)
|
| 50 |
-
feedback.append(f"β
{hand_count}
|
|
|
|
| 51 |
|
| 52 |
for idx, hand_landmarks in enumerate(results.multi_hand_landmarks):
|
| 53 |
mp_drawing.draw_landmarks(output_image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 54 |
|
| 55 |
-
#
|
| 56 |
landmarks = hand_landmarks.landmark
|
| 57 |
fingers_up = 0
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
| 60 |
|
|
|
|
| 61 |
for i in range(5):
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
fingers_up += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
-
feedback.append("β
|
| 68 |
-
feedback.append("ποΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
else:
|
| 71 |
# Posture analysis
|
|
@@ -88,33 +201,66 @@ def analyze_posture(image, analysis_type="basic", age=None, height=None, weight=
|
|
| 88 |
if landmarks[mp_pose.PoseLandmark.NOSE.value].visibility > 0.5:
|
| 89 |
visible_parts.append("Head")
|
| 90 |
|
| 91 |
-
#
|
| 92 |
left_shoulder = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
|
| 93 |
right_shoulder = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value]
|
| 94 |
|
| 95 |
if left_shoulder.visibility > 0.5 and right_shoulder.visibility > 0.5:
|
| 96 |
visible_parts.append("Shoulders")
|
| 97 |
|
| 98 |
-
#
|
| 99 |
shoulder_diff = abs(left_shoulder.y - right_shoulder.y)
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
if left_shoulder.y < right_shoulder.y:
|
| 102 |
-
feedback.append("β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
else:
|
| 104 |
-
feedback.append("β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
-
feedback.append("β
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
| 109 |
left_elbow = landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value]
|
| 110 |
right_elbow = landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value]
|
| 111 |
|
| 112 |
if left_elbow.visibility > 0.5 and right_elbow.visibility > 0.5:
|
| 113 |
visible_parts.append("Elbows")
|
| 114 |
|
| 115 |
-
#
|
| 116 |
try:
|
| 117 |
def calculate_angle(a, b, c):
|
|
|
|
| 118 |
a = np.array(a)
|
| 119 |
b = np.array(b)
|
| 120 |
c = np.array(c)
|
|
@@ -124,90 +270,359 @@ def analyze_posture(image, analysis_type="basic", age=None, height=None, weight=
|
|
| 124 |
angle = 360 - angle
|
| 125 |
return angle
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
left_shoulder_pos = [left_shoulder.x, left_shoulder.y]
|
| 129 |
left_elbow_pos = [left_elbow.x, left_elbow.y]
|
| 130 |
left_wrist_pos = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x,
|
| 131 |
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
|
| 132 |
|
| 133 |
left_angle = calculate_angle(left_shoulder_pos, left_elbow_pos, left_wrist_pos)
|
| 134 |
-
feedback.append(
|
| 135 |
|
| 136 |
-
# Right elbow
|
| 137 |
right_shoulder_pos = [right_shoulder.x, right_shoulder.y]
|
| 138 |
right_elbow_pos = [right_elbow.x, right_elbow.y]
|
| 139 |
right_wrist_pos = [landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x,
|
| 140 |
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y]
|
| 141 |
|
| 142 |
right_angle = calculate_angle(right_shoulder_pos, right_elbow_pos, right_wrist_pos)
|
| 143 |
-
feedback.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
-
except:
|
| 146 |
-
feedback.append("β οΈ
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
#
|
| 149 |
left_hip = landmarks[mp_pose.PoseLandmark.LEFT_HIP.value]
|
| 150 |
right_hip = landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value]
|
| 151 |
|
| 152 |
if left_hip.visibility > 0.5 and right_hip.visibility > 0.5:
|
| 153 |
visible_parts.append("Hips")
|
| 154 |
|
|
|
|
| 155 |
hip_diff = abs(left_hip.y - right_hip.y)
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
if left_hip.y < right_hip.y:
|
| 158 |
-
feedback.append("β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
else:
|
| 160 |
-
feedback.append("β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
else:
|
| 162 |
-
feedback.append("β
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
#
|
| 165 |
nose = landmarks[mp_pose.PoseLandmark.NOSE.value]
|
| 166 |
if nose.visibility > 0.5:
|
| 167 |
shoulder_center_x = (left_shoulder.x + right_shoulder.x) / 2
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
if nose.x < shoulder_center_x:
|
| 172 |
-
feedback.append("
|
|
|
|
|
|
|
|
|
|
| 173 |
else:
|
| 174 |
-
feedback.append("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
else:
|
| 176 |
-
feedback.append("
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
# Age-
|
| 179 |
if analysis_type == "enhanced" and age:
|
| 180 |
feedback.append("")
|
| 181 |
-
feedback.append("π―
|
|
|
|
| 182 |
if age < 25:
|
| 183 |
-
feedback.append("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
elif age < 45:
|
| 185 |
-
feedback.append("
|
| 186 |
-
|
| 187 |
-
feedback.append("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
-
#
|
| 190 |
if visible_parts:
|
| 191 |
-
feedback.insert(0, f"β
|
| 192 |
-
feedback.insert(1, "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
else:
|
| 194 |
-
feedback.append("β
|
| 195 |
-
feedback.append("π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
gr.Markdown("""
|
| 203 |
-
# π― Pose-Think: AI
|
| 204 |
-
##
|
| 205 |
|
| 206 |
-
**
|
| 207 |
""")
|
| 208 |
|
| 209 |
with gr.Row():
|
| 210 |
-
with gr.Column():
|
|
|
|
|
|
|
| 211 |
# Analysis type selection
|
| 212 |
analysis_type = gr.Radio(
|
| 213 |
choices=[
|
|
@@ -220,62 +635,120 @@ with gr.Blocks(title="π― Pose-Think: AI Movement Analysis") as demo:
|
|
| 220 |
)
|
| 221 |
|
| 222 |
# Profile info (for enhanced mode)
|
| 223 |
-
gr.Markdown("### π€
|
| 224 |
-
age_input = gr.Number(label="Age", minimum=10, maximum=100, value=None)
|
| 225 |
height_input = gr.Number(label="Height (cm)", minimum=100, maximum=250, value=None)
|
| 226 |
weight_input = gr.Number(label="Weight (kg)", minimum=30, maximum=200, value=None)
|
| 227 |
|
| 228 |
-
#
|
| 229 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
-
#
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
with gr.Column():
|
| 235 |
-
#
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
-
#
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
)
|
| 249 |
|
| 250 |
-
#
|
| 251 |
-
|
| 252 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
-
###
|
| 255 |
-
-
|
| 256 |
-
-
|
| 257 |
-
-
|
|
|
|
|
|
|
| 258 |
|
| 259 |
-
###
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
|
| 266 |
-
###
|
| 267 |
-
-
|
| 268 |
-
-
|
| 269 |
-
-
|
| 270 |
-
-
|
| 271 |
-
-
|
| 272 |
|
| 273 |
-
### π‘ **
|
| 274 |
-
- **
|
| 275 |
-
- **
|
| 276 |
-
- **
|
| 277 |
-
- **
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
""")
|
| 279 |
|
| 280 |
if __name__ == "__main__":
|
| 281 |
-
demo.launch(
|
|
|
|
|
|
| 1 |
+
# Pose-Think: AI-Powered Movement Analysis Suite - REAL-TIME VERSION
|
| 2 |
import cv2
|
| 3 |
import mediapipe as mp
|
| 4 |
import gradio as gr
|
|
|
|
| 10 |
mp_drawing = mp.solutions.drawing_utils
|
| 11 |
|
| 12 |
def analyze_posture(image, analysis_type="basic", age=None, height=None, weight=None):
|
| 13 |
+
"""Advanced AI-powered posture analysis with biomechanical insights"""
|
| 14 |
if image is None:
|
| 15 |
+
return None, "β No image detected / GΓΆrΓΌntΓΌ tespit edilemedi"
|
| 16 |
|
| 17 |
+
# Convert BGR to RGB for MediaPipe processing
|
| 18 |
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 19 |
output_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR)
|
| 20 |
|
| 21 |
feedback = []
|
| 22 |
+
technical_analysis = []
|
| 23 |
+
health_recommendations = []
|
| 24 |
|
| 25 |
+
# Enhanced profile analysis with biomechanical calculations
|
| 26 |
if analysis_type == "enhanced" and (age or height or weight):
|
| 27 |
profile_info = []
|
| 28 |
+
biomechanical_analysis = []
|
| 29 |
+
|
| 30 |
if age:
|
| 31 |
+
profile_info.append(f"Age: {age} years")
|
| 32 |
+
|
| 33 |
+
# Age-related biomechanical insights
|
| 34 |
+
if age < 25:
|
| 35 |
+
biomechanical_analysis.append("𧬠YOUNG ADULT: Peak bone density period (20-30 years)")
|
| 36 |
+
biomechanical_analysis.append("πͺ Muscle elasticity optimal - Focus on movement patterns")
|
| 37 |
+
biomechanical_analysis.append("β‘ High metabolic rate supports posture correction")
|
| 38 |
+
elif age < 45:
|
| 39 |
+
biomechanical_analysis.append("𧬠ADULT: Bone density maintenance phase")
|
| 40 |
+
biomechanical_analysis.append("πͺ Muscle tone declining ~1% annually after 30")
|
| 41 |
+
biomechanical_analysis.append("β οΈ Early prevention of postural deviations critical")
|
| 42 |
+
elif age < 65:
|
| 43 |
+
biomechanical_analysis.append("𧬠MIDDLE-AGED: Accelerated bone density loss (~1-2%/year)")
|
| 44 |
+
biomechanical_analysis.append("πͺ Sarcopenia onset - muscle mass loss 3-8%/decade")
|
| 45 |
+
biomechanical_analysis.append("𦴠Increased risk of vertebral compression fractures")
|
| 46 |
+
else:
|
| 47 |
+
biomechanical_analysis.append("𧬠SENIOR: Significant structural changes in spine")
|
| 48 |
+
biomechanical_analysis.append("πͺ Advanced sarcopenia - strength loss 1.5-3%/year")
|
| 49 |
+
biomechanical_analysis.append("β οΈ High priority: Fall prevention and spinal stability")
|
| 50 |
+
|
| 51 |
if height and weight:
|
| 52 |
bmi = weight / ((height/100) ** 2)
|
| 53 |
+
profile_info.append(f"BMI: {bmi:.1f} kg/mΒ²")
|
| 54 |
+
|
| 55 |
+
# BMI-based biomechanical analysis
|
| 56 |
+
if bmi < 18.5:
|
| 57 |
+
biomechanical_analysis.append(f"π UNDERWEIGHT (BMI {bmi:.1f}): Reduced muscle mass affects postural stability")
|
| 58 |
+
biomechanical_analysis.append("β οΈ Low body weight may indicate insufficient core strength")
|
| 59 |
+
health_recommendations.append("π Increase protein intake (1.6-2.2g/kg body weight)")
|
| 60 |
+
health_recommendations.append("ποΈ Resistance training to build postural muscles")
|
| 61 |
+
elif bmi < 25:
|
| 62 |
+
biomechanical_analysis.append(f"β
NORMAL WEIGHT (BMI {bmi:.1f}): Optimal loading for spinal structures")
|
| 63 |
+
biomechanical_analysis.append("πͺ Good weight distribution reduces joint stress")
|
| 64 |
+
elif bmi < 30:
|
| 65 |
+
biomechanical_analysis.append(f"β οΈ OVERWEIGHT (BMI {bmi:.1f}): Increased anterior load on lumbar spine")
|
| 66 |
+
biomechanical_analysis.append("π Extra weight creates ~4x increased knee joint stress")
|
| 67 |
+
biomechanical_analysis.append("𦴠Elevated risk of lower crossed syndrome")
|
| 68 |
+
health_recommendations.append("π Cardio exercise: 150min/week moderate intensity")
|
| 69 |
+
health_recommendations.append("π§ Core strengthening to counter anterior weight shift")
|
| 70 |
+
else:
|
| 71 |
+
biomechanical_analysis.append(f"π¨ OBESE (BMI {bmi:.1f}): Significant postural compensation patterns")
|
| 72 |
+
biomechanical_analysis.append("π Increased thoracic kyphosis and lumbar lordosis likely")
|
| 73 |
+
biomechanical_analysis.append("β οΈ High risk: sleep apnea affecting cervical posture")
|
| 74 |
+
health_recommendations.append("π©Ί Medical consultation recommended for weight management")
|
| 75 |
+
health_recommendations.append("π Low-impact exercise (swimming, aqua therapy)")
|
| 76 |
|
| 77 |
if profile_info:
|
| 78 |
+
feedback.append(f"π€ BIOMETRIC PROFILE: {' | '.join(profile_info)}")
|
| 79 |
+
feedback.append("")
|
| 80 |
+
|
| 81 |
+
if biomechanical_analysis:
|
| 82 |
+
feedback.extend(biomechanical_analysis)
|
| 83 |
feedback.append("")
|
| 84 |
|
| 85 |
if analysis_type == "hand":
|
| 86 |
+
# Advanced Hand Analysis with Biomechanical Insights
|
| 87 |
with mp_hands.Hands(
|
| 88 |
static_image_mode=False,
|
| 89 |
max_num_hands=2,
|
|
|
|
| 94 |
|
| 95 |
if results.multi_hand_landmarks:
|
| 96 |
hand_count = len(results.multi_hand_landmarks)
|
| 97 |
+
feedback.append(f"β
HAND DETECTION: {hand_count} hand(s) identified")
|
| 98 |
+
technical_analysis.append("π¬ TECHNICAL ANALYSIS:")
|
| 99 |
|
| 100 |
for idx, hand_landmarks in enumerate(results.multi_hand_landmarks):
|
| 101 |
mp_drawing.draw_landmarks(output_image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
|
| 102 |
|
| 103 |
+
# Advanced finger analysis
|
| 104 |
landmarks = hand_landmarks.landmark
|
| 105 |
fingers_up = 0
|
| 106 |
+
finger_angles = []
|
| 107 |
+
tip_ids = [4, 8, 12, 16, 20] # Thumb, Index, Middle, Ring, Pinky tips
|
| 108 |
+
pip_ids = [3, 6, 10, 14, 18] # PIP joints
|
| 109 |
+
mcp_ids = [2, 5, 9, 13, 17] # MCP joints
|
| 110 |
+
finger_names = ["Thumb", "Index", "Middle", "Ring", "Pinky"]
|
| 111 |
|
| 112 |
+
# Calculate finger extension angles
|
| 113 |
for i in range(5):
|
| 114 |
+
tip_y = landmarks[tip_ids[i]].y
|
| 115 |
+
pip_y = landmarks[pip_ids[i]].y
|
| 116 |
+
mcp_y = landmarks[mcp_ids[i]].y
|
| 117 |
+
|
| 118 |
+
if tip_y < pip_y: # Finger extended
|
| 119 |
fingers_up += 1
|
| 120 |
+
# Calculate extension angle
|
| 121 |
+
if i > 0: # Not thumb
|
| 122 |
+
extension_angle = abs(np.arctan2(tip_y - mcp_y, landmarks[tip_ids[i]].x - landmarks[mcp_ids[i]].x) * 180 / np.pi)
|
| 123 |
+
finger_angles.append(f"{finger_names[i]}: {extension_angle:.1f}Β°")
|
| 124 |
+
|
| 125 |
+
# Hand analysis results
|
| 126 |
+
hand_side = "Right" if idx == 0 else "Left"
|
| 127 |
+
feedback.append(f"ποΈ {hand_side} HAND ANALYSIS:")
|
| 128 |
+
feedback.append(f" β’ Fingers Extended: {fingers_up}/5")
|
| 129 |
+
feedback.append(f" β’ Hand Symmetry: {'Normal' if hand_count == 2 else 'Single hand detected'}")
|
| 130 |
|
| 131 |
+
# Technical measurements
|
| 132 |
+
if finger_angles:
|
| 133 |
+
technical_analysis.append(f"π {hand_side} Hand Extension Angles:")
|
| 134 |
+
for angle in finger_angles:
|
| 135 |
+
technical_analysis.append(f" β’ {angle}")
|
| 136 |
+
|
| 137 |
+
# Grip strength estimation based on finger positions
|
| 138 |
+
if fingers_up == 0:
|
| 139 |
+
feedback.append(" β’ Grip Pattern: FULL FIST - Strong grip indication")
|
| 140 |
+
technical_analysis.append("πͺ Estimated grip strength: HIGH (all digits flexed)")
|
| 141 |
+
elif fingers_up == 5:
|
| 142 |
+
feedback.append(" β’ Grip Pattern: OPEN HAND - Relaxed extension")
|
| 143 |
+
technical_analysis.append("π€ Hand state: RELAXED (full extension)")
|
| 144 |
+
else:
|
| 145 |
+
feedback.append(f" β’ Grip Pattern: PARTIAL ({fingers_up} digits extended)")
|
| 146 |
+
technical_analysis.append(f"β‘ Mixed pattern: {fingers_up} extensors active")
|
| 147 |
+
|
| 148 |
+
# Neurological assessment
|
| 149 |
+
wrist_landmark = landmarks[0] # Wrist center
|
| 150 |
+
middle_finger_tip = landmarks[12] # Middle finger tip
|
| 151 |
+
hand_span = np.sqrt((middle_finger_tip.x - wrist_landmark.x)**2 +
|
| 152 |
+
(middle_finger_tip.y - wrist_landmark.y)**2)
|
| 153 |
+
|
| 154 |
+
technical_analysis.append(f"π§ Neurological Indicators:")
|
| 155 |
+
technical_analysis.append(f" β’ Hand-span ratio: {hand_span:.3f}")
|
| 156 |
+
technical_analysis.append(f" β’ Fine motor control: {'GOOD' if fingers_up > 0 else 'GRIP DOMINANT'}")
|
| 157 |
+
|
| 158 |
+
if fingers_up == 1: # Pointing gesture
|
| 159 |
+
technical_analysis.append("βοΈ Index pointing detected - Precise motor control active")
|
| 160 |
+
elif fingers_up == 2: # Peace sign or similar
|
| 161 |
+
technical_analysis.append("βοΈ Dual digit extension - Good finger independence")
|
| 162 |
+
|
| 163 |
+
# Overall hand health assessment
|
| 164 |
+
feedback.append("")
|
| 165 |
+
feedback.append("π₯ HAND HEALTH ASSESSMENT:")
|
| 166 |
+
if hand_count == 2:
|
| 167 |
+
feedback.append("β
Bilateral hand function - Normal neurological presentation")
|
| 168 |
+
health_recommendations.append("π€² Continue bilateral activities for brain health")
|
| 169 |
+
else:
|
| 170 |
+
feedback.append("β οΈ Single hand visible - Ensure bilateral movement patterns")
|
| 171 |
+
health_recommendations.append("π Practice bilateral coordination exercises")
|
| 172 |
+
|
| 173 |
else:
|
| 174 |
+
feedback.append("β NO HANDS DETECTED")
|
| 175 |
+
feedback.append("ποΈ Position hands clearly in camera view")
|
| 176 |
+
technical_analysis.append("π Detection Parameters:")
|
| 177 |
+
technical_analysis.append(" β’ Minimum confidence: 50%")
|
| 178 |
+
technical_analysis.append(" β’ Tracking confidence: 50%")
|
| 179 |
+
technical_analysis.append(" β’ Max hands: 2")
|
| 180 |
+
health_recommendations.append("π‘ Ensure good lighting for hand visibility")
|
| 181 |
+
health_recommendations.append("πΌοΈ Use contrasting background for better detection")
|
| 182 |
|
| 183 |
else:
|
| 184 |
# Posture analysis
|
|
|
|
| 201 |
if landmarks[mp_pose.PoseLandmark.NOSE.value].visibility > 0.5:
|
| 202 |
visible_parts.append("Head")
|
| 203 |
|
| 204 |
+
# Advanced Shoulder Analysis with Biomechanical Assessment
|
| 205 |
left_shoulder = landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value]
|
| 206 |
right_shoulder = landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value]
|
| 207 |
|
| 208 |
if left_shoulder.visibility > 0.5 and right_shoulder.visibility > 0.5:
|
| 209 |
visible_parts.append("Shoulders")
|
| 210 |
|
| 211 |
+
# Calculate shoulder level difference in degrees and millimeters
|
| 212 |
shoulder_diff = abs(left_shoulder.y - right_shoulder.y)
|
| 213 |
+
shoulder_angle = np.arctan2(right_shoulder.y - left_shoulder.y,
|
| 214 |
+
right_shoulder.x - left_shoulder.x) * 180 / np.pi
|
| 215 |
+
|
| 216 |
+
# Estimate actual measurements (assuming average shoulder width of 40cm)
|
| 217 |
+
shoulder_width_pixels = abs(right_shoulder.x - left_shoulder.x)
|
| 218 |
+
if shoulder_width_pixels > 0:
|
| 219 |
+
pixel_to_mm = 400 / shoulder_width_pixels # 400mm average shoulder width
|
| 220 |
+
height_diff_mm = shoulder_diff * pixel_to_mm * 1000 # Convert to mm
|
| 221 |
+
else:
|
| 222 |
+
height_diff_mm = 0
|
| 223 |
+
|
| 224 |
+
technical_analysis.append("π¬ SHOULDER BIOMECHANICS:")
|
| 225 |
+
technical_analysis.append(f" β’ Shoulder angle: {abs(shoulder_angle):.1f}Β° from horizontal")
|
| 226 |
+
technical_analysis.append(f" β’ Height difference: ~{height_diff_mm:.0f}mm")
|
| 227 |
+
technical_analysis.append(f" β’ Asymmetry ratio: {(shoulder_diff * 100):.1f}%")
|
| 228 |
+
|
| 229 |
+
if shoulder_diff > 0.05: # >5% asymmetry
|
| 230 |
if left_shoulder.y < right_shoulder.y:
|
| 231 |
+
feedback.append(f"β οΈ LEFT SHOULDER ELEVATED: {height_diff_mm:.0f}mm higher")
|
| 232 |
+
technical_analysis.append("π§ Possible causes: Left upper trap hyperactivity")
|
| 233 |
+
technical_analysis.append("𦴠Compensation: Right lateral flexion likely")
|
| 234 |
+
health_recommendations.append("π Left upper trapezius stretching (30s x 3)")
|
| 235 |
+
health_recommendations.append("πͺ Right side strengthening exercises")
|
| 236 |
else:
|
| 237 |
+
feedback.append(f"β οΈ RIGHT SHOULDER ELEVATED: {height_diff_mm:.0f}mm higher")
|
| 238 |
+
technical_analysis.append("π§ Possible causes: Right upper trap hyperactivity")
|
| 239 |
+
technical_analysis.append("𦴠Compensation: Left lateral flexion likely")
|
| 240 |
+
health_recommendations.append("π Right upper trapezius stretching (30s x 3)")
|
| 241 |
+
health_recommendations.append("πͺ Left side strengthening exercises")
|
| 242 |
+
|
| 243 |
+
# Additional clinical insights
|
| 244 |
+
if shoulder_diff > 0.08: # >8% asymmetry
|
| 245 |
+
feedback.append("π¨ SIGNIFICANT ASYMMETRY - Clinical assessment recommended")
|
| 246 |
+
technical_analysis.append("β οΈ Risk factors: Scoliosis, muscle imbalance, ergonomic issues")
|
| 247 |
+
health_recommendations.append("π©Ί Consider physiotherapy evaluation")
|
| 248 |
else:
|
| 249 |
+
feedback.append("β
SHOULDERS LEVEL - Excellent postural symmetry")
|
| 250 |
+
technical_analysis.append("β
Normal shoulder girdle alignment")
|
| 251 |
+
feedback.append(f"π Asymmetry: {height_diff_mm:.0f}mm (Normal: <20mm)")
|
| 252 |
|
| 253 |
+
# Advanced Elbow Joint Analysis with Biomechanical Calculations
|
| 254 |
left_elbow = landmarks[mp_pose.PoseLandmark.LEFT_ELBOW.value]
|
| 255 |
right_elbow = landmarks[mp_pose.PoseLandmark.RIGHT_ELBOW.value]
|
| 256 |
|
| 257 |
if left_elbow.visibility > 0.5 and right_elbow.visibility > 0.5:
|
| 258 |
visible_parts.append("Elbows")
|
| 259 |
|
| 260 |
+
# Enhanced elbow angle calculations with clinical interpretation
|
| 261 |
try:
|
| 262 |
def calculate_angle(a, b, c):
|
| 263 |
+
"""Calculate angle between three points using vector math"""
|
| 264 |
a = np.array(a)
|
| 265 |
b = np.array(b)
|
| 266 |
c = np.array(c)
|
|
|
|
| 270 |
angle = 360 - angle
|
| 271 |
return angle
|
| 272 |
|
| 273 |
+
def interpret_elbow_angle(angle, side):
|
| 274 |
+
"""Clinical interpretation of elbow angles"""
|
| 275 |
+
if angle > 165:
|
| 276 |
+
return f"β
{side} elbow: FULL EXTENSION ({angle:.1f}Β°) - Excellent mobility"
|
| 277 |
+
elif angle > 140:
|
| 278 |
+
return f"β
{side} elbow: GOOD EXTENSION ({angle:.1f}Β°) - Normal range"
|
| 279 |
+
elif angle > 90:
|
| 280 |
+
return f"β οΈ {side} elbow: MODERATE FLEXION ({angle:.1f}Β°) - Check for tension"
|
| 281 |
+
else:
|
| 282 |
+
return f"π¨ {side} elbow: EXCESSIVE FLEXION ({angle:.1f}Β°) - Requires attention"
|
| 283 |
+
|
| 284 |
+
# Left elbow biomechanical analysis
|
| 285 |
left_shoulder_pos = [left_shoulder.x, left_shoulder.y]
|
| 286 |
left_elbow_pos = [left_elbow.x, left_elbow.y]
|
| 287 |
left_wrist_pos = [landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].x,
|
| 288 |
landmarks[mp_pose.PoseLandmark.LEFT_WRIST.value].y]
|
| 289 |
|
| 290 |
left_angle = calculate_angle(left_shoulder_pos, left_elbow_pos, left_wrist_pos)
|
| 291 |
+
feedback.append(interpret_elbow_angle(left_angle, "LEFT"))
|
| 292 |
|
| 293 |
+
# Right elbow biomechanical analysis
|
| 294 |
right_shoulder_pos = [right_shoulder.x, right_shoulder.y]
|
| 295 |
right_elbow_pos = [right_elbow.x, right_elbow.y]
|
| 296 |
right_wrist_pos = [landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].x,
|
| 297 |
landmarks[mp_pose.PoseLandmark.RIGHT_WRIST.value].y]
|
| 298 |
|
| 299 |
right_angle = calculate_angle(right_shoulder_pos, right_elbow_pos, right_wrist_pos)
|
| 300 |
+
feedback.append(interpret_elbow_angle(right_angle, "RIGHT"))
|
| 301 |
+
|
| 302 |
+
# Bilateral comparison and clinical insights
|
| 303 |
+
angle_difference = abs(left_angle - right_angle)
|
| 304 |
+
technical_analysis.append("οΏ½ ELBOW JOINT BIOMECHANICS:")
|
| 305 |
+
technical_analysis.append(f" β’ Left elbow angle: {left_angle:.1f}Β° (Normal: 0-150Β°)")
|
| 306 |
+
technical_analysis.append(f" β’ Right elbow angle: {right_angle:.1f}Β° (Normal: 0-150Β°)")
|
| 307 |
+
technical_analysis.append(f" β’ Bilateral difference: {angle_difference:.1f}Β°")
|
| 308 |
+
|
| 309 |
+
if angle_difference > 15:
|
| 310 |
+
feedback.append(f"β οΈ ELBOW ASYMMETRY: {angle_difference:.1f}Β° difference")
|
| 311 |
+
technical_analysis.append("π§ Possible causes: Unilateral muscle imbalance")
|
| 312 |
+
health_recommendations.append("π Bilateral stretching and strengthening")
|
| 313 |
+
health_recommendations.append("ποΈ Focus on weaker side strengthening")
|
| 314 |
+
else:
|
| 315 |
+
feedback.append(f"β
ELBOW SYMMETRY: Good bilateral balance ({angle_difference:.1f}Β°)")
|
| 316 |
+
|
| 317 |
+
# Movement quality assessment
|
| 318 |
+
avg_angle = (left_angle + right_angle) / 2
|
| 319 |
+
if avg_angle < 90:
|
| 320 |
+
technical_analysis.append("π Pattern: FLEXION DOMINANT - Possible anterior head posture")
|
| 321 |
+
health_recommendations.append("π± Reduce screen time and improve ergonomics")
|
| 322 |
+
health_recommendations.append("π§ Chest stretches and posterior deltoid strengthening")
|
| 323 |
+
elif avg_angle > 150:
|
| 324 |
+
technical_analysis.append("π Pattern: EXTENSION DOMINANT - Good postural alignment")
|
| 325 |
+
health_recommendations.append("β
Maintain current activity patterns")
|
| 326 |
+
|
| 327 |
+
# Clinical range of motion assessment
|
| 328 |
+
if left_angle < 30 or right_angle < 30:
|
| 329 |
+
feedback.append("π¨ SEVERE FLEXION CONTRACTURE detected")
|
| 330 |
+
technical_analysis.append("β οΈ ROM limitation: May indicate joint pathology")
|
| 331 |
+
health_recommendations.append("π©Ί Urgent: Consult orthopedic specialist")
|
| 332 |
|
| 333 |
+
except Exception as e:
|
| 334 |
+
feedback.append("β οΈ ELBOW ANALYSIS ERROR: Unable to calculate joint angles")
|
| 335 |
+
technical_analysis.append(f"π§ Error details: {str(e)[:50]}...")
|
| 336 |
+
technical_analysis.append("π‘ Ensure clear visibility of shoulder-elbow-wrist alignment")
|
| 337 |
|
| 338 |
+
# Advanced Hip Analysis with Pelvic Biomechanics
|
| 339 |
left_hip = landmarks[mp_pose.PoseLandmark.LEFT_HIP.value]
|
| 340 |
right_hip = landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value]
|
| 341 |
|
| 342 |
if left_hip.visibility > 0.5 and right_hip.visibility > 0.5:
|
| 343 |
visible_parts.append("Hips")
|
| 344 |
|
| 345 |
+
# Calculate pelvic obliquity and rotation
|
| 346 |
hip_diff = abs(left_hip.y - right_hip.y)
|
| 347 |
+
pelvic_angle = np.arctan2(right_hip.y - left_hip.y,
|
| 348 |
+
right_hip.x - left_hip.x) * 180 / np.pi
|
| 349 |
+
|
| 350 |
+
# Estimate pelvic measurements
|
| 351 |
+
hip_width_pixels = abs(right_hip.x - left_hip.x)
|
| 352 |
+
if hip_width_pixels > 0:
|
| 353 |
+
pixel_to_mm = 300 / hip_width_pixels # 300mm average hip width
|
| 354 |
+
pelvic_tilt_mm = hip_diff * pixel_to_mm * 1000
|
| 355 |
+
else:
|
| 356 |
+
pelvic_tilt_mm = 0
|
| 357 |
+
|
| 358 |
+
technical_analysis.append("π¬ PELVIC BIOMECHANICS:")
|
| 359 |
+
technical_analysis.append(f" β’ Pelvic obliquity: {abs(pelvic_angle):.1f}Β° from horizontal")
|
| 360 |
+
technical_analysis.append(f" β’ Hip height difference: ~{pelvic_tilt_mm:.0f}mm")
|
| 361 |
+
technical_analysis.append(f" β’ Frontal plane deviation: {(hip_diff * 100):.1f}%")
|
| 362 |
+
|
| 363 |
+
if hip_diff > 0.03: # >3% asymmetry
|
| 364 |
if left_hip.y < right_hip.y:
|
| 365 |
+
feedback.append(f"β οΈ LEFT HIP ELEVATED: {pelvic_tilt_mm:.0f}mm higher")
|
| 366 |
+
technical_analysis.append("𦴠Pelvic pattern: Left lateral pelvic tilt")
|
| 367 |
+
technical_analysis.append("π§ Compensation: Right hip adductor tightness likely")
|
| 368 |
+
health_recommendations.append("π§ Right hip adductor stretching (30s x 3)")
|
| 369 |
+
health_recommendations.append("πͺ Left gluteus medius strengthening")
|
| 370 |
+
health_recommendations.append("π Single-leg balance exercises")
|
| 371 |
else:
|
| 372 |
+
feedback.append(f"β οΈ RIGHT HIP ELEVATED: {pelvic_tilt_mm:.0f}mm higher")
|
| 373 |
+
technical_analysis.append("𦴠Pelvic pattern: Right lateral pelvic tilt")
|
| 374 |
+
technical_analysis.append("π§ Compensation: Left hip adductor tightness likely")
|
| 375 |
+
health_recommendations.append("π§ Left hip adductor stretching (30s x 3)")
|
| 376 |
+
health_recommendations.append("πͺ Right gluteus medius strengthening")
|
| 377 |
+
health_recommendations.append("π Single-leg balance exercises")
|
| 378 |
+
|
| 379 |
+
# Clinical implications
|
| 380 |
+
if hip_diff > 0.05: # >5% asymmetry
|
| 381 |
+
feedback.append("π¨ SIGNIFICANT PELVIC OBLIQUITY - Assessment recommended")
|
| 382 |
+
technical_analysis.append("β οΈ Risk factors: Scoliosis, leg length discrepancy, SI joint dysfunction")
|
| 383 |
+
health_recommendations.append("π©Ί Consider pelvic assessment by physiotherapist")
|
| 384 |
+
health_recommendations.append("π Leg length measurement recommended")
|
| 385 |
else:
|
| 386 |
+
feedback.append("β
HIPS LEVEL - Excellent pelvic alignment")
|
| 387 |
+
technical_analysis.append("β
Normal frontal plane pelvic position")
|
| 388 |
+
feedback.append(f"π Pelvic obliquity: {pelvic_tilt_mm:.0f}mm (Normal: <15mm)")
|
| 389 |
|
| 390 |
+
# Advanced Cervical Spine and Head Position Analysis
|
| 391 |
nose = landmarks[mp_pose.PoseLandmark.NOSE.value]
|
| 392 |
if nose.visibility > 0.5:
|
| 393 |
shoulder_center_x = (left_shoulder.x + right_shoulder.x) / 2
|
| 394 |
+
shoulder_center_y = (left_shoulder.y + right_shoulder.y) / 2
|
| 395 |
+
|
| 396 |
+
# Calculate forward head posture
|
| 397 |
+
head_offset_x = abs(nose.x - shoulder_center_x)
|
| 398 |
+
head_offset_y = nose.y - shoulder_center_y
|
| 399 |
+
|
| 400 |
+
# Calculate craniovertebral angle approximation
|
| 401 |
+
cv_angle = np.arctan2(head_offset_y, head_offset_x) * 180 / np.pi
|
| 402 |
|
| 403 |
+
technical_analysis.append("π¬ CERVICAL SPINE BIOMECHANICS:")
|
| 404 |
+
technical_analysis.append(f" β’ Forward head posture: {head_offset_x:.3f} ratio")
|
| 405 |
+
technical_analysis.append(f" β’ Craniovertebral angle: ~{abs(cv_angle):.1f}Β°")
|
| 406 |
+
technical_analysis.append(f" β’ Head position: {head_offset_y:.3f} vertical offset")
|
| 407 |
+
|
| 408 |
+
if head_offset_x > 0.08: # Significant forward head posture
|
| 409 |
if nose.x < shoulder_center_x:
|
| 410 |
+
feedback.append(f"β οΈ FORWARD HEAD POSTURE: Left deviation ({head_offset_x:.2f})")
|
| 411 |
+
technical_analysis.append("π§ Pattern: Left cervical side-bending with rotation")
|
| 412 |
+
health_recommendations.append("π Right cervical rotation stretches")
|
| 413 |
+
health_recommendations.append("οΏ½ Deep neck flexor strengthening")
|
| 414 |
else:
|
| 415 |
+
feedback.append(f"β οΈ FORWARD HEAD POSTURE: Right deviation ({head_offset_x:.2f})")
|
| 416 |
+
technical_analysis.append("π§ Pattern: Right cervical side-bending with rotation")
|
| 417 |
+
health_recommendations.append("π Left cervical rotation stretches")
|
| 418 |
+
health_recommendations.append("πͺ Deep neck flexor strengthening")
|
| 419 |
+
|
| 420 |
+
# Forward head posture implications
|
| 421 |
+
if abs(cv_angle) < 45: # Severe forward head posture
|
| 422 |
+
feedback.append("οΏ½ SEVERE FORWARD HEAD POSTURE detected")
|
| 423 |
+
technical_analysis.append("β οΈ Risk: Upper cervical hyperextension, suboccipital tension")
|
| 424 |
+
health_recommendations.append("π± Immediate ergonomic assessment required")
|
| 425 |
+
health_recommendations.append("π©Ί Consider cervical spine evaluation")
|
| 426 |
+
|
| 427 |
+
# Muscle imbalance analysis
|
| 428 |
+
technical_analysis.append("πͺ Muscle imbalance pattern:")
|
| 429 |
+
technical_analysis.append(" β’ Tight: Upper trapezius, levator scapulae, suboccipitals")
|
| 430 |
+
technical_analysis.append(" β’ Weak: Deep neck flexors, lower trapezius")
|
| 431 |
+
|
| 432 |
+
health_recommendations.append("π§ Chin tuck exercises (10 reps x 3 sets)")
|
| 433 |
+
health_recommendations.append("π Upper trapezius and levator scapulae stretching")
|
| 434 |
+
health_recommendations.append("π₯οΈ Monitor height adjustment (top 1/3 at eye level)")
|
| 435 |
+
|
| 436 |
else:
|
| 437 |
+
feedback.append("β
NECK CENTERED - Optimal cervical alignment")
|
| 438 |
+
technical_analysis.append("β
Normal cervical lordosis maintenance")
|
| 439 |
+
if abs(cv_angle) > 50: # Good craniovertebral angle
|
| 440 |
+
feedback.append(f"π Craniovertebral angle: {abs(cv_angle):.1f}Β° (Excellent: >50Β°)")
|
| 441 |
|
| 442 |
+
# Enhanced Age-Specific Biomechanical Recommendations
|
| 443 |
if analysis_type == "enhanced" and age:
|
| 444 |
feedback.append("")
|
| 445 |
+
feedback.append("π― AGE-SPECIFIC BIOMECHANICAL ANALYSIS:")
|
| 446 |
+
|
| 447 |
if age < 25:
|
| 448 |
+
feedback.append("𧬠YOUNG ADULT PHASE (Peak Development):")
|
| 449 |
+
feedback.append(" β’ Bone density: Peak accumulation period (90% by age 20)")
|
| 450 |
+
feedback.append(" β’ Muscle mass: Optimal growth potential")
|
| 451 |
+
feedback.append(" β’ Neuroplasticity: High adaptation capacity")
|
| 452 |
+
|
| 453 |
+
technical_analysis.append("π Physiological advantages:")
|
| 454 |
+
technical_analysis.append(" β’ Collagen synthesis: Peak efficiency")
|
| 455 |
+
technical_analysis.append(" β’ Proprioception: Excellent balance control")
|
| 456 |
+
technical_analysis.append(" β’ Recovery rate: 24-48 hours optimal")
|
| 457 |
+
|
| 458 |
+
health_recommendations.extend([
|
| 459 |
+
"π High-impact activities encouraged (bone loading)",
|
| 460 |
+
"π€Έ Proprioceptive training for injury prevention",
|
| 461 |
+
"πͺ Establish proper movement patterns now",
|
| 462 |
+
"π Ergonomic education for lifelong habits"
|
| 463 |
+
])
|
| 464 |
+
|
| 465 |
elif age < 45:
|
| 466 |
+
feedback.append("𧬠ADULT PHASE (Maintenance Period):")
|
| 467 |
+
feedback.append(" β’ Bone density: Peak maintained, slow decline begins (~30)")
|
| 468 |
+
feedback.append(" β’ Muscle mass: 1% annual loss after age 30")
|
| 469 |
+
feedback.append(" β’ Flexibility: Gradual decrease without intervention")
|
| 470 |
+
|
| 471 |
+
technical_analysis.append("π Physiological changes:")
|
| 472 |
+
technical_analysis.append(" β’ Type II muscle fibers: Beginning to decline")
|
| 473 |
+
technical_analysis.append(" β’ Connective tissue: Reduced elasticity")
|
| 474 |
+
technical_analysis.append(" β’ Hormonal: Growth hormone decline affects recovery")
|
| 475 |
+
|
| 476 |
+
health_recommendations.extend([
|
| 477 |
+
"ποΈ Resistance training 2-3x/week (muscle preservation)",
|
| 478 |
+
"π§ Daily stretching routine (maintain flexibility)",
|
| 479 |
+
"βοΈ Weight management crucial for joint health",
|
| 480 |
+
"πΌ Workplace ergonomics assessment recommended"
|
| 481 |
+
])
|
| 482 |
+
|
| 483 |
+
elif age < 65:
|
| 484 |
+
feedback.append("𧬠MIDDLE-AGE PHASE (Active Intervention):")
|
| 485 |
+
feedback.append(" β’ Bone density: Accelerated loss (1-2% annually)")
|
| 486 |
+
feedback.append(" β’ Sarcopenia: 3-8% muscle loss per decade")
|
| 487 |
+
feedback.append(" β’ Balance: Proprioceptive decline increases fall risk")
|
| 488 |
+
|
| 489 |
+
technical_analysis.append("οΏ½ Critical interventions needed:")
|
| 490 |
+
technical_analysis.append(" β’ Calcium absorption: Reduced efficiency")
|
| 491 |
+
technical_analysis.append(" β’ Postural muscles: Significant weakening")
|
| 492 |
+
technical_analysis.append(" β’ Spinal discs: Dehydration and height loss")
|
| 493 |
+
|
| 494 |
+
health_recommendations.extend([
|
| 495 |
+
"𦴠Weight-bearing exercise essential (bone preservation)",
|
| 496 |
+
"βοΈ Balance training 3x/week (fall prevention)",
|
| 497 |
+
"π Vitamin D3 + K2 supplementation consideration",
|
| 498 |
+
"π©Ί Annual bone density screening recommended"
|
| 499 |
+
])
|
| 500 |
+
|
| 501 |
+
else: # 65+
|
| 502 |
+
feedback.append("𧬠SENIOR PHASE (Preservation Focus):")
|
| 503 |
+
feedback.append(" β’ Bone health: High fracture risk, especially vertebral")
|
| 504 |
+
feedback.append(" β’ Muscle power: 1.5-3% annual decline")
|
| 505 |
+
feedback.append(" β’ Postural control: Significant balance impairment")
|
| 506 |
+
|
| 507 |
+
technical_analysis.append("οΏ½ Age-related structural changes:")
|
| 508 |
+
technical_analysis.append(" β’ Kyphosis progression: Thoracic curvature increase")
|
| 509 |
+
technical_analysis.append(" β’ Disc height: 20-30% reduction from peak")
|
| 510 |
+
technical_analysis.append(" β’ Reaction time: Slowed protective responses")
|
| 511 |
+
|
| 512 |
+
health_recommendations.extend([
|
| 513 |
+
"πΆ Daily walking program (maintain bone loading)",
|
| 514 |
+
"πΊ Chair-based exercises if mobility limited",
|
| 515 |
+
"π Home safety assessment for fall prevention",
|
| 516 |
+
"π₯ Group exercise classes for social engagement"
|
| 517 |
+
])
|
| 518 |
|
| 519 |
+
# Comprehensive visible parts summary with clinical significance
|
| 520 |
if visible_parts:
|
| 521 |
+
feedback.insert(0, f"β
ANATOMICAL VISIBILITY: {', '.join(visible_parts)}")
|
| 522 |
+
feedback.insert(1, f"π DETECTION CONFIDENCE: {len(visible_parts)}/4 major regions")
|
| 523 |
+
feedback.insert(2, "")
|
| 524 |
+
|
| 525 |
+
# Analysis completeness score
|
| 526 |
+
completeness_score = (len(visible_parts) / 4) * 100
|
| 527 |
+
technical_analysis.insert(0, f"π¬ ANALYSIS COMPLETENESS: {completeness_score:.0f}%")
|
| 528 |
+
if completeness_score == 100:
|
| 529 |
+
technical_analysis.insert(1, "β
Full-body biomechanical assessment possible")
|
| 530 |
+
elif completeness_score >= 75:
|
| 531 |
+
technical_analysis.insert(1, "β‘ Comprehensive postural analysis achieved")
|
| 532 |
+
else:
|
| 533 |
+
technical_analysis.insert(1, "β οΈ Limited analysis - optimize camera positioning")
|
| 534 |
+
|
| 535 |
else:
|
| 536 |
+
feedback.append("β POSE NOT DETECTED")
|
| 537 |
+
feedback.append("π POSITIONING REQUIREMENTS:")
|
| 538 |
+
feedback.append(" β’ Distance: 2-3 meters from camera")
|
| 539 |
+
feedback.append(" β’ Lighting: Bright, even illumination")
|
| 540 |
+
feedback.append(" β’ Background: Plain, contrasting surface")
|
| 541 |
+
feedback.append(" β’ Posture: Stand naturally, arms relaxed")
|
| 542 |
+
|
| 543 |
+
technical_analysis.append("π§ DETECTION PARAMETERS:")
|
| 544 |
+
technical_analysis.append(" β’ Model complexity: Level 1 (balanced speed/accuracy)")
|
| 545 |
+
technical_analysis.append(" β’ Detection confidence: 50% minimum")
|
| 546 |
+
technical_analysis.append(" β’ Tracking confidence: 50% minimum")
|
| 547 |
+
technical_analysis.append(" β’ Segmentation: Disabled (faster processing)")
|
| 548 |
+
|
| 549 |
+
health_recommendations.extend([
|
| 550 |
+
"π‘ Improve lighting conditions for better detection",
|
| 551 |
+
"πΌοΈ Use solid background wall behind you",
|
| 552 |
+
"π Position entire body within camera frame",
|
| 553 |
+
"π Wear fitted clothing for clearer body outline"
|
| 554 |
+
])
|
| 555 |
+
|
| 556 |
+
# Compile comprehensive feedback with technical insights
|
| 557 |
+
final_feedback = []
|
| 558 |
+
|
| 559 |
+
# Add main feedback
|
| 560 |
+
final_feedback.extend(feedback)
|
| 561 |
|
| 562 |
+
# Add technical analysis section
|
| 563 |
+
if technical_analysis:
|
| 564 |
+
final_feedback.append("")
|
| 565 |
+
final_feedback.append("οΏ½ βββ TECHNICAL BIOMECHANICAL ANALYSIS βββ")
|
| 566 |
+
final_feedback.extend(technical_analysis)
|
| 567 |
+
|
| 568 |
+
# Add health recommendations section
|
| 569 |
+
if health_recommendations:
|
| 570 |
+
final_feedback.append("")
|
| 571 |
+
final_feedback.append("π₯ βββ EVIDENCE-BASED RECOMMENDATIONS βββ")
|
| 572 |
+
final_feedback.extend(health_recommendations)
|
| 573 |
+
|
| 574 |
+
# Add general wellness footer
|
| 575 |
+
final_feedback.append("")
|
| 576 |
+
final_feedback.append("π SCIENTIFIC BASIS:")
|
| 577 |
+
final_feedback.append("β’ Analysis based on biomechanical research and clinical guidelines")
|
| 578 |
+
final_feedback.append("β’ Recommendations follow evidence-based physiotherapy protocols")
|
| 579 |
+
final_feedback.append("β’ For persistent issues, consult qualified healthcare professionals")
|
| 580 |
+
|
| 581 |
+
return output_image, "\n".join(final_feedback)
|
| 582 |
+
|
| 583 |
+
# Global variables for real-time settings
|
| 584 |
+
current_analysis_type = "basic"
|
| 585 |
+
current_age = None
|
| 586 |
+
current_height = None
|
| 587 |
+
current_weight = None
|
| 588 |
+
|
| 589 |
+
def update_settings(analysis_type, age, height, weight):
|
| 590 |
+
"""Update global settings for real-time analysis"""
|
| 591 |
+
global current_analysis_type, current_age, current_height, current_weight
|
| 592 |
+
current_analysis_type = analysis_type
|
| 593 |
+
current_age = age
|
| 594 |
+
current_height = height
|
| 595 |
+
current_weight = weight
|
| 596 |
+
return f"β
Settings updated: {analysis_type} mode"
|
| 597 |
|
| 598 |
+
def live_analysis(image):
|
| 599 |
+
"""Live analysis function for real-time processing"""
|
| 600 |
+
global current_analysis_type, current_age, current_height, current_weight
|
| 601 |
+
return analyze_posture(image, current_analysis_type, current_age, current_height, current_weight)
|
| 602 |
+
|
| 603 |
+
# Real-time Gradio Interface with enhanced AI feedback
|
| 604 |
+
with gr.Blocks(title="π― Pose-Think: Advanced AI Movement Analysis", css="""
|
| 605 |
+
.gradio-container {
|
| 606 |
+
max-width: 1400px !important;
|
| 607 |
+
}
|
| 608 |
+
.feedback-box {
|
| 609 |
+
font-family: 'Monaco', 'Consolas', monospace;
|
| 610 |
+
font-size: 12px;
|
| 611 |
+
line-height: 1.4;
|
| 612 |
+
}
|
| 613 |
+
""") as demo:
|
| 614 |
|
| 615 |
gr.Markdown("""
|
| 616 |
+
# π― Pose-Think: Advanced AI Movement Analysis Suite
|
| 617 |
+
## β‘ REAL-TIME biomechanical analysis with clinical insights
|
| 618 |
|
| 619 |
+
**Enhanced AI feedback with technical measurements, biomechanical analysis, and evidence-based recommendations!**
|
| 620 |
""")
|
| 621 |
|
| 622 |
with gr.Row():
|
| 623 |
+
with gr.Column(scale=1):
|
| 624 |
+
gr.Markdown("### βοΈ Analysis Configuration")
|
| 625 |
+
|
| 626 |
# Analysis type selection
|
| 627 |
analysis_type = gr.Radio(
|
| 628 |
choices=[
|
|
|
|
| 635 |
)
|
| 636 |
|
| 637 |
# Profile info (for enhanced mode)
|
| 638 |
+
gr.Markdown("### π€ Biometric Profile (Enhanced Mode)")
|
| 639 |
+
age_input = gr.Number(label="Age (years)", minimum=10, maximum=100, value=None)
|
| 640 |
height_input = gr.Number(label="Height (cm)", minimum=100, maximum=250, value=None)
|
| 641 |
weight_input = gr.Number(label="Weight (kg)", minimum=30, maximum=200, value=None)
|
| 642 |
|
| 643 |
+
# Settings update button
|
| 644 |
+
update_btn = gr.Button("βοΈ Update Analysis Settings", variant="primary", size="lg")
|
| 645 |
+
settings_status = gr.Textbox(
|
| 646 |
+
label="Configuration Status",
|
| 647 |
+
value="Ready for advanced real-time analysis",
|
| 648 |
+
interactive=False,
|
| 649 |
+
lines=2
|
| 650 |
+
)
|
| 651 |
+
|
| 652 |
+
# Update settings when button clicked
|
| 653 |
+
update_btn.click(
|
| 654 |
+
fn=update_settings,
|
| 655 |
+
inputs=[analysis_type, age_input, height_input, weight_input],
|
| 656 |
+
outputs=[settings_status]
|
| 657 |
+
)
|
| 658 |
|
| 659 |
+
# Technical info
|
| 660 |
+
gr.Markdown("""
|
| 661 |
+
### π¬ Technical Features:
|
| 662 |
+
- **Biomechanical calculations**
|
| 663 |
+
- **Clinical angle measurements**
|
| 664 |
+
- **Evidence-based recommendations**
|
| 665 |
+
- **Age-specific analysis**
|
| 666 |
+
- **Real-time processing**
|
| 667 |
+
""")
|
| 668 |
|
| 669 |
+
with gr.Column(scale=2):
|
| 670 |
+
# Real-time interface
|
| 671 |
+
gr.Markdown("### πΉ Live Biomechanical Analysis")
|
| 672 |
+
|
| 673 |
+
live_interface = gr.Interface(
|
| 674 |
+
fn=live_analysis,
|
| 675 |
+
inputs=gr.Image(sources=["webcam"], streaming=True),
|
| 676 |
+
outputs=[
|
| 677 |
+
gr.Image(label="π― Live Analysis with Landmarks"),
|
| 678 |
+
gr.Textbox(
|
| 679 |
+
label="π Advanced AI Feedback & Technical Analysis",
|
| 680 |
+
lines=25,
|
| 681 |
+
max_lines=30,
|
| 682 |
+
elem_classes=["feedback-box"]
|
| 683 |
+
)
|
| 684 |
+
],
|
| 685 |
+
live=True,
|
| 686 |
+
allow_flagging="never",
|
| 687 |
+
show_progress=False,
|
| 688 |
+
title=None,
|
| 689 |
+
description="Advanced real-time biomechanical analysis with clinical insights"
|
| 690 |
)
|
| 691 |
+
|
| 692 |
+
# Enhanced instructions and features
|
| 693 |
+
gr.Markdown("""
|
| 694 |
+
## οΏ½ Advanced Analysis Features
|
| 695 |
|
| 696 |
+
### β‘ **Real-Time Capabilities:**
|
| 697 |
+
- **π΄ Live Camera Stream**: Continuous biomechanical analysis
|
| 698 |
+
- **β‘ Instant Technical Feedback**: Clinical measurements in real-time
|
| 699 |
+
- **π§ Dynamic Configuration**: Update settings while analyzing
|
| 700 |
+
- **π Scientific Analysis**: Evidence-based biomechanical insights
|
|
|
|
| 701 |
|
| 702 |
+
### π― **Enhanced Analysis Modes:**
|
| 703 |
+
|
| 704 |
+
#### π― **Basic Posture Analysis:**
|
| 705 |
+
- Shoulder symmetry with angle calculations
|
| 706 |
+
- Elbow joint range of motion assessment
|
| 707 |
+
- Pelvic alignment and hip level analysis
|
| 708 |
+
- Cervical spine positioning evaluation
|
| 709 |
+
- Technical measurements in degrees and millimeters
|
| 710 |
+
|
| 711 |
+
#### π― **Enhanced Posture Analysis:**
|
| 712 |
+
- All basic features PLUS:
|
| 713 |
+
- BMI-based biomechanical load analysis
|
| 714 |
+
- Age-specific physiological insights
|
| 715 |
+
- Clinical risk factor assessment
|
| 716 |
+
- Evidence-based exercise recommendations
|
| 717 |
+
- Personalized intervention strategies
|
| 718 |
|
| 719 |
+
#### π€ **Advanced Hand Analysis:**
|
| 720 |
+
- Individual finger extension angles
|
| 721 |
+
- Grip strength pattern assessment
|
| 722 |
+
- Fine motor control evaluation
|
| 723 |
+
- Neurological function indicators
|
| 724 |
+
- Bilateral symmetry comparison
|
| 725 |
|
| 726 |
+
### π **Technical Measurements:**
|
| 727 |
+
- **Joint Angles**: Precise degree measurements using vector mathematics
|
| 728 |
+
- **Asymmetry Ratios**: Percentage deviations from optimal alignment
|
| 729 |
+
- **Distance Calculations**: Millimeter-level position assessments
|
| 730 |
+
- **Range of Motion**: Clinical interpretation of movement patterns
|
| 731 |
+
- **Biomechanical Loads**: Force distribution analysis
|
| 732 |
|
| 733 |
+
### οΏ½ **Clinical Insights:**
|
| 734 |
+
- **Muscle Imbalance Patterns**: Identification of tight/weak muscle groups
|
| 735 |
+
- **Postural Syndromes**: Recognition of common dysfunction patterns
|
| 736 |
+
- **Risk Assessments**: Injury probability and prevention strategies
|
| 737 |
+
- **Age-Related Changes**: Physiological adaptations and interventions
|
| 738 |
+
- **Evidence-Based Protocols**: Research-backed exercise prescriptions
|
| 739 |
|
| 740 |
+
### π‘ **Optimization Tips:**
|
| 741 |
+
- **πΈ Camera Setup**: 2-3 meters distance, eye-level positioning
|
| 742 |
+
- **π‘ Lighting**: Bright, even illumination from front
|
| 743 |
+
- **πΌοΈ Background**: Plain, contrasting wall surface
|
| 744 |
+
- **π Clothing**: Fitted garments for clear body outline
|
| 745 |
+
- **β‘ Performance**: Close unnecessary applications for smooth processing
|
| 746 |
+
|
| 747 |
+
### π©Ί **Disclaimer:**
|
| 748 |
+
This tool provides educational biomechanical analysis and should not replace professional medical assessment.
|
| 749 |
+
For persistent pain or significant postural deviations, consult qualified healthcare professionals.
|
| 750 |
""")
|
| 751 |
|
| 752 |
if __name__ == "__main__":
|
| 753 |
+
demo.launch(
|
| 754 |
+
)
|