# Visualise side‑by‑side overlay = cv2.addWeighted(img, 0.7, cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR), 0.3, 0) cv2.imshow("Original", img) cv2.imshow("Mask", mask) cv2.imshow("Overlay", overlay) cv2.waitKey(0) cv2.destroyAllWindows()
model: name: sam_samantha version: 5 backbone: vit_h image_size: 1024 num_classes: 1 # Usually segmentation → binary mask preprocess: normalize: true mean: [0.485, 0.456, 0.406] std: [0.229, 0.224, 0.225] device: cuda Below is a minimal, self‑contained script that loads the model and runs a single inference on a video frame. camshowrecordings/model/sam_samantha/5
python run_inference.py path/to/your/frame.jpg If you have a GPU, make sure torch.cuda.is_available() returns True . The script will automatically use the device defined in config.yaml . 7️⃣ Using the Model on a Whole Video Below is a compact example that reads a video file, runs the model on every N ‑th frame, and writes an output video with the segmentation overlay. # Visualise side‑by‑side overlay = cv2
def process_video(in_path: Path, out_path: Path, stride: int = 5): cap = cv2.VideoCapture(str(in_path)) if not cap.isOpened(): raise RuntimeError(f"Cannot open in_path") 7️⃣ Using the Model on a Whole Video
# 3️⃣ Install dependencies pip install -r requirements.txt If the repo is private, make sure you have the right SSH key or token. 5️⃣ Inspecting the Model Files Navigate to the model folder:
if frame_idx % stride == 0: mask = infer(frame) # binary mask (0/255) overlay = cv2.addWeighted(frame, 0.7, cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR), 0.3, 0) out.write(overlay) else: out.write(frame) # write raw frame for non‑processed indices