Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Segment image with SAM-2 given point/box prompts
[x, y]
from grid_cortex_client import CortexClient, ModelType import numpy as np from PIL import Image client = CortexClient() image = np.array(Image.open("cat.jpg")) mask = client.run( ModelType.SAM2, image_input=image, prompts=[[320, 240]], labels=[1], ) print(mask.shape) # (480, 640)
from grid_cortex_client import CortexClient import numpy as np from PIL import Image client = CortexClient() img = Image.open("scene.jpg") # 640x480 RGB mask = client.run( model_id="sam2", image_input=img, prompts=[[320, 350]], labels=[1], # 1 = foreground ) print(mask.shape, mask.dtype) # (480, 640) uint8 print(np.unique(mask)) # [0, 255] foreground = np.count_nonzero(mask) print(f"foreground pixels: {foreground} ({foreground / mask.size * 100:.1f}%)") # foreground pixels: 138532 (45.1%)
Was this page helpful?