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