A FastAPI-based REST API for real-time hand gesture recognition using pre-trained XGBoost models and MediaPipe hand landmarks.
- π Fast predictions using XGBoost
- π Confidence scores and probability distributions
- π Preprocessing pipeline matching training methodology
- π³ Docker support for easy deployment
- π Comprehensive API documentation with Swagger UI
- β Health checks and monitoring endpoints
- π§ͺ Test suite included
git clone <your-repo-url>
cd hand-gesture-api
Copy your trained model files to the models/
directory:
*.pkl
or*.json
- XGBoost model file*label_encoder*.pkl
- Label encoder file
# Build and run
docker-compose up --build
# Or run just the API
docker build -t hand-gesture-api .
docker run -p 8000:8000 hand-gesture-api
# Install dependencies
pip install -r requirements.txt
# Run the API
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
- Local:
http://0.0.0.0:8000
- Docker:
http://0.0.0.0:8000
GET /health
POST /predict
Content-Type: application/json
{
"landmarks": [
{"x": 0.5, "y": 0.5, "z": 0.0},
{"x": 0.52, "y": 0.48, "z": 0.01},
// ... 19 more landmarks (21 total)
]
}
Response:
{
"gesture": "peace",
"confidence": 0.95,
"probabilities": {
"peace": 0.95,
"ok": 0.03,
"stop": 0.02
},
"processing_time_ms": 12.5
}
GET /gestures
GET /model/info
Visit http://0.0.0.0:8000/docs
for Swagger UI documentation.
The API expects exactly 21 hand landmarks in MediaPipe format:
- x, y: Normalized coordinates in [0, 1]
- z: Depth coordinate (can be negative)
Landmarks should be ordered according to MediaPipe hand landmark model: 0. WRIST
- THUMB_CMC
- THUMB_MCP ... (see MediaPipe documentation)
pytest tests/ -v
app/
βββ main.py # FastAPI application
βββ models/
β βββ predictor.py # Model loading and prediction
βββ schemas/
β βββ request_response.py # Pydantic models
βββ utils/
βββ preprocessing.py # Landmark preprocessing
- New endpoints: Add to
app/main.py
- New schemas: Add to
app/schemas/
- New preprocessing: Add to
app/utils/
- Tests: Add to
tests/
- Environment Variables:
export MODEL_PATH=/path/to/model
export ENCODER_PATH=/path/to/encoder
- Docker Production:
docker-compose --profile production up
- Batch predictions: Modify API to accept multiple landmark sets
- Model optimization: Use ONNX or TensorRT for faster inference
- Caching: Add Redis for caching frequent predictions
- Load balancing: Use multiple API instances behind nginx
- Health endpoint:
/health
- Metrics: Processing time included in responses
- Logs: Structured logging with timestamps
- Docker health checks: Built-in container health monitoring
- Average Prediction Time: A separate metric for prediction time because response time is not a valid indicator for performance in this case
- Confidence and Landmarks Distribution: To check for data drift and model degredation
- CPU and Memory Usage: For server performance statistics
-
Model not loading:
- Check model files are in
models/
directory - Verify file permissions
- Check logs for specific error messages
- Check model files are in
-
Invalid landmarks:
- Ensure exactly 21 landmarks
- Verify coordinate ranges (x,y in [0,1])
- Check landmark ordering matches MediaPipe
-
Docker issues:
- Ensure port 8000 is available
- Check Docker daemon is running
- Verify model files are mounted correctly
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload --log-level debug
[Your License Here]
## 13. Setup Commands
Now you can set up everything:
```bash
mkdir hand-gesture-api
cd hand-gesture-api
mkdir -p app/models app/schemas app/utils tests models
touch app/__init__.py app/models/__init__.py app/schemas/__init__.py app/utils/__init__.py tests/__init__.py
Then copy your trained model files to the models/
directory and run:
docker-compose up --build# Hand-Gesture-Controlled-Puzzle-Game-Backend