Uploading Geometry Annotations

This notebook shows how to:

Configuration

[ ]:
%pip install git+https://github.com/SonanceAI/datamint-python-api

If you haven’t already, setup your api key:

[ ]:
# !datamint config --api-key MY_API_KEY

Setup-up example Project and data(optional)

creating project

[ ]:
from datamint.examples import ProjectMR

PROJECT_NAME = 'Example Project MR'

ProjectMR.create(project_name=PROJECT_NAME)
'99a99f48-cc2a-4eb3-a729-2bcaf3288f44'

Go to the web app, select your project, and navigate to ‘Project Settings’. In ‘Shapes and regions’, add a new line shape named ‘Line1’:

image.png

We will be adding editing Project Settings via Python API soon.

Creating a Line annotation

Reference: AnnotationsApi.add_line_annotation

[ ]:
from datamint import Api

api = Api()
proj = api.projects.get_by_name(PROJECT_NAME)
[ ]:
res = api.resources.get_list(project_name=PROJECT_NAME)[0]  # get the first resource
api.annotations.add_line_annotation((0, 0), (10, 30),  # a line starting at (0, 0) and ending at (10, 30), x and y coordinates respectively
                                    resource=res,
                                    identifier='Line1',
                                    frame_index=0,  # The first frame
                                    worklist_id=proj.fetch_worklists()[0].id
                                    )

The result of the above step can be seen in the web app by selecting the resource in the Files tab:

image.png

Creating a Box annotation

Reference: AnnotationsApi.add_box_annotation

First, add a new box/rectangle shape named ‘BoundingBox1’ in your project settings (similar to the line shape setup above).

[ ]:
# Add a box annotation with diagonal corners at (5, 5) and (25, 20)
api.annotations.add_box_annotation((5, 5), (25, 20),  # box from top-left (5,5) to bottom-right (25,20)
                                   resource=res,
                                   identifier='BoundingBox1',
                                   frame_index=0,  # The first frame
                                   worklist_id=proj.fetch_worklists()[0].id)

Both line and box annotations support:

  • DICOM coordinate conversion: Automatically convert pixel coordinates to patient coordinates

  • Patient coordinates: Direct specification of 3D patient coordinates

  • Model annotations: Associate annotations with specific AI models

Example with DICOM metadata:

# Assuming you have DICOM metadata
import pydicom

dicom_metadata = pydicom.dcmread("/path/to/your/dicom/file.dcm")
api.annotations.add_box_annotation((10, 10), (50, 50),
                                   resource=res,
                                   identifier='DicomBox',
                                   frame_index=0,
                                   metadata=dicom_metadata,  # Required when coords_system='patient'
                                   coords_system='patient',  # Automatically converts to patient coordinates
                                   worklist_id=proj.fetch_worklists()[0].id)