cctv-object-detection / Dockerfile
openscopelabs's picture
feat(scan): right-click scan-mode alerting + per-camera pose
d6b1333
Raw History Blame Contribute Delete
5.14 kB
# syntax=docker/dockerfile:1.6
#
# Cakra CCTV YOLO Analytics — single-stage image suited for both
# Hugging Face Spaces (sdk: docker, app_port: 7860) and a generic
# VPS. ffmpeg is the only external runtime dependency beyond the
# Python packages in requirements.txt.
FROM python:3.12-slim
# --- system deps -----------------------------------------------------
# - ffmpeg : HLS demux on the input + libx264 encode on the output
# - libgl1 : opencv-python-headless still pulls libGL.so for some
# ops on Debian slim, even though we use the headless
# build
# - libglib2.0 : opencv + supervision transitive dep
# - curl : healthcheck convenience
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ffmpeg \
libgl1 \
libglib2.0-0 \
curl \
ca-certificates \
git \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# --- python deps -----------------------------------------------------
# Copy requirements first to maximise layer caching on code-only edits.
COPY requirements.txt ./
# Install CPU-only torch/torchvision first — the default PyPI wheels
# bundle CUDA (~700 MB extra) which a Hugging Face CPU space can't use
# anyway. Subsequent `pip install -r requirements.txt` sees torch>=2.2
# is already satisfied and skips the redownload.
RUN pip install --no-cache-dir --upgrade pip \
&& pip install --no-cache-dir \
--index-url https://download.pytorch.org/whl/cpu \
torch torchvision \
&& pip install --no-cache-dir -r requirements.txt
# --- app source ------------------------------------------------------
COPY app ./app
COPY scripts ./scripts
COPY docs ./docs
COPY run.sh ./
# Profile overlays — loaded by app/config.py when CAKRA_PROFILE matches
# the filename suffix. Without these the deploy would silently fall back
# to the bare defaults even if `CAKRA_PROFILE=vps` is set in the env.
COPY .env.hfspace .env.vps ./
# Ensure run.sh is executable (git mode bits are sometimes lost during
# Hugging Face Spaces sync).
RUN chmod +x run.sh
# Bake the detection model so the first /connect doesn't have to wait
# on a 70-450 MB download.
#
# Two backbone families are supported, picked via YOLO_BUILD_FAMILY:
#
# yolov8 — standard YOLOv8 with 80 COCO classes. ~8x faster on CPU
# than YOLO-World M at similar mAP for the COCO subset we
# care about (car / truck / bus / motorcycle / bicycle /
# person). Recommended for production. DEFAULT.
#
# yoloworld — YOLO-World v2 with 17 Indonesian-traffic classes baked
# in via CLIP text embeddings (becak, bemo, bajaj, angkot,
# ojek included). Zero-shot capability but 5-10x heavier on
# CPU — only worthwhile on a GPU host.
#
# YOLO_BUILD_SIZE picks the variant size inside the family
# (n/s/m/l/x for yolov8 — yolo-world has no n).
# `--build-arg YOLO_BUILD_SIZE=skip` skips the bake entirely if you
# prefer to mount a custom model at runtime.
ARG YOLO_BUILD_FAMILY=yolov8
ARG YOLO_BUILD_SIZE=m
RUN if [ "$YOLO_BUILD_SIZE" != "skip" ]; then \
case "$YOLO_BUILD_FAMILY" in \
yolov8) \
PYTHONPATH=. python -m scripts.setup_yolov8 "$YOLO_BUILD_SIZE" ;; \
yoloworld) \
# CLIP is only resolved when the build actually needs it,
# keeping the default yolov8 image leaner by ~50 MB +
# avoiding a CDN hop at build time on non-WorldYOLO paths.
pip install --no-cache-dir "clip @ git+https://github.com/ultralytics/CLIP.git" \
&& PYTHONPATH=. python -m scripts.setup_yolo_world "$YOLO_BUILD_SIZE" ;; \
*) echo "Unknown YOLO_BUILD_FAMILY=$YOLO_BUILD_FAMILY (expected yolov8|yoloworld)"; exit 1 ;; \
esac; \
fi
# Also export the ONNX variant during build so HF Spaces / CPU VPS
# deploys can switch backend instantly via MODEL_PATH without paying
# the ~20s runtime export cost (or carrying onnxscript memory pressure
# at first request). YOLO_EXPORT_ONNX=skip disables this step.
ARG YOLO_EXPORT_ONNX=auto
ARG YOLO_EXPORT_IMGSZ=640
RUN if [ "$YOLO_BUILD_SIZE" != "skip" ] && [ "$YOLO_EXPORT_ONNX" != "skip" ]; then \
PYTHONPATH=. python -m scripts.export_onnx --imgsz "$YOLO_EXPORT_IMGSZ"; \
fi
# HLS segments are written here at runtime; ephemeral disk is fine.
RUN mkdir -p /tmp/cakra-hls
# Alerts artifact directory. VPS deploy mounts /opt/cakra/yolo/alerts
# over this path so JPEG snapshots + MP4 clips survive container
# recreate; HF Spaces / local just use the in-container tmpfs.
RUN mkdir -p /opt/cakra/yolo/alerts
# --- runtime ---------------------------------------------------------
# Hugging Face Spaces exposes ${PORT} (default 7860). On a generic VPS
# you can override the same variable.
ENV PORT=7860 \
HOST=0.0.0.0 \
HLS_OUTPUT_DIR=/tmp/cakra-hls \
HLS_ENCODER=libx264 \
PYTHONUNBUFFERED=1
EXPOSE 7860
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD curl -fs http://localhost:${PORT}/health || exit 1
CMD ["./run.sh"]