Datasets:
Add annotated sample pack: quality, timeline, clip captions, keyframe masks (v1/v2), SAM 3.1 dense tracks
07f5340 verified Download annotated/scripts/09_dense_preview.py from hkust-aaron/laparoscopic-robotic-vision-sample: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/datasets/hkust-aaron/laparoscopic-robotic-vision-sample/resolve/main/annotated/scripts/09_dense_preview.py
- Command line
-
hf download hf://datasets/hkust-aaron/laparoscopic-robotic-vision-sample/annotated/scripts/09_dense_preview.py
-
curl -L -o 09_dense_preview.py https://huggingface.co/datasets/hkust-aaron/laparoscopic-robotic-vision-sample/resolve/main/annotated/scripts/09_dense_preview.py
2.22 kB
| """Preview sheets for the dense SAM 3.1 tracks. | |
| Usage: python 09_dense_preview.py <video> <dense_tracks_dir> | |
| Draws the masks of 24 evenly spaced frames (and, per clip, frames 0 / 125 / 249) | |
| over the video and writes dense_tracks/previews/overview.jpg and | |
| previews/clip_XX.jpg. Colour = class; text-prompt tracks are grey-blue. | |
| """ | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import cv2 | |
| import numpy as np | |
| video, root = sys.argv[1], Path(sys.argv[2]) | |
| COL = {"white_insulated_jawed": (255, 200, 0), "dark_rod": (0, 165, 255), "fenestrated_grasper": (0, 255, 0), | |
| "gauze": (255, 0, 255), "instrument_text_detected": (200, 160, 120)} | |
| meta = {int(p.stem[5:]): json.load(open(p)) for p in sorted((root / "tracks").glob("clip_*.json"))} | |
| cap = cv2.VideoCapture(video) | |
| def overlay(f): | |
| cap.set(cv2.CAP_PROP_POS_FRAMES, f) | |
| ok, img = cap.read() | |
| c = f // 250 | |
| inst = cv2.imread(str(root / "masks" / f"clip_{c:02d}" / f"{f:05d}.png"), 0) | |
| for tid in np.unique(inst)[1:]: | |
| m = (inst == tid).astype(np.uint8) | |
| col = COL[meta[c]["tracks"][str(tid)]["category"]] | |
| img[m == 1] = (0.55 * img[m == 1] + 0.45 * np.array(col)).astype(np.uint8) | |
| cnts, _ = cv2.findContours(m, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
| cv2.drawContours(img, cnts, -1, col, 2) | |
| ys, xs = np.nonzero(m) | |
| cv2.putText(img, str(tid), (int(xs.mean()), int(ys.mean())), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2) | |
| cv2.putText(img, f"frame {f}", (8, 24), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2) | |
| return cv2.resize(img, (360, 288)) | |
| def sheet(frames, cols): | |
| tiles = [overlay(f) for f in frames] | |
| while len(tiles) % cols: | |
| tiles.append(np.zeros_like(tiles[0])) | |
| return np.vstack([np.hstack(tiles[i:i + cols]) for i in range(0, len(tiles), cols)]) | |
| out = root / "previews" | |
| out.mkdir(exist_ok=True) | |
| cv2.imwrite(str(out / "overview.jpg"), sheet([int(x) for x in np.linspace(0, 17369, 24)], 6), [cv2.IMWRITE_JPEG_QUALITY, 85]) | |
| for c, m in meta.items(): | |
| s, n = m["start_frame"], m["num_frames"] | |
| cv2.imwrite(str(out / f"clip_{c:02d}.jpg"), sheet([s, s + n // 2, s + n - 1], 3), [cv2.IMWRITE_JPEG_QUALITY, 85]) | |
| print("previews", len(meta) + 1) | |