Datasets:
Add annotated sample pack: quality, timeline, clip captions, keyframe masks (v1/v2), SAM 3.1 dense tracks
07f5340 verified Download annotated/scripts/01_frame_metrics.py from hkust-aaron/laparoscopic-robotic-vision-sample: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/datasets/hkust-aaron/laparoscopic-robotic-vision-sample/resolve/main/annotated/scripts/01_frame_metrics.py
- Command line
-
hf download hf://datasets/hkust-aaron/laparoscopic-robotic-vision-sample/annotated/scripts/01_frame_metrics.py
-
curl -L -o 01_frame_metrics.py https://huggingface.co/datasets/hkust-aaron/laparoscopic-robotic-vision-sample/resolve/main/annotated/scripts/01_frame_metrics.py
2.77 kB
| """Per-frame quality metrics for the whole video + review contact sheets. | |
| Usage: python 01_frame_metrics.py <video> <out_csv> <sheet_dir> | |
| Metrics (every source frame): | |
| sharpness variance of Laplacian on the grey image (higher = sharper) | |
| brightness mean grey level 0-255 | |
| red_ratio mean R / (R+G+B); intracorporeal tissue is strongly red | |
| green_ratio mean G / (R+G+B) | |
| saturated fraction of pixels with grey >= 250 (glare / light flooding) | |
| dark fraction of pixels with grey <= 15 | |
| motion mean absolute grey difference to the previous frame | |
| """ | |
| import csv | |
| import sys | |
| from pathlib import Path | |
| 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) | |
| cap = cv2.VideoCapture(video) | |
| fps = cap.get(cv2.CAP_PROP_FPS) | |
| SHEET_STEP_S = 4 # one thumbnail every 4 s for the overview sheets | |
| COLS, ROWS, TW, TH = 5, 6, 288, 230 | |
| thumbs = [] | |
| prev = None | |
| rows = [] | |
| idx = 0 | |
| while True: | |
| ok, frame = cap.read() | |
| if not ok: | |
| break | |
| grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) | |
| small = cv2.resize(grey, (180, 144), interpolation=cv2.INTER_AREA) | |
| b, g, r = [c.astype(np.float64).mean() for c in cv2.split(frame)] | |
| total = r + g + b + 1e-9 | |
| motion = 0.0 if prev is None else float(np.abs(small.astype(np.int16) - prev).mean()) | |
| prev = small.astype(np.int16) | |
| rows.append([ | |
| idx, round(idx / fps, 3), | |
| round(float(cv2.Laplacian(grey, cv2.CV_64F).var()), 2), | |
| round(float(grey.mean()), 2), | |
| round(r / total, 4), round(g / total, 4), | |
| round(float((grey >= 250).mean()), 4), | |
| round(float((grey <= 15).mean()), 4), | |
| round(motion, 3), | |
| ]) | |
| if idx % int(round(fps * SHEET_STEP_S)) == 0: | |
| 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) | |
| idx += 1 | |
| cap.release() | |
| with open(out_csv, "w", newline="") as f: | |
| w = csv.writer(f) | |
| w.writerow(["frame_index", "timestamp_seconds", "sharpness", "brightness", "red_ratio", | |
| "green_ratio", "saturated", "dark", "motion"]) | |
| w.writerows(rows) | |
| per = COLS * ROWS | |
| for s in range(0, len(thumbs), per): | |
| chunk = thumbs[s:s + per] | |
| sheet = np.zeros((ROWS * TH, COLS * TW, 3), np.uint8) | |
| for i, t in enumerate(chunk): | |
| y, x = divmod(i, COLS) | |
| sheet[y * TH:(y + 1) * TH, x * TW:(x + 1) * TW] = t | |
| cv2.imwrite(str(sheet_dir / f"overview_{s // per:02d}.jpg"), sheet, [cv2.IMWRITE_JPEG_QUALITY, 85]) | |
| print("frames", idx, "fps", fps, "sheets", (len(thumbs) + per - 1) // per) | |