CRICKET / code /labeling.py
d9993r's picture
Add files using upload-large-folder tool
ea1a74e verified
Raw History Blame Contribute Delete
2.44 kB
"""Ball-label frames for the review site (standard sections 7.4 and 9.2).
Picks a few frames per delivery between release and about 0.8 s (real) later,
saves them as images with the current automatic ball position as a proposal,
and puts deliveries where the ball was NOT tracked first. Reviewers click the
ball (or "no ball visible") in the Ball labels tab; train_ball.py turns those
clicks into a training set for a cricket-specific ball detector."""
import cv2
from common import absp, rel, write_json
def extract_label_frames(deliveries, cfg, R, per_delivery=4, force=False):
out_dir = R / "labeling" / "frames"
man_path = R / "labeling" / "manifest.json"
if man_path.exists() and not force:
import json
return json.load(open(man_path))
out_dir.mkdir(parents=True, exist_ok=True)
src_paths = {}
import json
for s in json.load(open(R / "registry" / "sources.json")):
src_paths[s["source_id"]] = s["path"]
items = []
for d in deliveries:
rel_ev = d["events"].get("ball_release")
if d["qc"]["status"] == "fail" or not rel_ev:
continue
w = d["window"]
span = 0.8 * w["time_scale"]
ts = [rel_ev["t_clip"] + span * (k + 1) / (per_delivery + 1) for k in range(per_delivery)]
cap = cv2.VideoCapture(str(absp(src_paths[d["source_id"]])))
pts = d["ball"]["points"]
tracked = len(pts) >= 3
for t in ts:
if t > w["duration_video_s"]:
continue
fi = int(w["first_frame"] + round(t * w["fps"]))
cap.set(cv2.CAP_PROP_POS_FRAMES, fi)
ok, img = cap.read()
if not ok:
continue
fid = f"{d['delivery_id']}_f{fi}"
p = out_dir / f"{fid}.jpg"
cv2.imwrite(str(p), img, [cv2.IMWRITE_JPEG_QUALITY, 92])
near = [q for q in pts if abs(q["t_clip"] - t) <= 0.6 / w["fps"]]
items.append({"id": fid, "delivery_id": d["delivery_id"], "source_id": d["source_id"], "frame": fi,
"t_clip": round(t, 3), "image": rel(p), "size": [img.shape[1], img.shape[0]],
"proposal": {"x": near[0]["x"], "y": near[0]["y"]} if near else None,
"priority": 1.0 if not tracked else 0.3})
cap.release()
items.sort(key=lambda it: (-it["priority"], it["id"]))
write_json(man_path, items)
return items