Vision API Server
YOLO-based object detection REST API built with FastAPI. Supports image and video analysis with NVIDIA CUDA, AMD ROCm, and CPU backends.
Features
- Image Detection — object detection with JSON response or annotated image output
- Video Analysis — motion-based frame selection: time grid plus the strongest motion peaks
- Video Annotation — async pipeline: YOLO every Nth frame + hold mode for real-time bbox overlay
- Multi-Backend — NVIDIA GPU (CUDA/NVENC), AMD GPU (ROCm/VAAPI), CPU
- Hardware-Accelerated Encoding — auto-detected FFmpeg HW accel for video decode/encode
- Two-Tier Model Cache — preloaded models (always in memory) + on-demand models with TTL eviction
- Codec Flexibility — H.264, H.265, AV1 output with configurable CRF quality
Quick Start
Local
pip install -r requirements.txt
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload
Requires Python 3.13+ and FFmpeg installed on the system.
Docker
cd docker
# NVIDIA GPU
./docker-up-nvidia.sh
# AMD GPU
./docker-up-amd.sh
# CPU only
./docker-up-cpu.sh
Detached mode: ./docker-up-detach-nvidia.sh (same for amd/cpu).
Stop: ./docker-down-nvidia.sh (same for amd/cpu).
Port mapping: container 8000 → host 3001
GPU Requirements
| Backend |
Requirements |
| NVIDIA |
Driver 530+, nvidia-container-toolkit, CUDA-compatible GPU |
| AMD |
ROCm 5.0+, supported GPU (RX 6000+, MI100+) |
| CPU |
No special requirements |
API Endpoints
| Endpoint |
Method |
Description |
/detect |
POST |
Image detection → JSON |
/detect/visualize |
POST |
Image detection → annotated JPEG |
/detect/video |
POST |
Video detection on motion-selected frames → JSON |
/detect/video/visualize |
POST |
Submit video annotation job (async) |
/extract/frames |
POST |
Extract motion-selected frames as base64 |
/jobs/{job_id} |
GET |
Job status and progress |
/jobs/{job_id}/download |
GET |
Download annotated video |
/models |
GET |
List loaded/cached models |
/health |
GET |
Health check |
Interactive docs available at /docs (Swagger UI) and /redoc.
Usage Examples
# Health check
curl http://localhost:3001/health
# Image detection
curl -X POST "http://localhost:3001/detect?conf=0.6" \
-F "file=@image.jpg"
# Image detection with specific model
curl -X POST "http://localhost:3001/detect?model=yolo26m.pt" \
-F "file=@image.jpg"
# Image with bounding boxes
curl -X POST "http://localhost:3001/detect/visualize" \
-F "file=@image.jpg" -o annotated.jpg
# Video analysis (motion-selected frames)
curl -X POST "http://localhost:3001/detect/video?max_frames=20" \
-F "file=@video.mp4"
# Video annotation (async job)
JOB=$(curl -s -X POST "http://localhost:3001/detect/video/visualize" \
-F "file=@video.mp4" | jq -r '.job_id')
# Poll job status
curl http://localhost:3001/jobs/$JOB
# Download annotated video
curl http://localhost:3001/jobs/$JOB/download -o annotated.mp4
Common Parameters
| Parameter |
Type |
Default |
Range |
Description |
conf |
float |
0.5 |
0.0–1.0 |
Confidence threshold |
imgsz |
int |
640 |
32–2016 |
Inference image size |
max_det |
int |
100 |
1–1000 |
Max detections per image/frame |
model |
string |
— |
— |
Model name (e.g. yolo26s.pt) |
classes |
string |
— |
— |
Comma-separated class filter (person,car) |
detect_every |
int |
5 |
1–300 |
YOLO every N frames (video annotation) |
Upgrading from 2.x
Clients should send max_frames=6 (frigate-analyzer: DETECT_MAX_FRAMES=6) and drop scene_threshold; with the old max_frames=50 a 16-second segment with motion returns up to 16 frames where 2.x returned 2, roughly 8× the inferences on /detect/video until the client is updated. /detect/video now hands YOLO frames in BGR (2.x passed RGB by mistake), so detections on the same recordings differ from 2.x.
Models
YOLO26 models are downloaded automatically on first use:
| Model |
Size |
Trade-off |
yolo26n.pt |
~6 MB |
Fastest, lower accuracy |
yolo26s.pt |
~25 MB |
Good balance |
yolo26m.pt |
~50 MB |
Medium |
yolo26l.pt |
~80 MB |
Higher accuracy |
yolo26x.pt |
~130 MB |
Most accurate, slowest |
Configuration
All settings via environment variables or .env file:
| Variable |
Default |
Description |
YOLO_MODELS |
'{}' |
Preloaded models as JSON, e.g. {"yolo26s.pt":"cuda:0"} |
YOLO_DEVICE |
cpu |
Default device for on-demand models |
YOLO_MODEL_TTL |
900 |
Cached model TTL in seconds (min 60) |
MAX_FILE_SIZE |
10485760 |
Max image upload size in bytes |
MAX_EXECUTOR_WORKERS |
4 |
ThreadPoolExecutor workers |
INFERENCE_TIMEOUT |
30.0 |
Inference timeout in seconds |
VIDEO_EXTRACT_TIMEOUT |
300.0 |
Wall-clock deadline for both ffmpeg passes of one video request, seconds (min 10) |
LOG_LEVEL |
INFO |
Logging level |
VIDEO_JOB_TTL |
3600 |
Completed job TTL in seconds |
VIDEO_JOBS_DIR |
/tmp/vision_jobs |
Job files directory |
MAX_QUEUED_JOBS |
10 |
Max queued annotation jobs |
DEFAULT_DETECT_EVERY |
5 |
YOLO inference every N frames |
VIDEO_CODEC |
auto |
auto / h264 / h265 / av1 |
VIDEO_CRF |
18 |
Quality: 0=lossless, 18=near-lossless, 23=default |
VIDEO_HW_ACCEL |
auto |
auto / nvidia / amd / cpu |
VAAPI_DEVICE |
/dev/dri/renderD128 |
AMD VAAPI render device |
VIDEO_FP16 |
auto |
auto / true / false: FP16 YOLO in video annotation; auto = FP16 on NVIDIA, FP32 on AMD and CPU (FP16 on CPU is ignored) |
VIDEO_BATCH_SIZE |
auto |
auto / 1–64 frames per YOLO call in video annotation; auto = 8 on NVIDIA, 1 on AMD and CPU; halved automatically on GPU out-of-memory |
WATCHDOG_ENABLED |
true |
Process watchdog; false runs uvicorn without it |
WATCHDOG_INTERVAL / WATCHDOG_TIMEOUT |
30 / 10 |
/health probe period and HTTP timeout (seconds) |
WATCHDOG_FAILURES |
3 |
Consecutive failures before the container is restarted |
WATCHDOG_STARTUP_TIMEOUT |
600 |
Seconds to wait for the first healthy answer |
WATCHDOG_MIN_UPTIME / WATCHDOG_FLAP_COOLDOWN |
600 / 900 |
A hang earlier than MIN_UPTIME after start waits FLAP_COOLDOWN before restarting; 0 disables |
WATCHDOG_STOP_GRACE |
8 |
Seconds after SIGTERM before SIGKILL; keep below the compose stop_grace_period (10 s). The wait that confirms the SIGKILL is at least 1 s |
WATCHDOG_MAIL_TO |
(empty) |
E-mail every watchdog restart; needs WATCHDOG_SMTP_HOST (_PORT 587, _USER, _PASSWORD, _STARTTLS true, WATCHDOG_MAIL_FROM) |
Architecture
flowchart TB
subgraph FastAPI["FastAPI Application"]
E1["/detect · /detect/visualize"]
E2["/detect/video · /extract/frames"]
E3["/detect/video/visualize"]
end
E1 --> TPE["ThreadPoolExecutor<br/>(YOLO inference)"]
E2 --> FFE["FFmpeg Two-Pass Extraction<br/>(motion scan → frame fetch)"]
E2 --> TPE
E3 --> JM["JobManager<br/>(async queue)"]
TPE --> MM["ModelManager<br/>(two-tier cache)"]
JM --> VA["VideoAnnotator<br/>(YOLO every Nth frame + hold mode)"]
VA --> MM
VA --> FP["FFmpeg Pipe<br/>(decode → encode)"]
FP --> HW["HW Acceleration<br/>NVIDIA (NVENC) · AMD (VAAPI) · CPU"]
Key Design Decisions
- Async inference — YOLO runs in
ThreadPoolExecutor via run_in_executor() to keep the event loop responsive
- Two-tier model cache — preloaded models (configured at startup, never evicted) + cached models (loaded on demand, TTL-based eviction)
- Video annotation pipeline — async job API with single background worker; YOLO every Nth frame with "hold mode" (reuse last detections for intermediate frames)
- Motion-based frame selection — two FFmpeg passes: a gray 640 px scan measures the largest changed region between neighbouring frames, then the selected frames are read out at full resolution; the selection is frame 0, a grid every
max_gap (or min_interval, whichever is larger) thinned to max_frames - 1, and the strongest motion peaks above motion_threshold, no closer than min_interval and capped at max_frames — so a recording of any length keeps a slot for the strongest motion peak (the slot returns to the grid when no peak qualifies)
- Process watchdog —
supervisor.py runs uvicorn as a child and polls /health from outside the Python process; a GPU hang that freezes the interpreter (GIL held) ends in a SIGKILL and a container restart instead of an indefinite outage
Limits
| Resource |
Limit |
| Image upload |
10 MB default, 100 MB max configurable |
| Video upload |
500 MB |
| Image formats |
jpg, jpeg, png, webp, bmp |
| Video formats |
mp4, avi, mov, mkv, webm, wmv, flv |
Project Structure
app/
├── main.py # FastAPI app, endpoints, lifespan
├── config.py # Pydantic settings from env vars
├── models.py # Request/response Pydantic models
├── model_manager.py # YOLO model lifecycle, two-tier cache
├── job_manager.py # Video annotation job queue, TTL cleanup
├── video_annotator.py # YOLO + hold mode video annotation
├── batch_inference.py # Video pass 1: LetterBox-exact pre-resize, batched YOLO
├── frame_threads.py # Reader/writer threads for the ffmpeg pipes
├── ffmpeg_pipe.py # FFmpeg subprocess pipe decoder/encoder
├── hw_accel.py # Hardware acceleration detection
├── video_utils.py # Two-pass frame extraction: motion scan + frame fetch
├── frame_selection.py # Motion metric and frame selection rule
├── inference_utils.py # Async inference via ThreadPoolExecutor
├── image_utils.py # Image validation and decoding
├── visualization.py # Bounding box rendering
├── dependencies.py # FastAPI dependency injection
└── supervisor.py # Process watchdog: container command, restarts on hung /health
docker/
├── nvidia/ # NVIDIA GPU Dockerfile + compose
├── amd/ # AMD GPU Dockerfile + compose
├── cpu/ # CPU-only Dockerfile + compose
└── *.sh # Up/down/detach scripts
tests/ # pytest test suite
Testing
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt -r requirements-dev.txt
python -m pytest tests/ -v
License
Copyright (C) 2026 Alexander Zinin mail@zinin.ru
Licensed under the GNU Affero General Public License v3.0 or later
(AGPL-3.0-or-later). See LICENSE.