Developer guide for importing annotations on DICOM data and sample import formats.
Overview
To import annotations in Labelbox, you need to create an annotations payload. In this section, we provide this payload for every supported annotation type.
Annotation payload types
Labelbox supports two formats for the annotations payload:
- Python annotation types (recommended)
- Provides a seamless transition between third-party platforms, machine learning pipelines, and Labelbox.
- Allows you to build annotations locally with local file paths, numpy arrays, or URLs
- Easily convert Python Annotation Type format to NDJSON format to quickly import annotations to Labelbox
- Supports one-level nested classification (radio, checklist, or free-form text) under a tool or classification annotation.
- JSON
- Skips formatting annotation payload in the Labelbox Python annotation type
- Supports any levels of nested classification (radio, checklist, or free-form text) under a tool or classification annotation.
Label import types
Labelbox additionally supports two types of label imports:
- Model-assisted labeling (MAL)
- This workflow allows you to import computer-generated predictions (or simply annotations created outside of Labelbox) as pre-labels on an asset.
- Ground truth
- This workflow functionality allows you to bulk import your ground truth annotations from an external or third-party labeling system into Labelbox Annotate. Using the label import API to import external data is a useful way to consolidate and migrate all annotations into Labelbox as a single source of truth.
Supported annotations
The following annotations are supported for a HTML data row:
- Polyline
- Segmentation masks
Tools
Polyline
polyline_annotation = [
lb_types.DICOMObjectAnnotation(
name="line_dicom",
group_key=lb_types.GroupKey.AXIAL,
frame=1,
value=lb_types.Line(points=[
lb_types.Point(x=10, y=10),
lb_types.Point(x=200, y=20),
lb_types.Point(x=250, y=250)
]),
segment_index=0,
keyframe=True,
),
lb_types.DICOMObjectAnnotation(
name="line_dicom",
group_key=lb_types.GroupKey.AXIAL,
frame=20,
value=lb_types.Line(points=[
lb_types.Point(x=10, y=10),
lb_types.Point(x=200, y=10),
lb_types.Point(x=300, y=300)
]),
segment_index=1,
keyframe=True,
),
]
polyline_annotation_ndjson = {
'name': 'line_dicom',
'groupKey': 'axial', # should be 'axial', 'sagittal', or 'coronal'
'segments': [
{
'keyframes': [{
'frame': 1,
'line': [
{'x': 10, 'y': 10},
{'x': 200, 'y': 20},
{'x': 250, 'y': 250},
]
}]},
{
'keyframes' : [{
'frame': 20,
'line': [
{'x': 10, 'y': 10},
{'x': 200, 'y': 10},
{'x': 300, 'y': 300},
]
}]}
],
}
Segmentation masks
MaskData
is mask data declared as a uint8 array of [H, W, 3]. You can also convert a polygon annotation or a 2D array to MaskData
. You can also specify a URL to a cloud-hosted mask (can be hosted on any cloud provider).
Masks cannot be larger than 9,000 in height or 9,000 pixels in width; otherwise, they will not be imported.
mask_annotation = [
lb_types.DICOMMaskAnnotation(
group_key='axial',
frames=[
lb_types.MaskFrame(
index=1,
instance_uri="https://storage.googleapis.com/labelbox-datasets/dicom-sample-data/sample-mask-1.png"
),
lb_types.MaskFrame(
index=5,
instance_uri="https://storage.googleapis.com/labelbox-datasets/dicom-sample-data/sample-mask-1.png"
)
],
instances=[
lb_types.MaskInstance(
color_rgb=(255, 255, 255),
name="segmentation_mask_dicom"
)
])
]
mask_annotation_ndjson = {
'groupKey': 'axial',
'masks': {
'frames': [{
'index': 1,
'instanceURI': "https://storage.googleapis.com/labelbox-datasets/dicom-sample-data/sample-mask-1.png"
}, {
'index': 5,
'instanceURI': "https://storage.googleapis.com/labelbox-datasets/dicom-sample-data/sample-mask-1.png"
}],
'instances': [
{
'colorRGB': (255, 255, 255),
'name': 'segmentation_mask_dicom'
}
]
}
}
Example: Import pre-labels or ground truths
The steps to import annotations as pre-labels (machine-assisted learning) are similar to those to import annotations as ground truth labels. However, they vary slightly, and we will describe the differences for each scenario.
Before you start
The below imports are needed to use the code examples in this section.
import labelbox as lb
import labelbox.types as lb_types
import uuid
Replace the value of API_KEY
with a valid API key to connect to the Labelbox client.
API_KEY = None
client = lb.Client(API_KEY)
Step 1: Import data rows
Data rows must first be uploaded to Catalog to attach annotations.
This example shows how to create a data row in Catalog by attaching it to a dataset .
global_key = "sample-dicom-1.dcm"
asset = {
"row_data": "https://storage.googleapis.com/labelbox-datasets/dicom-sample-data/sample-dicom-1.dcm",
"global_key": global_key,
}
dataset = client.create_dataset(name="dicom_demo_dataset")
task = dataset.create_data_rows([asset])
task.wait_till_done()
print("Errors :",task.errors)
print("Failed data rows:" ,task.failed_data_rows)
Step 2: Set up ontology
Your project ontology should support the tools and classifications required by your annotations. To ensure accurate schema feature mapping, the value used as the name
parameter should match the value of the name
field in your annotation.
For example, when we created an annotation above, we provided a nameannotation_name
. Now, when we set up our ontology, we must ensure that the name of our bounding box tool is also anotations_name
. The same alignment must hold true for the other tools and classifications we create in our ontology.
This example shows how to create an ontology containing all supported annotation types .
ontology_builder = lb.OntologyBuilder(
tools=[
lb.Tool(tool=lb.Tool.Type.RASTER_SEGMENTATION, name="segmentation_mask_dicom"),
lb.Tool(tool=lb.Tool.Type.LINE, name="line_dicom"),
]
)
ontology = client.create_ontology("Ontology DICOM Annotations", ontology_builder.asdict(), media_type=lb.MediaType.Dicom)
Step 3: Set Up a Labeling Project
project = client.create_project(name="dicom_project_demo", media_type=lb.MediaType.Dicom)
## connect ontology to your project
project.connect_ontology(ontology)
Step 4: Send Data Rows to Project
# Create a batch to send to your MAL project
batch = project.create_batch(
"first-batch-dicom-demo", # Each batch in a project must have a unique name
global_keys=[global_key], # a list of data row objects, data row ids or global keys
priority=5 # priority between 1(Highest) - 5(lowest)
)
print("Batch: ", batch)
Step 5: Create annotation payloads
For help understanding annotation payloads, see overview. To declare payloads, you can use Python annotation types (preferred) or NDJSON objects.
These examples demonstrate each format and how to compose annotations into labels attached to data rows.
annotations_list = polyline_annotation + mask_annotation
labels = [
lb_types.Label(
data={"global_key" : global_key },
annotations=annotations_list
)
]
label_ndjson = []
for annotation in [
polyline_annotation_ndjson,
mask_annotation_ndjson
]:
annotation.update({
'dataRow': {
'globalKey': global_key
}
})
label_ndjson.append(annotation)
Step 6: Import annotation payload
For prelabeled (model-assisted labeling) scenarios, pass your payload as the value of the predictions
parameter. For ground truths, pass the payload to the labels
parameter.
Option A: Upload as prelabels (model assisted labeling)
This option is helpful for speeding up the initial labeling process and reducing the manual labeling workload for high-volume datasets.
# Upload MAL label for this data row in project
upload_job = lb.MALPredictionImport.create_from_objects(
client = client,
project_id = project.uid,
name="mal_job"+str(uuid.uuid4()),
predictions=label
)
print(f"Errors: {upload_job.errors}", )
print(f"Status of uploads: {upload_job.statuses}"
Option B: Upload to a labeling project as ground truth
This option is helpful for loading high-confidence labels from another platform or previous projects that just need review rather than manual labeling effort.
# Upload label for this data row in project
upload_job = lb.LabelImport.create_from_objects(
client = client,
project_id = project.uid,
name="label_import_job"+str(uuid.uuid4()),
labels=label
)
print(f"Errors: {upload_job.errors}", )
print(f"Status of uploads: {upload_job.statuses}")