> ## Documentation Index
> Fetch the complete documentation index at: https://docs.vlm.run/llms.txt
> Use this file to discover all available pages before exploring further.

# Segmentation

> Create precise pixel-level segmentation masks for objects, regions, and features in images

Create precise pixel-level segmentation masks for objects, regions, and features in images. Perfect for medical imaging, autonomous driving, photo editing, and augmented reality applications.

<Frame caption="Segmented car mask visualized.">
  <img className="block dark:hidden" src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/car-segmentation.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=0fc17241051f3d5b782be9145904db99" width="640" height="480" data-path="agents/assets/car-segmentation.jpg" />
</Frame>

<Frame caption="Example detections of objects, people and faces.">
  <div style={{ display: 'flex', gap: '20px', justifyContent: 'center' }}>
    <div style={{ flex: 1 }}>
      <h4 style={{ textAlign: 'center', marginBottom: '10px' }}>Object Segmentation</h4>

      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/cars-segmentation.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=450dcceda835c306f0d63cc7bb8d36e8" alt="Segmentation of cars with their masks overlaid" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1024" height="576" data-path="agents/assets/cars-segmentation.jpg" />
    </div>

    <div style={{ flex: 1 }}>
      <h4 style={{ textAlign: 'center', marginBottom: '10px' }}>Face Segmentation</h4>

      <img src="https://mintcdn.com/autonomiai/n0UzFHKSRrWx1Yji/agents/assets/face-segmentation.jpg?fit=max&auto=format&n=n0UzFHKSRrWx1Yji&q=85&s=efa27d6e27af9769672f1d228394ab0c" alt="Segmentation of individual faces with their masks overlaid" style={{ width: '100%', border: '1px solid #ddd', borderRadius: '4px' }} width="1024" height="535" data-path="agents/assets/face-segmentation.jpg" />
    </div>
  </div>
</Frame>

## Usage Example

<Tip>
  For best results, we recommend using the [Structured Outputs API](/agents/structured-responses) to get responses in a structured and validated data format.
</Tip>

<CodeGroup>
  ```python Python theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  from vlmrun.client import VLMRun

  # Initialize the VLMRun client
  client = VLMRun(api_key="<VLMRUN_API_KEY>")

  # Segment objects in the image
  response = client.agent.completions.create(
      model="vlmrun-orion-1:auto",
      messages=[
          {
            "role": "user",
            "content": [
              {"type": "text", "text": "Segment all the cars in this image"},
              {"type": "image_url", "image_url": {"url": "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/image.object-detection/nascar.jpg", "detail": "auto"}}
            ]
          }
      ],
  )

  print(response.choices[0].message.content)
  ```

  ```python Python - Structured Outputs theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  from vlmrun.client import VLMRun
  from pydantic import BaseModel, Field

  # Define the response schema
  class SegmentationMask(BaseModel):
    label: str = Field(..., description="Name of the segmented object")
    mask_url: str = Field(..., description="Pre-signed URL to the PNG mask")

  class Segmentations(BaseModel):
    segmentations: list[SegmentationMask] = Field(..., description="List of segmentations")

  # Initialize the VLMRun client
  client = VLMRun(api_key="<VLMRUN_API_KEY>")

  # Segment objects with structured output
  response = client.agent.completions.create(
      model="vlmrun-orion-1:auto",
      messages=[
          {
            "role": "user",
            "content": [
              {"type": "text", "text": "Segment all the cars in this image"},
              {"type": "image_url", "image_url": {"url": "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/image.object-detection/nascar.jpg", "detail": "auto"}}
            ]
          }
      ],
      response_format={"type": "json_schema", "schema": Segmentations.model_json_schema()},
  )

  # Validate the response
  result = Segmentations.model_validate_json(response.choices[0].message.content)
  # >>> Segmentations(segmentations=[SegmentationMask(label="car", mask_url="https://..."), ...])
  ```

  ```typescript Node.js theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  import { VlmRun } from "vlmrun";

  const client = new VlmRun({
    apiKey: "<VLMRUN_API_KEY>",
    baseURL: "https://api.vlm.run/v1"
  });

  const response = await client.agent.completions.create({
    model: "vlmrun-orion-1:auto",
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "Segment all the cars in this image" },
          { type: "image_url", image_url: { url: "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/image.object-detection/nascar.jpg", detail: "auto" } }
        ]
      }
    ]
  });

  console.log(response.choices[0].message.content);
  ```

  ```typescript Node.js - Structured Outputs [expandable] theme={"theme":{"light":"github-light","dark":"dark-plus"}}
  import { VlmRun } from "vlmrun";
  import { z } from "zod";
  import { zodToJsonSchema } from "zod-to-json-schema";

  // Define the response schema with Zod
  const SegmentationsSchema = z.object({
    segmentations: z.array(z.object({
      label: z.string().describe("Name of the segmented object"),
      mask_url: z.string().describe("Pre-signed URL to the PNG mask")
    })).describe("List of segmentations")
  });

  // Initialize the VLMRun client
  const client = new VlmRun({
    apiKey: "<VLMRUN_API_KEY>",
    baseURL: "https://api.vlm.run/v1"
  });

  // Segment objects with structured output
  const response = await client.agent.completions.create({
    model: "vlmrun-orion-1:auto",
    messages: [
      {
        role: "user",
        content: [
          { type: "text", text: "Segment all the cars in this image" },
          { type: "image_url", image_url: { url: "https://storage.googleapis.com/vlm-data-public-prod/hub/examples/image.object-detection/nascar.jpg", detail: "auto" } }
        ]
      }
    ],
    response_format: {
      type: "json_schema",
      schema: zodToJsonSchema(SegmentationsSchema)
    }
  });

  const result = SegmentationsSchema.parse(JSON.parse(response.choices[0].message.content));
  ```
</CodeGroup>

## FAQ

<AccordionGroup>
  <Accordion title="What different types of segmentation are supported?" icon="layer-group">
    * **Instance Segmentation**: Segment individual objects with unique masks
    * **Semantic Segmentation**: Classify pixels by category or class
    * **Panoptic Segmentation**: Combine instance and semantic segmentation
  </Accordion>

  <Accordion title="What format do the segmentation masks come in?" icon="draw-polygon">
    The segmentation masks come in the format of a list of objects with their masks. The masks are in PNG format that can be retrieved as a pre-signed URL, per object instance.
  </Accordion>

  <Accordion title="What types of objects and categories can be segmented?" icon="person">
    Orion supports promptable segmentation, where you can describe in natural language the object(s) you want to segment (e.g., "segment the writing-related items in this image"). For best results, clearly describe the object(s) of interest. Below are some common examples of classes that can be segmented:

    <b>Common Objects</b>

    <ul>
      <li><b>People</b>: person, face, hand, foot</li>
      <li><b>Vehicles</b>: car, truck, bus, motorcycle, bicycle</li>
      <li><b>Animals</b>: dog, cat, bird, horse, cow, sheep</li>
      <li><b>Furniture</b>: chair, table, bed, sofa, desk</li>
      <li><b>Electronics</b>: laptop, phone, tv, keyboard, mouse</li>
    </ul>

    <b>Specialized Categories</b>

    <ul>
      <li><b>Medical</b>: organ, tissue, lesion, bone</li>
      <li><b>Nature</b>: tree, grass, sky, water, mountain</li>
      <li><b>Indoor</b>: wall, floor, ceiling, door, window</li>
      <li><b>Outdoor</b>: road, sidewalk, building, sign, traffic\_light</li>
    </ul>
  </Accordion>

  <Accordion title="What mask formats are supported?" icon="file-image">
    <b>PNG Masks</b>

    <ul>
      <li>Binary or grayscale images where each pixel value represents a segment ID</li>
      <li>Compatible with most image editing software</li>
      <li>Small file size for simple segmentations</li>
    </ul>

    <b>Other Formats</b> (Coming soon!): JSON Polygons and COCO Format
  </Accordion>
</AccordionGroup>
