Download miner.py from EliasCarter/ScoreVision: direct link, hf CLI and curl.
- Browser
- Download file 3.38 kB
-
https://huggingface.co/EliasCarter/ScoreVision/resolve/main/miner.py
- Command line
-
hf download hf://EliasCarter/ScoreVision/miner.py
-
curl -L -o miner.py https://huggingface.co/EliasCarter/ScoreVision/resolve/main/miner.py
3.38 kB
| """ | |
| Emitted cls_id values are subnet indices for manifests whose objects list is: | |
| [bus, car, truck, motorcycle] (e.g. manak0/Detect-detect-vehicle) | |
| YOLO COCO-80 class ids are remapped; other COCO classes are skipped. | |
| """ | |
| from logging import getLogger | |
| from pathlib import Path | |
| from numpy import ndarray | |
| from pydantic import BaseModel | |
| from ultralytics import YOLO | |
| logger = getLogger(__name__) | |
| WEIGHTS_NAME = "best.pt" | |
| # Must match manifest `objects` order for the vehicle element. | |
| SUBNET_VEHICLE_LABELS: tuple[str, ...] = ("bus", "car", "truck", "motorcycle") | |
| # Ultralytics COCO-80 -> subnet index (see SUBNET_VEHICLE_LABELS). | |
| COCO_TO_SUBNET_VEHICLE: dict[int, int] = { | |
| 5: 0, # bus | |
| 2: 1, # car | |
| 7: 2, # truck | |
| 3: 3, # motorcycle | |
| } | |
| def subnet_vehicle_label(cls_id: int) -> str: | |
| if 0 <= cls_id < len(SUBNET_VEHICLE_LABELS): | |
| return SUBNET_VEHICLE_LABELS[cls_id] | |
| return "?" | |
| class BoundingBox(BaseModel): | |
| x1: int | |
| y1: int | |
| x2: int | |
| y2: int | |
| cls_id: int | |
| conf: float | |
| class TVFrameResult(BaseModel): | |
| frame_id: int | |
| boxes: list[BoundingBox] | |
| keypoints: list[tuple[int, int]] | |
| class Miner: | |
| def __init__(self, path_hf_repo: Path) -> None: | |
| weights = path_hf_repo / WEIGHTS_NAME | |
| if not weights.is_file(): | |
| raise FileNotFoundError( | |
| f"Missing {WEIGHTS_NAME} in repo root. Add weights before push." | |
| ) | |
| self.model = YOLO(str(weights)) | |
| def __repr__(self) -> str: | |
| return f"Model: YOLO ({type(self.model).__name__})" | |
| def predict_batch( | |
| self, | |
| batch_images: list[ndarray], | |
| offset: int, | |
| n_keypoints: int, | |
| ) -> list[TVFrameResult]: | |
| bboxes: dict[int, list[BoundingBox]] = {} | |
| results_run = self.model.predict(batch_images, verbose=False) | |
| if results_run is not None: | |
| for frame_number_in_batch, detection in enumerate(results_run): | |
| frame_id = offset + frame_number_in_batch | |
| boxes: list[BoundingBox] = [] | |
| if hasattr(detection, "boxes") and detection.boxes is not None: | |
| for box in detection.boxes.data: | |
| row = box.tolist() | |
| if len(row) < 6: | |
| continue | |
| x1, y1, x2, y2, conf, coco_cls = row[:6] | |
| subnet_cls = COCO_TO_SUBNET_VEHICLE.get(int(coco_cls)) | |
| if subnet_cls is None: | |
| continue | |
| boxes.append( | |
| BoundingBox( | |
| x1=int(x1), | |
| y1=int(y1), | |
| x2=int(x2), | |
| y2=int(y2), | |
| cls_id=subnet_cls, | |
| conf=float(conf), | |
| ) | |
| ) | |
| bboxes[frame_id] = boxes | |
| zero_kp = [(0, 0)] * n_keypoints | |
| out: list[TVFrameResult] = [] | |
| for i in range(len(batch_images)): | |
| frame_id = offset + i | |
| out.append( | |
| TVFrameResult( | |
| frame_id=frame_id, | |
| boxes=bboxes.get(frame_id, []), | |
| keypoints=list(zero_kp), | |
| ) | |
| ) | |
| return out | |