device = torch.device(cfg.get("device", "cpu")) model.to(device)
import argparse import cv2 from pathlib import Path from run_inference import infer, model, preprocess, cfg # reuse the functions above camshowrecordings/model/sam_samantha/5
fps = cap.get(cv2.CAP_PROP_FPS) w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) device = torch
# ------------------------------------------------------------------ # 2️⃣ Helper: build the model (adjust to your repo's factory function) # ------------------------------------------------------------------ def build_model(cfg): """ Replace the body of this function with the actual model‑building logic from camshowrecordings. """ from camshowrecordings.models.sam import SamSamantha # Example import model = SamSamantha( backbone=cfg["model"]["backbone"], img_size=cfg["model"]["image_size"], num_classes=cfg["model"]["num_classes"] ) return model 7️⃣ Using the Model on a Whole Video
# ------------------------------------------------------------------ # 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
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.
# ------------------------------------------------------------------ # 6️⃣ Demo on a sample image # ------------------------------------------------------------------ if __name__ == "__main__": import argparse