Spaces:
Running
feat(detect): use MMDetection RTMDet-s as the primary detector
Browse filesDetection now runs through MMDetection 3.3: COCO-pretrained RTMDet-s from
the OpenMMLab model zoo, loaded with mmdet.apis.init_detector and run with
inference_detector. Ultralytics YOLOv8 stays as an automatic fallback, and
DETECTOR=yolo forces it.
- /pipeline and /pipeline_video share one _run_detector helper.
- /health reports detector_framework ("mmdetection" or "ultralytics").
- Score threshold defaults to 0.3 (DET_SCORE_THR).
- torch pinned to 2.1.2 CPU, numpy < 2 and opencv-headless < 4.11, since
the prebuilt mmcv 2.1.0 CPU ops are compiled against torch 2.1.
- mmcv/mmengine/mmdet installed with --no-deps so they don't pull in GUI
opencv-python; their runtime deps are listed in requirements.txt.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Dockerfile +10 -0
- app.py +102 -42
- requirements.txt +26 -5
|
@@ -9,9 +9,19 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 9 |
libgl1 libglib2.0-0 libgomp1 ffmpeg \
|
| 10 |
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
COPY requirements.txt .
|
| 13 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
COPY --chown=appuser:appuser . .
|
| 16 |
|
| 17 |
USER appuser
|
|
|
|
| 9 |
libgl1 libglib2.0-0 libgomp1 ffmpeg \
|
| 10 |
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
|
| 12 |
+
# torch 2.1 CPU first: the prebuilt mmcv 2.1.0 CPU wheel is compiled against it.
|
| 13 |
+
RUN pip install --no-cache-dir torch==2.1.2 torchvision==0.16.2 \
|
| 14 |
+
--index-url https://download.pytorch.org/whl/cpu
|
| 15 |
+
|
| 16 |
COPY requirements.txt .
|
| 17 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
|
| 19 |
+
# MMDetection stack (mmdet 3.3.0 needs mmcv >=2.0.0rc4,<2.2.0).
|
| 20 |
+
RUN pip install --no-cache-dir --no-deps \
|
| 21 |
+
https://download.openmmlab.com/mmcv/dist/cpu/torch2.1.0/mmcv-2.1.0-cp310-cp310-manylinux1_x86_64.whl \
|
| 22 |
+
mmengine==0.10.7 mmdet==3.3.0 \
|
| 23 |
+
&& python -c "import mmcv, mmdet, mmcv.ops; print('mmcv', mmcv.__version__, '| mmdet', mmdet.__version__)"
|
| 24 |
+
|
| 25 |
COPY --chown=appuser:appuser . .
|
| 26 |
|
| 27 |
USER appuser
|
|
@@ -33,6 +33,7 @@ app.add_middleware(CORSMiddleware, allow_origins=["*"],
|
|
| 33 |
# ββ global model handles ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
detector = None
|
| 35 |
detector_fmt = None
|
|
|
|
| 36 |
face_app = None
|
| 37 |
enhance_zero = None # Zero-DCE++ (low-light)
|
| 38 |
enhance_ffa = None # FFA-Net (fog)
|
|
@@ -381,35 +382,109 @@ def _enhance(img_bgr: np.ndarray, condition: str) -> tuple[np.ndarray, str]:
|
|
| 381 |
|
| 382 |
# ββ startup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
@app.on_event("startup")
|
| 385 |
async def startup():
|
| 386 |
-
global detector, detector_fmt, face_app, enhance_zero, enhance_ffa
|
| 387 |
|
| 388 |
_pull_from_hub()
|
| 389 |
|
| 390 |
-
# ββ detector
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
|
| 414 |
# ββ face analyzer (buffalo_l auto-downloads from InsightFace CDN) βββββββββ
|
| 415 |
try:
|
|
@@ -456,15 +531,7 @@ async def pipeline(body: dict,
|
|
| 456 |
enh_ms = (time.time() - t0) * 1000
|
| 457 |
|
| 458 |
t0 = time.time()
|
| 459 |
-
detections =
|
| 460 |
-
if detector:
|
| 461 |
-
for r in detector(enhanced, verbose=False, conf=0.45, iou=0.45):
|
| 462 |
-
for box in r.boxes:
|
| 463 |
-
detections.append({
|
| 464 |
-
"class": r.names[int(box.cls)],
|
| 465 |
-
"confidence": round(float(box.conf), 4),
|
| 466 |
-
"bbox": _xyxy_to_xywh(box.xyxy[0].tolist()),
|
| 467 |
-
})
|
| 468 |
det_ms = (time.time() - t0) * 1000
|
| 469 |
|
| 470 |
t0 = time.time()
|
|
@@ -552,15 +619,7 @@ async def pipeline_video(body: dict,
|
|
| 552 |
enh_ms_total += (time.time() - t0) * 1000
|
| 553 |
|
| 554 |
t0 = time.time()
|
| 555 |
-
last_dets =
|
| 556 |
-
if detector:
|
| 557 |
-
for r in detector(enhanced, verbose=False, conf=0.45, iou=0.45):
|
| 558 |
-
for box in r.boxes:
|
| 559 |
-
last_dets.append({
|
| 560 |
-
"class": r.names[int(box.cls)],
|
| 561 |
-
"confidence": round(float(box.conf), 4),
|
| 562 |
-
"bbox": _xyxy_to_xywh(box.xyxy[0].tolist()),
|
| 563 |
-
})
|
| 564 |
det_ms_total += (time.time() - t0) * 1000
|
| 565 |
|
| 566 |
t0 = time.time()
|
|
@@ -680,6 +739,7 @@ async def health():
|
|
| 680 |
"status": "ok",
|
| 681 |
"detector": detector is not None,
|
| 682 |
"detector_format": detector_fmt,
|
|
|
|
| 683 |
"face_app": face_app is not None,
|
| 684 |
"enhance_zero_dce": enhance_zero is not None,
|
| 685 |
"enhance_ffa_net": enhance_ffa is not None,
|
|
|
|
| 33 |
# ββ global model handles ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 34 |
detector = None
|
| 35 |
detector_fmt = None
|
| 36 |
+
detector_framework = None # "mmdetection" or "ultralytics"
|
| 37 |
face_app = None
|
| 38 |
enhance_zero = None # Zero-DCE++ (low-light)
|
| 39 |
enhance_ffa = None # FFA-Net (fog)
|
|
|
|
| 382 |
|
| 383 |
# ββ startup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 384 |
|
| 385 |
+
# ββ detector: MMDetection RTMDet (primary) βββββββββββββββββββββββββββββββββββ
|
| 386 |
+
# COCO-pretrained RTMDet-s from the official OpenMMLab model zoo, loaded with
|
| 387 |
+
# MMDetection's own API. COCO covers the outdoor classes this system needs
|
| 388 |
+
# (person, car, bus, truck, bicycle, motorcycle, traffic light, train).
|
| 389 |
+
RTMDET_CONFIG = "rtmdet/rtmdet_s_8xb32-300e_coco.py"
|
| 390 |
+
RTMDET_CKPT_URL = ("https://download.openmmlab.com/mmdetection/v3.0/rtmdet/"
|
| 391 |
+
"rtmdet_s_8xb32-300e_coco/rtmdet_s_8xb32-300e_coco_20220905_161602-387a891e.pth")
|
| 392 |
+
DET_SCORE_THR = float(os.environ.get("DET_SCORE_THR", "0.3"))
|
| 393 |
+
|
| 394 |
+
|
| 395 |
+
def _load_rtmdet():
|
| 396 |
+
"""Build RTMDet-s with mmdet.apis.init_detector. Returns None on any failure."""
|
| 397 |
+
try:
|
| 398 |
+
import mmdet
|
| 399 |
+
from mmdet.apis import init_detector
|
| 400 |
+
cfg_path = os.path.join(os.path.dirname(mmdet.__file__), ".mim", "configs", RTMDET_CONFIG)
|
| 401 |
+
ckpt_path = os.path.join(MODELS, os.path.basename(RTMDET_CKPT_URL))
|
| 402 |
+
if not os.path.exists(ckpt_path):
|
| 403 |
+
os.makedirs(MODELS, exist_ok=True)
|
| 404 |
+
with _requests.get(RTMDET_CKPT_URL, stream=True, timeout=120) as r:
|
| 405 |
+
r.raise_for_status()
|
| 406 |
+
tmp = ckpt_path + ".part"
|
| 407 |
+
with open(tmp, "wb") as f:
|
| 408 |
+
for chunk in r.iter_content(1 << 20):
|
| 409 |
+
f.write(chunk)
|
| 410 |
+
os.rename(tmp, ckpt_path)
|
| 411 |
+
print(f"[hub] downloaded {os.path.basename(ckpt_path)} ({os.path.getsize(ckpt_path)//1024} KB)")
|
| 412 |
+
model = init_detector(cfg_path, ckpt_path, device="cpu")
|
| 413 |
+
print(f"[startup] Detector: RTMDet-s COCO [mmdetection {mmdet.__version__}, CPU]")
|
| 414 |
+
return model
|
| 415 |
+
except Exception as e:
|
| 416 |
+
print(f"[startup] RTMDet (MMDetection) load failed ({e}) β falling back to Ultralytics")
|
| 417 |
+
return None
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def _run_detector(img_bgr: np.ndarray) -> list:
|
| 421 |
+
"""Run the loaded detector and return [{class, confidence, bbox}] dicts."""
|
| 422 |
+
if detector is None:
|
| 423 |
+
return []
|
| 424 |
+
out = []
|
| 425 |
+
if detector_framework == "mmdetection":
|
| 426 |
+
from mmdet.apis import inference_detector
|
| 427 |
+
inst = inference_detector(detector, img_bgr).pred_instances
|
| 428 |
+
names = detector.dataset_meta["classes"]
|
| 429 |
+
for bbox, score, label in zip(inst.bboxes.cpu().numpy(),
|
| 430 |
+
inst.scores.cpu().numpy(),
|
| 431 |
+
inst.labels.cpu().numpy()):
|
| 432 |
+
if score < DET_SCORE_THR:
|
| 433 |
+
continue
|
| 434 |
+
out.append({
|
| 435 |
+
"class": names[int(label)],
|
| 436 |
+
"confidence": round(float(score), 4),
|
| 437 |
+
"bbox": _xyxy_to_xywh(bbox.tolist()),
|
| 438 |
+
})
|
| 439 |
+
return out
|
| 440 |
+
for r in detector(img_bgr, verbose=False, conf=0.45, iou=0.45):
|
| 441 |
+
for box in r.boxes:
|
| 442 |
+
out.append({
|
| 443 |
+
"class": r.names[int(box.cls)],
|
| 444 |
+
"confidence": round(float(box.conf), 4),
|
| 445 |
+
"bbox": _xyxy_to_xywh(box.xyxy[0].tolist()),
|
| 446 |
+
})
|
| 447 |
+
return out
|
| 448 |
+
|
| 449 |
+
|
| 450 |
@app.on_event("startup")
|
| 451 |
async def startup():
|
| 452 |
+
global detector, detector_fmt, detector_framework, face_app, enhance_zero, enhance_ffa
|
| 453 |
|
| 454 |
_pull_from_hub()
|
| 455 |
|
| 456 |
+
# ββ detector: MMDetection RTMDet unless DETECTOR=yolo ββββββββββββββββββββ
|
| 457 |
+
if os.environ.get("DETECTOR", "rtmdet").lower() != "yolo":
|
| 458 |
+
detector = _load_rtmdet()
|
| 459 |
+
if detector is not None:
|
| 460 |
+
detector_fmt = "rtmdet_s_coco"
|
| 461 |
+
detector_framework = "mmdetection"
|
| 462 |
+
|
| 463 |
+
# ββ fallback detector (prefer ONNX, fallback to .pt, fallback to pretrained)
|
| 464 |
+
if detector is None:
|
| 465 |
+
try:
|
| 466 |
+
from ultralytics import YOLO
|
| 467 |
+
candidates = [
|
| 468 |
+
(f"{MODELS}/yolov8n_best.onnx", "onnx"),
|
| 469 |
+
(f"{MODELS}/yolov8n_int8.onnx", "onnx_int8"),
|
| 470 |
+
(f"{MODELS}/yolov8n_outdoor_aug_best.pt", "pytorch_aug"),
|
| 471 |
+
(f"{MODELS}/yolov8n_baseline_best.pt", "pytorch_baseline"),
|
| 472 |
+
(f"{MODELS}/rtdetr_outdoor_aug_best.pt", "rtdetr"),
|
| 473 |
+
]
|
| 474 |
+
for path, fmt in candidates:
|
| 475 |
+
if os.path.exists(path):
|
| 476 |
+
detector = YOLO(path)
|
| 477 |
+
detector_fmt = fmt
|
| 478 |
+
print(f"[startup] Detector: {os.path.basename(path)} [{fmt}]")
|
| 479 |
+
break
|
| 480 |
+
if detector is None:
|
| 481 |
+
# pretrained fallback β YOLO auto-downloads yolov8n.pt on first call
|
| 482 |
+
detector = YOLO("yolov8n.pt")
|
| 483 |
+
detector_fmt = "pytorch_pretrained"
|
| 484 |
+
print("[startup] Detector: yolov8n.pt [pytorch_pretrained] (auto-downloaded)")
|
| 485 |
+
detector_framework = "ultralytics"
|
| 486 |
+
except Exception as e:
|
| 487 |
+
print(f"[startup] Detector load failed: {e}")
|
| 488 |
|
| 489 |
# ββ face analyzer (buffalo_l auto-downloads from InsightFace CDN) βββββββββ
|
| 490 |
try:
|
|
|
|
| 531 |
enh_ms = (time.time() - t0) * 1000
|
| 532 |
|
| 533 |
t0 = time.time()
|
| 534 |
+
detections = _run_detector(enhanced)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 535 |
det_ms = (time.time() - t0) * 1000
|
| 536 |
|
| 537 |
t0 = time.time()
|
|
|
|
| 619 |
enh_ms_total += (time.time() - t0) * 1000
|
| 620 |
|
| 621 |
t0 = time.time()
|
| 622 |
+
last_dets = _run_detector(enhanced)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 623 |
det_ms_total += (time.time() - t0) * 1000
|
| 624 |
|
| 625 |
t0 = time.time()
|
|
|
|
| 739 |
"status": "ok",
|
| 740 |
"detector": detector is not None,
|
| 741 |
"detector_format": detector_fmt,
|
| 742 |
+
"detector_framework": detector_framework,
|
| 743 |
"face_app": face_app is not None,
|
| 744 |
"enhance_zero_dce": enhance_zero is not None,
|
| 745 |
"enhance_ffa_net": enhance_ffa is not None,
|
|
@@ -1,11 +1,16 @@
|
|
| 1 |
# HuggingFace Spaces inference API β CPU inference
|
| 2 |
-
#
|
| 3 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
fastapi>=0.111.0
|
| 6 |
uvicorn[standard]>=0.30.0
|
| 7 |
-
opencv-python-headless>=4.10.0
|
| 8 |
-
numpy>=1.26.0
|
| 9 |
requests>=2.31.0
|
| 10 |
python-multipart>=0.0.9
|
| 11 |
ultralytics>=8.4.0
|
|
@@ -13,4 +18,20 @@ insightface>=0.7.3
|
|
| 13 |
onnxruntime>=1.18.0
|
| 14 |
faiss-cpu>=1.8.0
|
| 15 |
huggingface_hub>=0.23.0
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# HuggingFace Spaces inference API β CPU inference
|
| 2 |
+
# Detection: MMDetection RTMDet-s (primary), Ultralytics YOLOv8 (fallback).
|
| 3 |
+
# torch/torchvision are installed first in the Dockerfile from the CPU index and
|
| 4 |
+
# pinned here so nothing upgrades them: mmcv 2.1.0's prebuilt CPU ops are built
|
| 5 |
+
# for torch 2.1, and torch 2.1 needs numpy < 2.
|
| 6 |
+
|
| 7 |
+
torch==2.1.2
|
| 8 |
+
torchvision==0.16.2
|
| 9 |
+
numpy>=1.26,<2
|
| 10 |
+
opencv-python-headless>=4.10.0,<4.11
|
| 11 |
|
| 12 |
fastapi>=0.111.0
|
| 13 |
uvicorn[standard]>=0.30.0
|
|
|
|
|
|
|
| 14 |
requests>=2.31.0
|
| 15 |
python-multipart>=0.0.9
|
| 16 |
ultralytics>=8.4.0
|
|
|
|
| 18 |
onnxruntime>=1.18.0
|
| 19 |
faiss-cpu>=1.8.0
|
| 20 |
huggingface_hub>=0.23.0
|
| 21 |
+
|
| 22 |
+
# Runtime deps of mmengine / mmcv / mmdet. Those three are installed with
|
| 23 |
+
# --no-deps in the Dockerfile so they don't pull in GUI opencv-python.
|
| 24 |
+
addict
|
| 25 |
+
yapf
|
| 26 |
+
termcolor
|
| 27 |
+
rich
|
| 28 |
+
pyyaml
|
| 29 |
+
packaging
|
| 30 |
+
Pillow
|
| 31 |
+
matplotlib
|
| 32 |
+
scipy
|
| 33 |
+
shapely
|
| 34 |
+
six
|
| 35 |
+
terminaltables
|
| 36 |
+
tqdm
|
| 37 |
+
pycocotools
|