# 2️⃣ Create & activate a virtual env (optional but recommended) python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate
device = torch.device(cfg.get("device", "cpu")) model.to(device) camshowrecordings/model/sam_samantha/5
import argparse import cv2 from pathlib import Path from run_inference import infer, model, preprocess, cfg # reuse the functions above # 2️⃣ Create & activate a virtual env
frame_idx = 0 while True: ret, frame = cap.read() if not ret: break camshowrecordings/model/sam_samantha/5
# Convert BGR → RGB img_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
# ------------------------------------------------------------------ # 5️⃣ Run inference # ------------------------------------------------------------------ def infer(frame: np.ndarray): x = preprocess(frame, cfg) with torch.no_grad(): # The exact call depends on the model; many SAM‑style models return a mask mask = model(x) # → (B, 1, H, W) logits or probabilities # Post‑process: convert logits → binary mask mask = torch.sigmoid(mask) > 0.5 mask_np = mask.squeeze().cpu().numpy().astype(np.uint8) * 255 return mask_np
# Normalize img_norm = img_rgb.astype(np.float32) / 255.0 mean = np.array(cfg["preprocess"]["mean"]) std = np.array(cfg["preprocess"]["std"]) img_norm = (img_norm - mean) / std