Views
No views yet
pip install mlx-vlm1from PIL import Image
2from mlx_vlm.utils import load_model, get_model_path
3from mlx_vlm.models.sam3.generate import Sam3Predictor
4from mlx_vlm.models.sam3.processing_sam3 import Sam3Processor
5
6model_path = get_model_path("mlx-community/sam3-mxfp8")
7model = load_model(model_path)
8processor = Sam3Processor.from_pretrained(str(model_path))
9predictor = Sam3Predictor(model, processor, score_threshold=0.3)1image = Image.open("photo.jpg")
2result = predictor.predict(image, text_prompt="a dog")
3
4for i in range(len(result.scores)):
5 x1, y1, x2, y2 = result.boxes[i]
6 print(f"[{result.scores[i]:.2f}] box=({x1:.0f}, {y1:.0f}, {x2:.0f}, {y2:.0f})")1result = predictor.predict(image, text_prompt="a person")
2
3# result.boxes -> (N, 4) xyxy bounding boxes
4# result.masks -> (N, H, W) binary segmentation masks
5# result.scores -> (N,) confidence scores
6
7import numpy as np
8overlay = np.array(image).copy()
9W, H = image.size
10for i in range(len(result.scores)):
11 mask = result.masks[i]
12 if mask.shape != (H, W):
13 mask = np.array(Image.fromarray(mask.astype(np.float32)).resize((W, H)))
14 binary = mask > 0
15 overlay[binary] = (overlay[binary] * 0.5 + np.array([255, 0, 0]) * 0.5).astype(np.uint8)1import numpy as np
2boxes = np.array([[100, 50, 400, 350]]) # xyxy pixel coords
3result = predictor.predict(image, text_prompt="a cat", boxes=boxes)1import mlx.core as mx
2
3inputs = processor.preprocess_image(image)
4text_inputs = processor.preprocess_text("a cat")
5outputs = model.detect(
6 mx.array(inputs["pixel_values"]),
7 mx.array(text_inputs["input_ids"]),
8 mx.array(text_inputs["attention_mask"]),
9)
10mx.eval(outputs)
11
12pred_masks = outputs["pred_masks"] # (B, 200, 288, 288) instance masks
13semantic_seg = outputs["semantic_seg"] # (B, 1, 288, 288) semantic segmentationpython -m mlx_vlm.models.sam3.track_video --video input.mp4 --prompt "a car" --model mlx-community/sam3-mxfp8| Flag | Default | Description |
|---|---|---|
--video | (required) | Input video path |
--prompt | (required) | Text prompt |
--output | <input>_tracked.mp4 | Output video path |
--model | facebook/sam3 | Model path or HF repo |
--threshold | 0.15 | Score threshold |
--every | 2 | Detect every N frames |