File size: 5,036 Bytes
bb3469b a4f90fa 98a97f4 a4f90fa 98a97f4 a4f90fa 98a97f4 a4f90fa 98a97f4 a4f90fa 98a97f4 a4f90fa 98a97f4 a4f90fa 98a97f4 491f8d0 98a97f4 a4f90fa 98a97f4 a4f90fa 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 6f5feff 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 491f8d0 98a97f4 | 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 | ---
title: GAIT_API
emoji: 🚶
colorFrom: blue
colorTo: indigo
sdk: docker
app_file: app.py
pinned: false
---
# Gait Analysis API
Clinical gait analysis service built with FastAPI, OpenCV, and MediaPipe Pose.
It processes a front-view walking video and returns:
- extracted gait biomarkers
- rule-based clinical interpretation
- overall gait stability score
- annotated skeleton video
- clinical dashboard plot
The notebook prototype is kept in [gait.ipynb](<gait.ipynb>), and the production API implementation is in [app.py](app.py).
## Table of contents
- Overview
- Project structure
- How it works
- API reference
- Local development
- Docker usage
- Storage cleanup strategy
- Hugging Face Spaces deployment
- GitHub Actions auto-deploy
- Troubleshooting
## Overview
This API is designed for single-video gait assessment.
Core stack:
- FastAPI for REST endpoints
- MediaPipe Pose for landmark extraction
- OpenCV for video I/O and skeleton overlay
- NumPy/SciPy for signal processing and feature extraction
- Matplotlib for biomarker visualizations
Dependencies are listed in [requirements.txt](requirements.txt).
## Project structure
- [app.py](app.py): Main API + gait analysis pipeline
- [requirements.txt](requirements.txt): Python dependencies
- [Dockerfile](Dockerfile): Container build (HF Spaces compatible)
- [scripts/start.sh](scripts/start.sh): Container startup + background cleanup loop
- [scripts/cleanup_runs.py](scripts/cleanup_runs.py): Deletes old generated files
- [.github/workflows/deploy-hf-space.yml](.github/workflows/deploy-hf-space.yml): Auto-sync GitHub repo to HF Space
- [gait.ipynb](<gait.ipynb>): Original notebook source logic
## How it works
High-level flow:
1. Upload `video` + `gender`
2. Extract pose landmarks for each frame
3. Validate video (person detected, front-view check)
4. Build temporal signals (ankles, feet, arm swing, hip center)
5. Smooth + detrend + detect peaks
6. Compute biomarkers (`stride_variability`, `cadence`, `symmetry_ratio`, arm metrics)
7. Create clinical interpretation text
8. Compute weighted gait stability score
9. Generate dashboard image + annotated video
10. Return JSON payload
Main endpoints are declared in [app.py](app.py#L560-L785).
## API reference
### `GET /`
Basic API metadata and endpoint hints.
### `POST /analyze`
Accepts multipart form-data:
- `video`: gait video (`mp4/mov/avi/...`)
- `gender`: `male` or `female`
Returns analysis JSON with base64-embedded files (`annotated_video`, `clinical_dashboard`).
Use this when you want everything in one response.
### `POST /analyze_files`
Accepts multipart form-data:
- `video`: gait video
- `gender`: `male` or `female`
Returns analysis JSON with downloadable URLs:
- `/download/{session_id}_annotated.mp4`
- `/download/{session_id}_dashboard.png`
This is generally the better choice for deployment because responses stay smaller than full base64 payloads.
### `GET /download/{filename}`
Downloads generated output files from `runs/outputs`.
### `GET /health`
Simple health check.
## Local development
1. Create environment and install dependencies
```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```
2. Run API
```bash
uvicorn app:app --reload --host 0.0.0.0 --port 8000
```
3. Open docs
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
## Docker usage
Build:
```bash
docker build -t gait-api:latest .
```
Run:
```bash
docker run --rm -p 7860:7860 gait-api:latest
```
Container defaults:
- serves on port `7860`
- startup script: [scripts/start.sh](scripts/start.sh)
- output directory: `/app/runs/outputs`
## Storage cleanup strategy
Generated files from `/analyze_files` are stored under `runs/outputs`.
Cleanup is handled by [scripts/cleanup_runs.py](scripts/cleanup_runs.py):
- default retention: 30 minutes
- deletes old files under `runs/`
- preserves required directory structure
In Docker/HF Spaces, [scripts/start.sh](scripts/start.sh) starts a background cleanup loop automatically.
Configurable environment variables:
- `CLEANUP_INTERVAL_SECONDS` (default: `1800`)
- `RUNS_MAX_AGE_MINUTES` (default: `30`)
Optional manual run:
```bash
python scripts/cleanup_runs.py --path ./runs --max-age-minutes 30 --dry-run
```
## Hugging Face Spaces deployment (Docker)
This repository is configured for Docker Spaces.
Key points:
- README front matter is required and already included
- container uses [Dockerfile](Dockerfile)
- app starts via [scripts/start.sh](scripts/start.sh)
- `PORT` env is respected (default `7860`)
Recommended endpoint on Spaces:
- Use `/analyze_files` for better response size and reliability
## GitHub Actions auto-deploy to HF Space
Workflow: [.github/workflows/deploy-hf-space.yml](.github/workflows/deploy-hf-space.yml)
Behavior:
- triggers on push to `main`
- sanitizes `HF_TOKEN`
- force-pushes repository to `xplorers/GAIT_API`
Required GitHub secret:
- `HF_TOKEN`: Hugging Face token with write access to the target Space
|