Spaces:
Build error
Build error
Upload app.py
Browse files112345 unmodified lines1011121313141515161717181920212223242325262728293027313233343536373839404142434445464748495051523031325354555657585960616263646566676869707172737475767735787980818283848539868788894 unmodified lines949596509798991001011025510310410510658591071081091101111121136211411511611711811912012112212312412512612712812913065131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173676817417517617717817918018118218318416 unmodified lines20120220391204205206207208209210982112122132142152162172182192202212222232242252262272281002292301022312322332342352361042372382392402412422432442452462472481062492502512521082532542552562572582591152602612622632642652663 unmodified lines2702712721272732742752762772782792802812822832842852862872882892901342912921362932942952962972982991433003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313321451463333343353363371483383393403413423433443453463473483493503513523533543553563573583593603613621563633641583653663673683693703711603723733743753763773783793803813823833843851623863873883891643903913923933943953961683973983994004014021704034044054064074084091724104114124134141744154164174184194204214224234244254264274284294304314324334344354361764374384394404411784424434441814454464471834484494504514524534544554564574584594604614624634644651851861874664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175181895195201911925215225235245255265275285295305315325335345351941955365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795801975815825835845855865875885895905915925935945955965971992005985996006016026036046056066076086096106116126136146152032046166176182066196206216222106236246256266276286 unmodified lines635636637223224638639640641642228229643644645646647648649650651652653654655656657658231232659660661662663664665666667668234235236237238239240669670671672673674675676677678679680681682683684685686687688689690691692693694695243244245246247248696697698699700701250251252702703704705706707708709710711712713714715716717718254255256257719720721722723724725259260261262263264726727728729730731732266267268269270271733734735736737738739273274275276277740741742743744745746747748749750751752281753754# Pose-Think: AI-Powered Movement Analysis Suite - Gradio 4.8.0 Compatible# Pose-Think: AI-Powered Movement Analysis Suite - REAL-TIME VERSIONimport cv2import mediapipe as mpimport gradio as gr5 unmodified linesmp_drawing = mp.solutions.drawing_utils
def analyze_posture(image, analysis_type="basic", age=None, height=None, weight=None): """Main analysis function compatible with Gradio 4.8.0""" """Advanced AI-powered posture analysis with biomechanical insights""" if image is None: return None, "β No image / GΓΆrΓΌntΓΌ yok" return None, "β No image detected / GΓΆrΓΌntΓΌ tespit edilemedi" # Convert BGR to RGB # 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 = [] # Profile info for enhanced mode # 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}") 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}") if bmi > 25: feedback.append("β οΈ BMI high - extra load on posture") 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"π€ Profile: {' | '.join(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": # Hand analysis # Advanced Hand Analysis with Biomechanical Insights with mp_hands.Hands( static_image_mode=False, max_num_hands=2,4 unmodified lines if results.multi_hand_landmarks: hand_count = len(results.multi_hand_landmarks) feedback.append(f"β
{hand_count} hands detected") 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) # Simple finger counting # Advanced finger analysis landmarks = hand_landmarks.landmark fingers_up = 0 finger_angles = [] tip_ids = [4, 8, 12, 16, 20] pip_ids = [3, 6, 10, 14, 18] 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): if landmarks[tip_ids[i]].y < landmarks[pip_ids[i]].y: 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'}") feedback.append(f"ποΈ Hand {idx+1}: {fingers_up} fingers up") # 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("ποΈ Show your hands to the camera") 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 analysis16 unmodified lines if landmarks[mp_pose.PoseLandmark.NOSE.value].visibility > 0.5: visible_parts.append("Head") # Shoulders # 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") # Shoulder level check # 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: if shoulder_diff > 0.05: # >5% asymmetry if left_shoulder.y < right_shoulder.y: feedback.append("β οΈ Left shoulder higher") 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("β οΈ Right shoulder higher") 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") 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)") # Elbows and angles # 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") # Calculate elbow angles # 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)3 unmodified lines angle = 360 - angle return angle # Left elbow 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(f"π Left elbow: {left_angle:.1f}Β°") feedback.append(interpret_elbow_angle(left_angle, "LEFT")) # Right elbow angle # 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(f"π Right elbow: {right_angle:.1f}Β°") 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: feedback.append("β οΈ Cannot calculate elbow angles") 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") # Hips # 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: if hip_diff > 0.03: # >3% asymmetry if left_hip.y < right_hip.y: feedback.append("β οΈ Left hip higher") 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("β οΈ Right hip higher") 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") 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)") # Neck position # 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 = abs(nose.x - shoulder_center_x) 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 if head_offset > 0.08: 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("π Neck tilted left") 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("π Neck tilted right") 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") 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Β°)") # Age-specific recommendations for enhanced mode # Enhanced Age-Specific Biomechanical Recommendations if analysis_type == "enhanced" and age: feedback.append("") feedback.append("π― Age-Specific Recommendations:") feedback.append("π― AGE-SPECIFIC BIOMECHANICAL ANALYSIS:") if age < 25: feedback.append("π‘ Young age: Form good posture habits now") 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("π‘ Middle age: Regular exercise important") else: feedback.append("π‘ Mature age: Focus on bone health") 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" ]) # List visible parts # Comprehensive visible parts summary with clinical significance if visible_parts: feedback.insert(0, f"β
Visible: {', '.join(visible_parts)}") feedback.insert(1, "") 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("β Body not detected") feedback.append("π Stand so full body is visible to camera") 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(feedback) return output_image, "\n".join(final_feedback)
# Global variables for real-time settingscurrent_analysis_type = "basic"current_age = Nonecurrent_height = Nonecurrent_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"
# Simple Gradio Interface compatible with 4.8.0with gr.Blocks(title="π― Pose-Think: AI Movement Analysis") as demo: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 feedbackwith 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: AI-Powered Movement Analysis Suite ## Real-time posture and movement analysis with multiple modes # π― Pose-Think: Advanced AI Movement Analysis Suite ## β‘ REAL-TIME biomechanical analysis with clinical insights **Choose your analysis type and get instant feedback on what the camera sees!** **Enhanced AI feedback with technical measurements, biomechanical analysis, and evidence-based recommendations!** """) with gr.Row(): with gr.Column(): with gr.Column(scale=1): gr.Markdown("### βοΈ Analysis Configuration") # Analysis type selection analysis_type = gr.Radio( choices=[6 unmodified lines ) # Profile info (for enhanced mode) gr.Markdown("### π€ Optional Profile (for Enhanced mode)") age_input = gr.Number(label="Age", minimum=10, maximum=100, value=None) 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) # Camera input input_image = gr.Image(sources=["webcam"], label="πΉ Camera") # 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] ) # Analysis button analyze_btn = gr.Button("π Analyze", variant="primary", size="lg") # Technical info gr.Markdown(""" ### π¬ Technical Features: - **Biomechanical calculations** - **Clinical angle measurements** - **Evidence-based recommendations** - **Age-specific analysis** - **Real-time processing** """) with gr.Column(): # Outputs output_image = gr.Image(label="π― Analysis Result") feedback_text = gr.Textbox( label="π Detailed Feedback", lines=15, interactive=False 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 # Analysis function analyze_btn.click( fn=analyze_posture, inputs=[input_image, analysis_type, age_input, height_input, weight_input], outputs=[output_image, feedback_text] ) ### β‘ **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 # Usage instructions gr.Markdown(""" ## π How to Use ### π― **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 ### π― **Analysis Types:** - **Basic Posture**: Body parts, joint angles, alignment - **Enhanced Posture**: Basic + age/BMI insights + personalized recommendations - **Hand Tracking**: Hand detection and finger counting #### π€ **Advanced Hand Analysis:** - Individual finger extension angles - Grip strength pattern assessment - Fine motor control evaluation - Neurological function indicators - Bilateral symmetry comparison ### π **Instructions:** 1. **Choose analysis type** from the radio buttons 2. **Allow camera access** when prompted by your browser 3. **Position yourself** 2-3 meters from camera (full body visible for posture) 4. **For Enhanced mode**: Optionally enter age/height/weight for personalized insights 5. **Click Analyze** to get instant detailed feedback ### π **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 ### π― **What you'll see:** - β
**Green checkmarks**: Good alignment/posture - β οΈ **Warning signs**: Issues detected that need attention - π **Measurements**: Joint angles in degrees - π **Position info**: Head, neck, shoulder positions - π€ **Profile insights**: Age-specific recommendations (Enhanced mode) ### οΏ½ **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 ### π‘ **Tips for best results:** - **Good lighting**: Ensure even, bright lighting - **Plain background**: Use contrasting, simple background - **Stable position**: Minimize movement during analysis - **Full visibility**: Keep target body parts clearly visible ### π‘ **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() demo.launch( )app.py+579-106