A Flask-based REST API for analyzing dance poses in videos using MediaPipe pose estimation.
# Activate your virtual environment
source dance/bin/activate
# Install dependencies
pip install -r requirements_flask.txt# Simple start
python flask_api.py
# Or use the startup script with options
python start_api.py --host 0.0.0.0 --port 5000 --debug- Web Interface: http://localhost:5000
- API Documentation: http://localhost:5000 (with
Accept: application/jsonheader) - Health Check: http://localhost:5000/health
- Web Interface: Returns HTML upload form
- API Docs: Returns JSON documentation (with
Accept: application/jsonheader)
Upload a video file for pose analysis.
Request:
- Content-Type:
multipart/form-data - Field:
video(video file)
Response:
{
"success": true,
"job_id": "uuid-string",
"message": "Video processed successfully",
"result": {
"video_info": {
"duration_seconds": 15.5,
"total_frames": 465,
"frames_with_poses": 420
},
"pose_summary": {
"standing": {"count": 180, "percentage": 42.86},
"arms_up": {"count": 95, "percentage": 22.62}
},
"timeline": [
{"timestamp": 0.03, "poses": ["standing"]},
{"timestamp": 0.07, "poses": ["standing", "arms_up"]}
]
},
"download_url": "/result/uuid-string"
}Get analysis results by job ID.
Query Parameters:
download=true: Download as JSON file
Response:
{
"success": true,
"job_id": "uuid-string",
"result": { /* analysis results */ }
}List all available analysis results.
Response:
{
"success": true,
"total_results": 5,
"results": [
{
"job_id": "uuid-string",
"created_at": 1640995200,
"size_bytes": 2048,
"download_url": "/result/uuid-string?download=true"
}
]
}Health check endpoint.
Response:
{
"status": "healthy",
"service": "dance-pose-detector",
"version": "1.0.0"
}The API can detect the following dance poses:
- Standing - Basic upright standing position
- Arms Up - Both arms raised above shoulders
- Arms Crossed - Arms crossed in front of body
- One Arm Up - Single arm raised
- Squat - Knees bent, lowered position
- Lunge - One leg forward, one back
- Arabesque - One leg raised behind
The analysis results include:
duration_seconds: Video length in secondstotal_frames: Total number of frames processedframes_with_poses: Number of frames where poses were detected
For each detected pose:
count: Number of times the pose was detectedpercentage: Percentage of total detections
Chronological list of pose detections:
timestamp: Time in seconds when pose was detectedposes: Array of pose names detected at that timestamp
- Max file size: 100MB
- Supported formats: MP4, AVI, MOV, MKV, WMV, FLV, WebM
uploads/: Temporary storage for uploaded videos (auto-cleaned)results/: Persistent storage for analysis resultstemplates/: HTML templates for web interface
# Start the API server first
python flask_api.py
# In another terminal, run tests
python test_api.pyUpload a video:
curl -X POST -F "video=@your_video.mp4" http://localhost:5000/uploadGet results:
curl http://localhost:5000/result/your-job-idDownload results:
curl "http://localhost:5000/result/your-job-id?download=true" -o results.jsonThe HTML interface provides:
- Drag & Drop Upload: Drag video files directly onto the upload area
- File Validation: Checks file format and size before upload
- Progress Indication: Visual feedback during processing
- Results Visualization:
- Video information summary
- Pose detection statistics with percentages
- Interactive timeline viewer
- Download options for full results
The API provides detailed error responses:
400: Bad request (invalid file, missing parameters)404: Resource not found (invalid job ID)413: File too large (exceeds 100MB limit)500: Internal server error (processing failure)
{
"error": "Error Type",
"message": "Detailed error description",
"job_id": "uuid-string" // if applicable
}- File uploads are validated for type and size
- Uploaded files are automatically cleaned up after processing
- Secure filename handling prevents directory traversal
- Results are stored with UUID-based filenames
- Processing time depends on video length and complexity
- Typical processing: ~1-2x video duration
- Memory usage scales with video resolution
- Results are cached for future retrieval
├── flask_api.py # Main Flask application
├── dance_pose_detector.py # Core pose detection logic
├── start_api.py # Startup script with options
├── test_api.py # API test suite
├── requirements_flask.txt # Python dependencies
├── templates/
│ └── upload.html # Web interface
├── uploads/ # Temporary upload storage
└── results/ # Analysis results storage
To add new pose detection:
- Add detection method to
DancePoseDetectorclass - Register in
dance_posesdictionary - Update API documentation
- Add tests for new pose
Edit templates/upload.html to modify:
- Styling and layout
- Upload behavior
- Results visualization
- Additional features
For issues or questions:
- Check the health endpoint:
/health - Review error messages in API responses
- Check server logs for detailed error information
- Verify file format and size requirements