File size: 1,830 Bytes
81d5f19 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | """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()
|