gait-analysis-backend / generate_report.py
techSnipe's picture
Upload folder using huggingface_hub
83ee618 verified
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()