from grid.model.perception.segmentation.sam2 import SAM2
car = AirGenCar()

# We will be capturing an image from the AirGen simulator 
# and run model inference on it.

img =  car.getImage("front_center", "rgb").data
prompts = np.array([[616, 208]])
labels = np.array([1])

model = SAM2(use_local = False)
result = model.run(rgbimage=img, prompts, labels)
print(result.shape)

The SAM2 class implements a wrapper for the Segment Anything 2.1 (SAM2) model, which segments objects in RGB images and videos based on input prompts.

class SAM2()
use_local
boolean
default:
"False"

If True, inference call is run on the local VM, else offloaded onto GRID-Cortex. Defaults to False.

def run()
rgbimage
np.ndarray
required

The input RGB image of shape (M,N,3)(M, N, 3).

prompts
np.ndarray
required

Points in the form of coordinates of shape (x,y)(x, y) to segment.

input_labels
np.ndarray
required

The labels of the points to segment.

multimask_output
boolean
default:
"False"

If True, returns a list of masks of shape (M,N)(M,N). Defaults to False. The mutlimask feature is only available for local inference.

Returns
np.ndarray

The predicted output.

from grid.model.perception.segmentation.sam2 import SAM2
car = AirGenCar()

# We will be capturing an image from the AirGen simulator 
# and run model inference on it.

img =  car.getImage("front_center", "rgb").data
prompts = np.array([[616, 208]])
labels = np.array([1])

model = SAM2(use_local = False)
result = model.run(rgbimage=img, prompts, labels)
print(result.shape)

This code is licensed under the Apache 2.0, and BSD-3 License.

Was this page helpful?