Upload detect_1960.py
Browse files- detect_1960.py +64 -0
detect_1960.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""RF-DETR @1960 player detection for tracking pre-annotation.
|
| 2 |
+
|
| 3 |
+
Detects player (class_id=1) on frames 0..f1.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
uv run detect_1960.py <video> <f1> <out.npy> <ckpt> [conf=0.4]
|
| 7 |
+
"""
|
| 8 |
+
from __future__ import annotations
|
| 9 |
+
|
| 10 |
+
import os
|
| 11 |
+
import sys
|
| 12 |
+
|
| 13 |
+
import cv2
|
| 14 |
+
import numpy as np
|
| 15 |
+
import torch
|
| 16 |
+
from PIL import Image
|
| 17 |
+
|
| 18 |
+
_orig_load = torch.load
|
| 19 |
+
torch.load = lambda *a, **k: _orig_load(*a, **{**k, "weights_only": False})
|
| 20 |
+
|
| 21 |
+
from rfdetr import RFDETRLarge
|
| 22 |
+
|
| 23 |
+
PLAYER_CLASS_ID = 1
|
| 24 |
+
DEFAULT_CONF = 0.4
|
| 25 |
+
RESOLUTION = 1960
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def main() -> None:
|
| 29 |
+
video_path = sys.argv[1]
|
| 30 |
+
frame_end = int(sys.argv[2])
|
| 31 |
+
out_path = sys.argv[3]
|
| 32 |
+
ckpt = sys.argv[4]
|
| 33 |
+
conf = float(sys.argv[5]) if len(sys.argv) > 5 else DEFAULT_CONF
|
| 34 |
+
|
| 35 |
+
detector = RFDETRLarge(pretrain_weights=ckpt)
|
| 36 |
+
for obj in (detector, detector.model):
|
| 37 |
+
if hasattr(obj, "resolution"):
|
| 38 |
+
obj.resolution = RESOLUTION
|
| 39 |
+
print(f"resolution={detector.model.resolution} conf={conf}", flush=True)
|
| 40 |
+
|
| 41 |
+
cap = cv2.VideoCapture(video_path)
|
| 42 |
+
rows: list[list[float]] = []
|
| 43 |
+
frame_idx = 0
|
| 44 |
+
while frame_idx <= frame_end:
|
| 45 |
+
ok, img = cap.read()
|
| 46 |
+
if not ok:
|
| 47 |
+
break
|
| 48 |
+
rgb = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
|
| 49 |
+
result = detector.predict(rgb, threshold=conf)
|
| 50 |
+
for box, class_id, score in zip(result.xyxy, result.class_id, result.confidence):
|
| 51 |
+
if int(class_id) != PLAYER_CLASS_ID:
|
| 52 |
+
continue
|
| 53 |
+
x1, y1, x2, y2 = map(float, box)
|
| 54 |
+
rows.append([frame_idx, x1, y1, x2, y2, float(score)])
|
| 55 |
+
frame_idx += 1
|
| 56 |
+
cap.release()
|
| 57 |
+
|
| 58 |
+
os.makedirs(os.path.dirname(out_path) or ".", exist_ok=True)
|
| 59 |
+
np.save(out_path, np.array(rows, dtype=np.float32))
|
| 60 |
+
print(f"frames={frame_idx} player_boxes={len(rows)} -> {out_path}", flush=True)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
main()
|