Datasets:
Add annotated sample pack: quality, timeline, clip captions, keyframe masks (v1/v2), SAM 3.1 dense tracks
07f5340 verified Download annotated/scripts/04_clip_motion.py from hkust-aaron/laparoscopic-robotic-vision-sample: direct link, hf CLI and curl.
- Browser
- Download file 4.1 kB
-
https://huggingface.co/datasets/hkust-aaron/laparoscopic-robotic-vision-sample/resolve/main/annotated/scripts/04_clip_motion.py
- Command line
-
hf download hf://datasets/hkust-aaron/laparoscopic-robotic-vision-sample/annotated/scripts/04_clip_motion.py
-
curl -L -o 04_clip_motion.py https://huggingface.co/datasets/hkust-aaron/laparoscopic-robotic-vision-sample/resolve/main/annotated/scripts/04_clip_motion.py
4.1 kB
| """Per-clip camera/scene motion statistics and offset overview sheets. | |
| Usage: python 04_clip_motion.py <video> <out_csv> <sheet_dir> | |
| The video is cut into 10 s clips (250 frames). Every 5th frame, corner features | |
| are tracked to the frame 5 later (Lucas-Kanade) and a similarity transform | |
| (shift + zoom + rotation) is fitted with RANSAC. Tissue deforms and instruments | |
| move, so this is a rough whole-image motion estimate, not camera pose. | |
| Per clip: | |
| shift_px_per_s median image shift speed | |
| inlier_ratio median share of tracks that agree with the fitted transform | |
| motion_level low / medium / high = tercile of shift_px_per_s within this video | |
| Only the relative ranking is meaningful. On this footage just 25-60 % of tracks | |
| agree with a single transform (glare, deforming tissue, moving instruments), so | |
| absolute speeds are inflated and a zoom/pan direction could not be estimated | |
| reliably; an earlier zoom_in/zoom_out label was dropped for that reason. | |
| Sheets: one thumbnail every 4 s starting at 2 s (the first overview set starts at 0 s). | |
| """ | |
| import csv | |
| import sys | |
| from pathlib import Path | |
| from statistics import median | |
| import cv2 | |
| import numpy as np | |
| video, out_csv, sheet_dir = sys.argv[1], sys.argv[2], Path(sys.argv[3]) | |
| sheet_dir.mkdir(parents=True, exist_ok=True) | |
| CLIP, STEP, FPS = 250, 5, 25 | |
| COLS, ROWS, TW, TH = 5, 6, 288, 230 | |
| cap = cv2.VideoCapture(video) | |
| prev, idx, pairs, thumbs = None, 0, {}, [] | |
| while True: | |
| ok, frame = cap.read() | |
| if not ok: | |
| break | |
| if idx % 100 == 50: | |
| t = cv2.resize(frame, (TW, TH), interpolation=cv2.INTER_AREA) | |
| cv2.rectangle(t, (0, 0), (TW, 16), (0, 0, 0), -1) | |
| cv2.putText(t, f"{idx / FPS:6.1f}s f{idx}", (4, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1) | |
| thumbs.append(t) | |
| if idx % STEP == 0: | |
| grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| if prev is not None: | |
| p0 = cv2.goodFeaturesToTrack(prev, 300, 0.01, 12) | |
| rec = None | |
| if p0 is not None and len(p0) >= 20: | |
| p1, st, _ = cv2.calcOpticalFlowPyrLK(prev, grey, p0, None, winSize=(21, 21), maxLevel=3) | |
| good = st.reshape(-1) == 1 | |
| if good.sum() >= 20: | |
| m, inl = cv2.estimateAffinePartial2D(p0[good], p1[good], method=cv2.RANSAC, | |
| ransacReprojThreshold=3.0) | |
| if m is not None: | |
| scale = float(np.hypot(m[0, 0], m[1, 0])) | |
| centre = np.array([360.0, 288.0]) | |
| shift = float(np.linalg.norm(m[:, :2] @ centre + m[:, 2] - centre)) | |
| rec = (shift, scale, float(inl.mean())) | |
| pairs.setdefault((idx - STEP) // CLIP, []).append(rec) | |
| prev = grey | |
| idx += 1 | |
| cap.release() | |
| with open(out_csv, "w", newline="") as f: | |
| w = csv.writer(f) | |
| w.writerow(["clip_id", "start_frame", "end_frame_exclusive", "shift_px_per_s", "inlier_ratio", | |
| "pairs_fitted", "motion_level"]) | |
| shifts = sorted(median(r[0] for r in v if r) * FPS / STEP for v in pairs.values() if any(v)) | |
| t1, t2 = shifts[len(shifts) // 3], shifts[2 * len(shifts) // 3] | |
| for c in sorted(pairs): | |
| recs = [r for r in pairs[c] if r] | |
| if not recs: | |
| w.writerow([c, c * CLIP, min(idx, (c + 1) * CLIP), "", "", 0, "unknown"]) | |
| continue | |
| shift = median(r[0] for r in recs) * FPS / STEP | |
| inl = median(r[2] for r in recs) | |
| level = "low" if shift < t1 else ("medium" if shift < t2 else "high") | |
| w.writerow([c, c * CLIP, min(idx, (c + 1) * CLIP), round(shift, 1), round(inl, 2), len(recs), level]) | |
| per = COLS * ROWS | |
| for s in range(0, len(thumbs), per): | |
| sheet = np.zeros((ROWS * TH, COLS * TW, 3), np.uint8) | |
| for i, t in enumerate(thumbs[s:s + per]): | |
| y, x = divmod(i, COLS) | |
| sheet[y * TH:(y + 1) * TH, x * TW:(x + 1) * TW] = t | |
| cv2.imwrite(str(sheet_dir / f"offset_{s // per:02d}.jpg"), sheet, [cv2.IMWRITE_JPEG_QUALITY, 85]) | |
| print("frames", idx, "clips", len(pairs), "thumbs", len(thumbs)) | |