For manual labeling or specific use cases, you can use get_model to access the model directly and provide bounding box prompts.
Bounding Box Format Requirements:
When passing prompts to predictor.predict, the bounding boxes must follow the format (x1, x2, y1, y2), where (x1, y1) is the lower-left corner and (x2, y2) is the upper-right corner.
Workflow:
- Get the model via
get_model(). - Preprocess the image using
format_image_shape, normalize_image, and appropriate numpy transpositions/expansions. - Call
predictor.predict(im, boxes_per_heatmap=[[[x1, y1, x2, y2]]]).
Note: The boxes_per_heatmap argument expects a nested list structure containing the coordinates.
import numpy as np
from cellSAM import get_model
from cellSAM.utils import format_image_shape, normalize_image
# 1. Get model
predictor = get_model()
# 2. Prepare image (example coordinates: x1, y1, w, h)
x1, y1, w, h = 290, 365, 60, 38
x2, y2 = x1 + w, y1 + h
im = format_image_shape(img)
im = normalize_image(im)
im = np.transpose(im, (2, 0, 1))
im = np.expand_dims(im, axis=0)
# 3. Predict with bounding box prompt (x1, y1, x2, y2)
pred_mask, *_ = predictor.predict(im, boxes_per_heatmap=[[[x1, y1, x2, y2]]])