Deploy Segment Anything to GCP Compute Engine
Set up a Compute Engine VM and run Segment Anything with Roboflow Inference, on its own or inside a Workflow.
Overview
Segment Anything draws a precise mask around the object at a point or inside a box you give it. The code below uses SAM 2, the current version; to segment every object of a class from a text prompt, see the SAM 3 guides. To deploy Segment Anything to GCP Compute Engine, you will:
- Set up a Compute Engine VM and start the Inference Server
- Run Segment Anything on an image
- Run Segment Anything in a Workflow
- Deploy with a coding agent
Step 1: Set Up a Compute Engine VM
The quickest route is the Inference CLI's cloud command. It uses SkyPilot (opens in a new tab) to launch a GPU instance in your Google Cloud account with the Inference Server already running. Connect SkyPilot to your Google Cloud account first (SkyPilot's cloud setup (opens in a new tab)), then run this on your own computer:
pip install inference "skypilot[gcp]"
inference cloud deploy --provider gcp --compute-type gpuWhen it finishes, it prints the server's address, such as http://34.66.116.66:9001. Use that address as api_url in the code on this page. inference cloud status lists your deployments, and inference cloud undeploy shuts one down so it stops billing.
To use an instance you manage yourself instead, launch a Compute Engine VM with an NVIDIA T4 or L4 GPU, and install the NVIDIA driver, Docker and the NVIDIA Container Toolkit (opens in a new tab) on it, then SSH in and start the server on all of the instance's network interfaces:
pip install inference-cli
inference server start --bind-address 0.0.0.0Then allow port 9001 from your own IP address only, with a firewall rule that targets a network tag you add to the VM: gcloud compute firewall-rules create allow-inference --allow=tcp:9001 --source-ranges=YOUR_IP/32 --target-tags=inference. The server has no authentication by default, so never open the port to the whole internet (securing a self-hosted server (opens in a new tab)).
Step 2: Run Segment Anything on an Image
pip install inference-sdkSet the ROBOFLOW_API_KEY environment variable to your Roboflow API key (opens in a new tab) first. Run the code on your own computer with api_url set to your server's address, or on the server itself with http://localhost:9001. Then run:
import os
from inference_sdk import InferenceHTTPClient
client = InferenceHTTPClient(
api_url="http://YOUR_SERVER_IP:9001",
api_key=os.environ["ROBOFLOW_API_KEY"],
)
result = client.sam2_segment_image(
"image.jpg",
prompts=[{"points": [{"x": 380, "y": 350, "positive": True}]}],
sam2_version_id="hiera_tiny",
)
print(result)Each point marks the object you want ("positive": True) or a region to leave out (False). The first request downloads the model weights, so it takes longer than the ones after. See the Segment Anything docs (opens in a new tab) for every option.
Step 3: Run a Workflow
A Workflow chains models with the logic around them, such as counting, tracking, filtering by zone or sending results to your own systems, and runs on your Compute Engine VM like a single model does. Build one in the Roboflow app with the Segment Anything block (opens in a new tab), then run it by its workspace and Workflow ID:
import os
from inference_sdk import InferenceHTTPClient
client = InferenceHTTPClient(
api_url="http://YOUR_SERVER_IP:9001",
api_key=os.environ["ROBOFLOW_API_KEY"],
)
result = client.run_workflow(
workspace_name="YOUR_WORKSPACE",
workflow_id="YOUR_WORKFLOW_ID",
images={"image": "image.jpg"},
)
print(result[0])The result has one entry per image, holding every output the Workflow defines. To run the Workflow on video, pass workflow and workspace to client.webrtc.stream() in place of model_id (video with Workflows (opens in a new tab)). See Deploy a Workflow (opens in a new tab) for the other ways to run one.
Step 4: Deploy with a Coding Agent
The Roboflow MCP server (opens in a new tab) gives a coding agent such as Claude Code or Cursor access to your Roboflow workspace. It can list your models, run one on a test image, build and run a Workflow, and write the deployment code for your Compute Engine VM. Add it to Claude Code:
claude mcp add -s user roboflow --transport http https://mcp.roboflow.com/mcpThe first time the agent uses it, you sign in to Roboflow in your browser. In Cursor, add https://mcp.roboflow.com/mcp under mcpServers in ~/.cursor/mcp.json. Then ask the agent, for example: "Deploy my Segment Anything model to GCP Compute Engine with Roboflow Inference and write a script that runs it on my camera stream."