Wildlife Detection API
February 12, 2026 · View on GitHub
A REST API that identifies animals in images using Microsoft MegaDetector v5a, built with Flask and PyTorch.
Setup
1. Create a virtual environment
python -m venv venv
source venv/bin/activate # macOS/Linux
2. Install dependencies
pip install -r requirements.txt
3. Download model weights
python app/download_model.py
This downloads the MegaDetector v5a model (~268MB) to weights/md_v5a.0.0.pt.
Usage
Start the server
python -m app.app
The API runs on http://localhost:5000 by default.
Detect animals in an image
curl -X POST -F "image=@path/to/photo.jpg" http://localhost:5000/predict
Response:
{
"animal": "animal",
"confidence": 0.95
}
API Reference
| Endpoint | Method | Description |
|---|---|---|
/predict | POST | Upload an image (multipart form, key: image) to detect animals |
Error responses:
400— No image provided or empty filename500— Server/model error
Running Tests
python -m pytest tests/
Or with unittest:
python -m unittest discover tests/
Project Structure
wildlifeApp/
app/
app.py # Flask API entry point
model_handler.py # MegaDetector inference logic
download_model.py # Model weight downloader
tests/
test_images/ # Test fixtures
test_model_handler.py
weights/ # Model weights (not committed)
requirements.txt
Tech Stack
- Framework: Flask
- ML Model: Microsoft MegaDetector v5a (YOLOv5)
- Runtime: PyTorch + torchvision
- Image Processing: Pillow