NewTemplates and Tutorials for Evaluating Agentic AI Traces

How to build a labeling tool for SAM2 interactive precision segmentation with prompt refinement in label studio

Bootstrapping datasets for SAM2 interactive precision segmentation with prompt refinement in Label Studio requires balancing automation with human intervention.

Annotators must iteratively refine machine-generated boundaries using exact point and box prompts.

Configure interactive smart tags to map positive and negative point prompts directly to raster boundaries.

Align label values exactly across keypoint, rectangle, and brush controls so the machine learning backend routes prompt data correctly.

Export finished annotations as PNG or NumPy arrays rather than polygon-based formats to preserve exact pixels.

Provide annotators with hotkey instructions for toggling prompt polarity and zooming to reduce manual fatigue.

The problem

Labeling for SAM2 interactive precision segmentation with prompt refinement in Label Studio demands massive effort when annotators draw every polygon boundary by hand across high-resolution image datasets. Annotators face extreme fatigue from constant zooming, panning, and tracing complex object borders. Enterprise perception teams also face strict compliance constraints around internal data storage, meaning you cannot push sensitive source media to public platforms. Building an internal tool from scratch to handle cloud storage permissions, interactive inference, and complex interfaces costs months of engineering time and delays model training.

The short answer

You will use Label Studio as the foundation for your workflow, and a coding agent generates the exact labeling interface you need. The agent uses two tools together: the XML labeling config builder skill to generate optimized interface configurations from a plain-language spec, and the Label Studio SDK/CLI to wire the config into a real project programmatically. Rather than building a new labeling application from scratch, agents generate the interface from your spec and deploy it into Label Studio in one pass.

Docs: LLM-friendly docs (markdown) → https://labelstud.io/llms.txt

Docs: SAM2 image backend examples → https://github.com/HumanSignal/label-studio-ml-backend/tree/master/label_studio_ml/examples/segment_anything_2_image

Docs: Segment Anything Model tutorial → https://labelstud.io/guide/ml_tutorials/segment_anything_model

What you're building

Provide an image and video viewing canvas that supports deep zooming and panning for examining fine object boundaries.

Include interactive keypoint and rectangle controls that act as positive and negative prompts for the underlying segmentation model.

Display a brush mask output layer that shows model inferences and allows manual touch-ups on misaligned pixel edges.

Maintain a unified label palette that synchronizes class categories across your points, boxes, and brush masks.

Show a dynamic outliner region list that tracks separate object instances and confidence scores for each generated shape.

Support keyboard shortcuts that let annotators swap between inclusion and exclusion prompts without moving their cursor.

How to build it in Label Studio

1. Set up the project

Start by deploying a self-hosted instance of Label Studio to ensure internal media assets remain within your secure environment. A single task for SAM2 interactive precision segmentation with prompt refinement in Label Studio consists of an image URL or video file path mapped to your internal storage buckets. You must configure the storage connectors to grant read-only permissions and sync the object pointers into the project alongside any pre-loaded label hierarchy files. Include metadata fields like camera ID, sequence timestamp, or collection batch in your task JSON so annotators can filter queues effectively.

2. Generate the labeling interface with the XML config skill

Direct your coding agent to process the feature spec and run the XML labeling config builder skill. Command the agent to output a validated Label Studio XML configuration that structures the user interface. Instruct it to bind interactive control tags to your visual data inputs and enforce the strict label value matching required for model inference.

<Image name="image" value="$image" zoom="true" zoomControl="true"> - displays the source visual data and provides panning and zooming capabilities.

<KeyPointLabels name="kp" toName="image" smart="true"> - captures interactive click prompts that tell the model which specific regions to include or exclude.

<RectangleLabels name="roi" toName="image" smart="true"> - captures bounding box prompts that constrain the model inference to a targeted region of interest.

<BrushLabels name="mask" toName="image"> - displays the raster mask output from the model and allows the annotator to paint manual touch-ups.

<VideoRectangle name="box" toName="video" smart="true"> - tracks object boundaries across sequential video frames for temporal datasets.

3. Wire it into a project with the SDK

Instruct the agent to use the Label Studio SDK/CLI to create the project with the generated config and upload your tasks. Tell the agent to import model predictions as pre-annotations by embedding mask coordinates under the predictions key of your task JSON. You can iterate on the config continuously using the same agent loop. Run a small batch, watch annotators struggle with prompt alignment, ask the agent to regenerate the XML with updated label values, and redeploy the project.

4. Set up review and quality workflows

Quality control for SAM2 interactive precision segmentation with prompt refinement in Label Studio relies on geometric agreement rather than simple classification matching. Configure the project to assign multiple annotators to a percentage of tasks and measure Intersection over Union (IoU) for the resulting brush masks. Route tasks that fall below your target agreement threshold into a dedicated reviewer queue. Reviewers can then inspect the mismatched masks, adjust the boundary points, and accept the definitive version.

5. Export and integrate

With Label Studio, you can export the raw annotations in JSON format by default, but brush masks require specific handling. You will extract the raster masks as PNG files or NumPy 2D arrays to preserve the precise pixel boundaries. Downstream pipelines ingest these arrays alongside the original JSON metadata, including confidence scores and model versions, to train instance segmentation models or feed human-in-the-loop robotics systems.

Why Label Studio for SAM2 interactive precision segmentation with prompt refinement in Label Studio

You can stream positive and negative keypoint clicks directly to the model through native machine learning backend connections to reduce manual tracing fatigue.

You can translate model-generated shapes into editable raster layers with brush mask outputs so annotators can fix minor edge errors without redrawing the entire object.

You can sync source media directly from internal buckets using cloud storage connectors to satisfy strict data compliance constraints.

Annotators can swap between inclusion and exclusion points rapidly using configurable hotkeys to speed up the prompt refinement process.

Review teams can prioritize the hardest examples by using Intersection over Union (IoU) agreement metrics to automatically identify poorly segmented boundaries.

Common variations

Annotators follow moving entities across sequential frames using bounding box prompts for single-object video tracking.

Teams annotate DICOM scans instead of natural images using brush tools for precise tumor delineation in medical image segmentation.

Annotators rely entirely on smart rectangle prompts to generate bounding boxes for zero-shot object detection without brush touch-ups.

Reviewers accept or reject final results from pre-computed masks loaded as predictions for automated mask generation workflows.

Next steps

XML labeling config builder skill → https://github.com/HumanSignal/create-xml-labeling-config-skill

Label Studio SDK/CLI → https://api.labelstud.io/api-reference/introduction/getting-started

LLM-friendly docs (markdown) → https://labelstud.io/llms.txt

SAM2 image backend examples → https://github.com/HumanSignal/label-studio-ml-backend/tree/master/label_studio_ml/examples/segment_anything_2_image

Segment Anything Model tutorial → https://labelstud.io/guide/ml_tutorials/segment_anything_model

GitHub → https://github.com/HumanSignal/label-studio

How do you configure cloud storage permissions for proprietary image datasets?

Map your Label Studio instance to Amazon S3 or Google Cloud Storage using read-only access policies. The platform syncs object pointers via the storage API to prevent duplication and respect your internal data retention mandates. This architecture keeps sensitive media inside your perimeter while the SAM2 backend streams the assets directly for inference.

Why must label values match across point, box, and brush tags?

The SAM2 machine learning backend requires strict string matching to route your prompts correctly. If you assign the value "object" to your KeyPointLabels tag, you must use the exact same value in your RectangleLabels and BrushLabels tags. This configuration ensures the model translates your clicks into the correct raster mask class.

How do annotators toggle between positive and negative point prompts?

Reviewers press the Alt key while clicking the canvas to switch the keypoint polarity from inclusion to exclusion. You should also instruct teams to disable the auto-accept feature in the Label Studio interface. Keeping this setting off leaves the prompts live so annotators can iteratively refine the mask edges before finalizing the shape.

What is the optimal export format for prompt-generated brush masks?

Export your finalized BrushLabels data as PNG files or NumPy 2D arrays. Downstream data engineering pipelines ingest these exact pixel matrices much more reliably than polygon coordinates. You should avoid standard bounding box formats like YOLO because they discard the fine edge details the SAM2 model generates.

Can you generate full video segmentation masks with the official SAM2 backend?

The current Label Studio SAM2 video backend limits inference to single-object tracking using bounding boxes. You map the VideoRectangle tag to your video source to follow an entity across sequential frames. The official integration does not output rasterized video segmentation masks at this time.

Related Content