File size: 4,218 Bytes
83ee618
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from gait_analysis import EnhancedGaitAnalyzer
import os
from datetime import datetime

def generate_markdown_report(results, video_path, output_path):
    """

    Generate a strictly structured gait analysis report.

    """
    summary = results.get("summary", {})
    observations = results.get("observations", {})
    strategies = results.get("compensatory_strategies", [])
    reflex_influence = results.get("reflex_influence", "None observed")
    safety_note = results.get("safety_note", "")
    weight_dist = results.get("weight_distribution", {})
    postural_strength = results.get("postural_strength", {})
    clinical_notes = results.get("clinical_notes", [])
    
    # 1. Walking Condition
    walking_condition = summary.get("walking_condition", "Not specified")
    
    # 2. Observed Movement Patterns
    patterns = f"""### Observed Movement Patterns



- **Trunk:** {observations.get('trunk', 'N/A')}

- **Head & Neck:** {observations.get('head_neck', 'N/A')}

- **Arms & Hands:** {observations.get('arms_hands', 'N/A')}

- **Lower Limbs:** {observations.get('lower_limbs', 'N/A')}

- **Symmetry:** {observations.get('symmetry', 'N/A')}

- **Weight Distribution:** {observations.get('weight_distribution', 'N/A')}

- **Postural Control:** {observations.get('postural_control', 'N/A')}"""

    # 3. Compensatory Strategies
    comp_strategies = "None identified"
    if strategies:
        comp_strategies = "\n".join([f"- {s}" for s in strategies])
        
    # 4. Clinical Notes
    clinical_notes_text = "None noted"
    if clinical_notes:
        clinical_notes_text = "\n".join([f"- {n}" for n in clinical_notes])
    
    # 5. Integrated Movement Summary
    # Combine stats into a summary
    asymmetry = summary.get("phase_asymmetry_percent", 0)
    stability = summary.get("stability_score", 0)
    strength = summary.get("strength_score", 0)
    weight_shift = summary.get("weight_shift_score", 0)
    gait_quality = summary.get("overall_gait_quality", "unknown")
    
    integrated_summary = f"""### Integrated Movement Summary



The subject demonstrates a **{walking_condition.lower()}** walking pattern with a stability score of **{stability:.0f}/100**, strength score of **{strength:.0f}/100**, and weight shift score of **{weight_shift:.0f}/100**.

Overall gait quality appears **{gait_quality}**.

Movement symmetry shows a **{asymmetry:.1f}%** phase variance.



{reflex_influence}



{safety_note}"""

    # Final Report Assembly
    report = f"""# Gait Analysis Report



## Walking Condition

**{walking_condition}**



{patterns}



### Compensatory Strategies

{comp_strategies}



### Clinical Notes

{clinical_notes_text}



{integrated_summary}



---

*Generated by Gait Analysis System v3.0*

"""

    # Write to file
    with open(output_path, 'w', encoding='utf-8') as f:
        f.write(report)
    
    print(f"✅ Strict Report generated successfully: {output_path}")
    return output_path


def main():
    # Path to test.mp4
    video_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "test.mp4"))
    
    print(f"Looking for video at: {video_path}")
    if not os.path.exists(video_path):
        print("Error: test.mp4 not found!")
        return

    print("Initializing EnhancedGaitAnalyzer...")
    analyzer = EnhancedGaitAnalyzer(age=4)
    
    print("Processing video... this might take a moment.")
    try:
        results = analyzer.process_video(video_path)
        
        # Generate report filename with timestamp
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        report_filename = f"gait_analysis_report_{timestamp}.md"
        report_path = os.path.join(os.path.dirname(video_path), report_filename)
        
        # Generate markdown report
        generate_markdown_report(results, video_path, report_path)
        
        print(f"\n{'='*60}")
        print(f"Report saved to: {report_path}")
        print(f"{'='*60}\n")
        
    except Exception as e:
        print(f"An error occurred during processing: {e}")
        import traceback
        traceback.print_exc()


if __name__ == "__main__":
    main()