> ## Documentation Index
> Fetch the complete documentation index at: https://api-reference.scale.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add Segmentation Annotations

> [!NOTE]
>
> **[Click for Python SDK](https://docs.nucleus.scale.com/en/latest/api/nucleus/index.html#nucleus.Dataset.annotate)**
>
> ```
> Dataset.annotate(
>     annotations: List[SegmentationAnnotation],
>     update: bool,
>     asynchronous: bool
> ) -> Union[dict, AsyncJob]
> ```

When uploading a mask annotation, Nucleus expects the mask file to be in PNG format. Currently, Nucleus only supports uploading masks from URL (local upload coming soon). For each mask, specify the mask\_url, as well as the `DatasetItem` you are annotating. A `DatasetItem` can be referenced by either its internal ID, or the reference ID you provided when uploading the image.

Nucleus automatically enforces the constraint that each `DatasetItem` can have at most one ground truth segmentation mask. As a consequence, if during upload a duplicate mask is detected for a given image, by default it will be ignored. You can change this behavior by setting the optional `update` boolean to `true`, which will replace the existing segmentation with the new mask specified in the request body.


## OpenAPI

````yaml openapi/nucleus.json POST /dataset/{dataset_id}/annotate_segmentation
openapi: 3.1.0
info:
  title: Nucleus API
  version: '1.1'
servers:
  - url: https://api.scale.com/v1/nucleus
security:
  - sec0: []
paths:
  /dataset/{dataset_id}/annotate_segmentation:
    post:
      summary: Add Segmentation Annotations
      operationId: add-segmentation-annotations
      parameters:
        - name: dataset_id
          in: path
          description: The Scale-generated ID of the Dataset.
          schema:
            type: string
          required: true
      requestBody:
        content:
          application/json:
            schema:
              type: object
              required:
                - segmentations
              properties:
                segmentations:
                  type: array
                  description: An array of `SegmentationAnnotation` objects.
                  items:
                    properties:
                      reference_id:
                        type: string
                        description: >-
                          User-specified ID of the image to which to apply this
                          Annotation.
                      annotation_id:
                        type: string
                        description: >-
                          Optional unique ID that uniquely identifies this
                          Annotation within its target DatasetItem. Upon ingest,
                          a matching annotation ID will be ignored by default,
                          and updated if update=True for dataset.annotate. If
                          not supplied, an annotation_id will be generated
                          automatically.
                      mask_url:
                        type: string
                        description: >-
                          URL pointing to the segmentation prediction mask which
                          is accessible to Scale. The mask is an HxW int8 array
                          saved in PNG format, with each pixel value ranging
                          from [0, N), where N is the number of possible classes
                          (for semantic segmentation) or instances (for instance
                          segmentation). The height and width of the mask must
                          be the same as the original image. The class name for
                          each value in the mask is stored in the list of
                          Segment objects passed for `annotations`.
                      annotations:
                        type: array
                        description: >-
                          Array of `Segments`, which represent mappings between
                          the integer values contained in `mask_url` and class
                          labels.
                        default: []
                      type:
                        type: string
                        description: For segmentation, this `type` will always be `mask`.
                        default: mask
                    required:
                      - reference_id
                      - mask_url
                      - annotations
                      - type
                    type: object
                update:
                  type: boolean
                  description: >-
                    Whether to overwrite existing annotation masks upon
                    `annotation_id` and `reference_id` collision.
                  default: false
      responses:
        '200':
          description: '200'
          content:
            application/json:
              examples:
                Result:
                  value:
                    dataset_id: ds_bw6de8s84pe0vbn6p5zg
                    annotations_processed: 2
                    annotations_ignored: 0
                    annotations_errored: 0
              schema:
                type: object
                properties:
                  dataset_id:
                    type: string
                    example: ds_bw6de8s84pe0vbn6p5zg
                  annotations_processed:
                    type: integer
                    example: 2
                    default: 0
                  annotations_ignored:
                    type: integer
                    example: 0
                    default: 0
                  annotations_errored:
                    type: integer
                    example: 0
                    default: 0
        '400':
          description: '400'
          content:
            application/json:
              examples:
                Result:
                  value: {}
              schema:
                type: object
                properties: {}
      deprecated: false
components:
  securitySchemes:
    sec0:
      type: http
      scheme: basic

````