skills/cv/video-frame-sampling-pipeline/SKILL.md
Efficiently sample N evenly-spaced frames from a video using OpenCV grab/retrieve pattern with optional resize for batch face detection or classification
npx skillsauth add wenmin-wu/ds-skills cv-video-frame-sampling-pipelineInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Processing every frame of a video is wasteful for classification tasks. Sampling N evenly-spaced frames via np.linspace and using OpenCV's grab()/retrieve() pattern (grab skips decoding, retrieve decodes only selected frames) is 3-5x faster than reading every frame. This produces a fixed-size batch suitable for CNN inference or face detection.
import cv2
import numpy as np
from PIL import Image
def sample_frames(video_path, n_frames=17, resize=None):
cap = cv2.VideoCapture(video_path)
total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
indices = np.linspace(0, total - 1, n_frames).astype(int)
indices_set = set(indices)
frames = []
for i in range(total):
grabbed = cap.grab()
if not grabbed:
break
if i in indices_set:
ret, frame = cap.retrieve()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
if resize:
img = img.resize([int(d * resize) for d in img.size])
frames.append(img)
cap.release()
return frames
cv2.VideoCapture, read total frame countnp.linspace(0, total-1, N)grab() (fast, no decode)retrieve() only for selected indices (decodes the frame)grab() without retrieve() skips decoding — essential for long videoscap.set(cv2.CAP_PROP_POS_FRAMES, i) is unreliable for some codecs; grab/retrieve is more robustdata-ai
Scaled Pinball Loss (SPL) metric for evaluating quantile forecasts, normalized by mean absolute successive differences of training data
data-ai
Walk backward through a time series and multiplicatively rescale segments when jumps exceed a fraction of the running mean to correct data collection anomalies
testing
Transform forecasting target to next/current ratio minus one so that optimizing MAE or squared error implicitly minimizes SMAPE
tools
Convert point forecasts to prediction intervals by scaling with logit-transformed quantile ratios passed through a Normal CDF