Download detect_1960.py from tmpdata/detect: direct link, hf CLI and curl.
- Browser
- Download file 1.83 kB
-
https://huggingface.co/tmpdata/detect/resolve/main/detect_1960.py
- Command line
-
hf download hf://tmpdata/detect/detect_1960.py
-
curl -L -o detect_1960.py https://huggingface.co/tmpdata/detect/resolve/main/detect_1960.py
1.83 kB
| """RF-DETR @1960 player detection for tracking pre-annotation. | |
| Detects player (class_id=1) on frames 0..f1. | |
| Usage: | |
| uv run detect_1960.py <video> <f1> <out.npy> <ckpt> [conf=0.4] | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import sys | |
| import cv2 | |
| import numpy as np | |
| import torch | |
| from PIL import Image | |
| _orig_load = torch.load | |
| torch.load = lambda *a, **k: _orig_load(*a, **{**k, "weights_only": False}) | |
| from rfdetr import RFDETRLarge | |
| PLAYER_CLASS_ID = 1 | |
| DEFAULT_CONF = 0.4 | |
| RESOLUTION = 1960 | |
| def main() -> None: | |
| video_path = sys.argv[1] | |
| frame_end = int(sys.argv[2]) | |
| out_path = sys.argv[3] | |
| ckpt = sys.argv[4] | |
| conf = float(sys.argv[5]) if len(sys.argv) > 5 else DEFAULT_CONF | |
| detector = RFDETRLarge(pretrain_weights=ckpt) | |
| for obj in (detector, detector.model): | |
| if hasattr(obj, "resolution"): | |
| obj.resolution = RESOLUTION | |
| print(f"resolution={detector.model.resolution} conf={conf}", flush=True) | |
| cap = cv2.VideoCapture(video_path) | |
| rows: list[list[float]] = [] | |
| frame_idx = 0 | |
| while frame_idx <= frame_end: | |
| ok, img = cap.read() | |
| if not ok: | |
| break | |
| rgb = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) | |
| result = detector.predict(rgb, threshold=conf) | |
| for box, class_id, score in zip(result.xyxy, result.class_id, result.confidence): | |
| if int(class_id) != PLAYER_CLASS_ID: | |
| continue | |
| x1, y1, x2, y2 = map(float, box) | |
| rows.append([frame_idx, x1, y1, x2, y2, float(score)]) | |
| frame_idx += 1 | |
| cap.release() | |
| os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True) | |
| np.save(out_path, np.array(rows, dtype=np.float32)) | |
| print(f"frames={frame_idx} player_boxes={len(rows)} -> {out_path}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |