scorevision: push artifact
Browse files
miner.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
TurboVision miner: YOLO11n detection weights shipped as yolo11n.pt in this repo root.
|
| 3 |
+
|
| 4 |
+
Place `yolo11n.pt` next to this file before `sv push` (e.g. download via Ultralytics locally).
|
| 5 |
+
Detection-only: bounding boxes from YOLO11n; keypoints are padded to n_keypoints with (0, 0).
|
| 6 |
+
|
| 7 |
+
Emitted cls_id values are subnet indices for manifests whose objects list is:
|
| 8 |
+
[bus, car, truck, motorcycle] (e.g. manak0/Detect-detect-vehicle)
|
| 9 |
+
YOLO COCO-80 class ids are remapped; other COCO classes are skipped.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
from logging import getLogger
|
| 13 |
+
from pathlib import Path
|
| 14 |
+
|
| 15 |
+
from numpy import ndarray
|
| 16 |
+
from pydantic import BaseModel
|
| 17 |
+
from ultralytics import YOLO
|
| 18 |
+
|
| 19 |
+
logger = getLogger(__name__)
|
| 20 |
+
|
| 21 |
+
WEIGHTS_NAME = "yolo11n.pt"
|
| 22 |
+
|
| 23 |
+
# Must match manifest `objects` order for the vehicle element.
|
| 24 |
+
SUBNET_VEHICLE_LABELS: tuple[str, ...] = ("bus", "car", "truck", "motorcycle")
|
| 25 |
+
|
| 26 |
+
# Ultralytics COCO-80 -> subnet index (see SUBNET_VEHICLE_LABELS).
|
| 27 |
+
COCO_TO_SUBNET_VEHICLE: dict[int, int] = {
|
| 28 |
+
5: 0, # bus
|
| 29 |
+
2: 1, # car
|
| 30 |
+
7: 2, # truck
|
| 31 |
+
3: 3, # motorcycle
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def subnet_vehicle_label(cls_id: int) -> str:
|
| 36 |
+
if 0 <= cls_id < len(SUBNET_VEHICLE_LABELS):
|
| 37 |
+
return SUBNET_VEHICLE_LABELS[cls_id]
|
| 38 |
+
return "?"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class BoundingBox(BaseModel):
|
| 42 |
+
x1: int
|
| 43 |
+
y1: int
|
| 44 |
+
x2: int
|
| 45 |
+
y2: int
|
| 46 |
+
cls_id: int
|
| 47 |
+
conf: float
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class TVFrameResult(BaseModel):
|
| 51 |
+
frame_id: int
|
| 52 |
+
boxes: list[BoundingBox]
|
| 53 |
+
keypoints: list[tuple[int, int]]
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class Miner:
|
| 57 |
+
"""
|
| 58 |
+
Loads YOLO11n from the Hugging Face repo snapshot. Must define `Miner` with
|
| 59 |
+
`predict_batch` as required by the TurboVision chute template.
|
| 60 |
+
"""
|
| 61 |
+
|
| 62 |
+
def __init__(self, path_hf_repo: Path) -> None:
|
| 63 |
+
weights = path_hf_repo / WEIGHTS_NAME
|
| 64 |
+
if not weights.is_file():
|
| 65 |
+
raise FileNotFoundError(
|
| 66 |
+
f"Missing {WEIGHTS_NAME} in repo root. Add weights before push."
|
| 67 |
+
)
|
| 68 |
+
self.model = YOLO(str(weights))
|
| 69 |
+
logger.info("YOLO11n weights loaded from %s", weights.name)
|
| 70 |
+
|
| 71 |
+
def __repr__(self) -> str:
|
| 72 |
+
return f"Model: YOLO ({type(self.model).__name__})"
|
| 73 |
+
|
| 74 |
+
def predict_batch(
|
| 75 |
+
self,
|
| 76 |
+
batch_images: list[ndarray],
|
| 77 |
+
offset: int,
|
| 78 |
+
n_keypoints: int,
|
| 79 |
+
) -> list[TVFrameResult]:
|
| 80 |
+
bboxes: dict[int, list[BoundingBox]] = {}
|
| 81 |
+
results_run = self.model.predict(batch_images, verbose=False)
|
| 82 |
+
if results_run is not None:
|
| 83 |
+
for frame_number_in_batch, detection in enumerate(results_run):
|
| 84 |
+
frame_id = offset + frame_number_in_batch
|
| 85 |
+
boxes: list[BoundingBox] = []
|
| 86 |
+
if hasattr(detection, "boxes") and detection.boxes is not None:
|
| 87 |
+
for box in detection.boxes.data:
|
| 88 |
+
row = box.tolist()
|
| 89 |
+
if len(row) < 6:
|
| 90 |
+
continue
|
| 91 |
+
x1, y1, x2, y2, conf, coco_cls = row[:6]
|
| 92 |
+
subnet_cls = COCO_TO_SUBNET_VEHICLE.get(int(coco_cls))
|
| 93 |
+
if subnet_cls is None:
|
| 94 |
+
continue
|
| 95 |
+
boxes.append(
|
| 96 |
+
BoundingBox(
|
| 97 |
+
x1=int(x1),
|
| 98 |
+
y1=int(y1),
|
| 99 |
+
x2=int(x2),
|
| 100 |
+
y2=int(y2),
|
| 101 |
+
cls_id=subnet_cls,
|
| 102 |
+
conf=float(conf),
|
| 103 |
+
)
|
| 104 |
+
)
|
| 105 |
+
bboxes[frame_id] = boxes
|
| 106 |
+
|
| 107 |
+
zero_kp = [(0, 0)] * n_keypoints
|
| 108 |
+
out: list[TVFrameResult] = []
|
| 109 |
+
for i in range(len(batch_images)):
|
| 110 |
+
frame_id = offset + i
|
| 111 |
+
out.append(
|
| 112 |
+
TVFrameResult(
|
| 113 |
+
frame_id=frame_id,
|
| 114 |
+
boxes=bboxes.get(frame_id, []),
|
| 115 |
+
keypoints=list(zero_kp),
|
| 116 |
+
)
|
| 117 |
+
)
|
| 118 |
+
return out
|