MD.ai
How-To Guides

Convert JSON

Convert exported annotations into formats for training frameworks

1. Download JSON

  • Option 1: Download the JSON file from the annotator's Export panel, and move it into your working directory.
  • Option 2: Download it with the Python client, as below.

Create Client

import mdai
DOMAIN = 'public.md.ai'
ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'
PROJECT_ID = 'MwBe19Br'  # from the annotator's Info panel
mdai_client = mdai.Client(domain=DOMAIN, access_token=ACCESS_TOKEN)

Example output

Successfully authenticated to public.md.ai.

Download annotations

# Download only the annotation data
p = mdai_client.project(PROJECT_ID, path='.',  annotations_only=True)

Example output

Using working directory for data.
Preparing annotations export for project MwBe19Br...
Downloading file: mdai_public_project_MwBe19Br_annotations_labelgroup_all_2020-09-23-214038.json
Success: annotations data for project MwBe19Br ready.
No project created. Downloaded annotations only.

2. JSON to Dataframe

Copy the downloaded file name from the output above or from your downloaded JSON file. (if errors occur, try downloading annotations outside of your firewall)

# Replace with your filename
JSON = 'mdai_public_project_MwBe19Br_annotations_labelgroup_all_2020-09-23-214038.json'
results = mdai.common_utils.json_to_dataframe(JSON)
# Annotations dataframe
annots_df = results['annotations']

3. Conversions

Bounding box

Extract box data dictionary items

import json

# Simplify table
columns_brief = ['id', 'StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID', 'labelName', 'data', 'annotationMode']
annots_df = annots_df[columns_brief]

# Box annotations
boxes = annots_df[annots_df.annotationMode == 'bbox']

# Extract box data
def extract_box_data(df):
    j = df.copy()
    j = j[(j.annotationMode == 'bbox') & (~j.data.isnull())]
    try:
        j['data'] = j['data'].apply(lambda x:json.loads(x.replace("'", "\"")))
    except:
        j['data']

    j['x'] = [d['x'] for _,d in j.data.items()]
    j['y'] = [d['y'] for _,d in j.data.items()]
    j['w'] = [d['width'] for _,d in j.data.items()]
    j['h'] = [d['height'] for _,d in j.data.items()]
    j = j.drop('data', axis=1)
    return j

boxes = extract_box_data(boxes)
boxes.head()

Example output

	id	StudyInstanceUID	SeriesInstanceUID	SOPInstanceUID	labelName	annotationMode	x	y	w	h
0	A_J07mWn	1.3.6.1.4.1.14519.5.2.1.1079.4008.312038908377...	1.3.6.1.4.1.14519.5.2.1.1079.4008.228089024512...	1.3.6.1.4.1.14519.5.2.1.1079.4008.515564548072...	Stomach	bbox	220.822845	124.504723	121.008514	82.380470
1	A_qxVdNk	1.3.6.1.4.1.14519.5.2.1.1079.4008.312038908377...	1.3.6.1.4.1.14519.5.2.1.1079.4008.228089024512...	1.3.6.1.4.1.14519.5.2.1.1079.4008.306261432947...	Stomach	bbox	183.308075	148.138794	99.586609	68.920502
2	A_49KLWk	1.3.6.1.4.1.14519.5.2.1.1079.4008.312038908377...	1.3.6.1.4.1.14519.5.2.1.1079.4008.228089024512...	1.3.6.1.4.1.14519.5.2.1.1079.4008.132057312174...	Stomach	bbox	251.314133	125.590210	84.764328	103.895081

Derive additional box data

Now lets get the box center, area, and bottom left corner

boxes['area'] = boxes.w * boxes.h
boxes['center_x'] = boxes.x + boxes.w/2
boxes['center_y'] = boxes.y + boxes.h/2
boxes['bottom_x'] = boxes.x + boxes.w
boxes['bottom_y'] = boxes.y + boxes.h
# Convert values to integers for simplicity
boxes[boxes.columns[6:]] = boxes[boxes.columns[6:]].astype('int')
boxes.head()

Example output

	id	StudyInstanceUID	SeriesInstanceUID	SOPInstanceUID	labelName	annotationMode	x	y	w	h	area	center_x	center_y	bottom_x	bottom_y
0	A_J07mWn	1.3.6.1.4.1.14519.5.2.1.1079.4008.312038908377...	1.3.6.1.4.1.14519.5.2.1.1079.4008.228089024512...	1.3.6.1.4.1.14519.5.2.1.1079.4008.515564548072...	Stomach	bbox	220	124	121	82	9968	280	165	341	206
1	A_qxVdNk	1.3.6.1.4.1.14519.5.2.1.1079.4008.312038908377...	1.3.6.1.4.1.14519.5.2.1.1079.4008.228089024512...	1.3.6.1.4.1.14519.5.2.1.1079.4008.306261432947...	Stomach	bbox	183	148	99	68	6863	232	182	282	217
2	A_49KLWk	1.3.6.1.4.1.14519.5.2.1.1079.4008.312038908377...	1.3.6.1.4.1.14519.5.2.1.1079.4008.228089024512...	1.3.6.1.4.1.14519.5.2.1.1079.4008.132057312174...	Stomach	bbox	251	125	84	103	8806	293	177	335	229

Formatting examples

Detectron 2

Detectron 2 expects bbox coordinates to be in the format of [x_upper_left, y_upper_left, x_lower_right, y_lower_right]. Be mindful of the need to scale the annotation data if image is scaled

scale = 1.
boxes['detectron_bbox'] =  [[row.x * scale, row.y * scale, row.bottom_x * scale, row.bottom_y * scale] for _,row in boxes.iterrows()]
Fastai

Fastai expects bbox coordinates to be in the format of (y_upper_left, x_upper_left, y_lower_right, x_lower_right) with the origin being in the upper left hand corner of the image. Remember to scale annotations if images are scaled

scale = 1.
boxes['fastai_bbox'] =  [[row.y * scale, row.x * scale, row.bottom_y * scale, row.bottom_x * scale] for _,row in boxes.iterrows()]

Freeform and polygon

Filter for freeform and polygon

import json

# Simplify table
columns_brief = ['id', 'StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID', 'labelName', 'data', 'annotationMode']
annots_df = annots_df[columns_brief]

# Shape annotations
shapes = annots_df[(annots_df.annotationMode == 'freeform') | (annots_df.annotationMode == 'polygon')]

Get box bounding the vertices

# Extract box data from vertices
import numpy as np

def vertices_to_boxes(data):
    vertices = data['vertices']
    px=[v[0] for v in vertices]
    py=[v[1] for v in vertices]
    x = int(np.min(px))
    y = int(np.min(py))
    x2 = int(np.max(px))
    y2 = int(np.max(py))
    w = x2 - x
    h = y2 - y
    return (x, y, w, h, x2, y2)

shapes['x'],shapes['y'],shapes['w'],shapes['h'],shapes['bottom_x'],shapes['bottom_y'] = zip(*shapes['data'].map(vertices_to_boxes))
shapes.head()

Example output

id	StudyInstanceUID	SeriesInstanceUID	SOPInstanceUID	labelName	data	annotationMode	x	y	w	h	bottom_x	bottom_y
0	A_JddPQE	1.3.46.670589.11.20182.5.0.8336.20130724083604...	1.3.46.670589.11.20182.5.0.7188.20130724085044...	1.3.6.1.4.1.9590.100.1.2.260616927513309992437...	Liver	{'vertices': [[64, 130], [64, 129], [64, 128],...	freeform	54	115	10	20	64	135
1	A_qG28ZY	1.3.46.670589.11.20182.5.0.8336.20130724083604...	1.3.46.670589.11.20182.5.0.7188.20130724085044...	1.3.6.1.4.1.9590.100.1.2.388757691711551811514...	Liver	{'vertices': [[66, 129], [66, 128], [66, 127],...	freeform	53	112	13	27	66	139

Longest diameter

Get longest diameter of a shape in mm. Pixel spacing is obtained using the pydicom library and the PixelSpacing tag

import cv2

def longest_diameter(data, pixel_spacing):
    try:
        row_spacing, col_spacing = pixel_spacing
    except:
        return -1

    max_points = most_distant_points(np.array(data['vertices'], dtype=np.float32))
    x1 = max_points[0][0]
    y1 = max_points[0][1]
    x2 = max_points[1][0]
    y2 = max_points[1][1]

    dx = col_spacing * (x2 - x1)
    dy = row_spacing * (y2 - y1)
    distance = np.sqrt(dx ** 2 + dy ** 2)
    # returns longest diameter in mm
    return round(distance, 2)

def most_distant_points(points):
    hull = cv2.convexHull(points, returnPoints=True)

    max_points = [points[0], points[1]]
    max_distance = 0
    for i in range(0,len(hull)):
        for j in range(i+1,len(hull)):
            p1 = hull[i][0]
            p2 = hull[j][0]
            distance = calc_distance(p1, p2)
            if distance > max_distance:
                max_points = [p1, p2]
                max_distance = distance
    return max_points

def calc_distance(p1, p2):
    return np.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)

Example

import pydicom as py

ds = py.dcmread('000.dcm')
row = shapes.iloc[0]  # an annotation on this image
ld = longest_diameter(row.data, ds.PixelSpacing)

Mask

To turn any annotation, including masks, into a binary mask, see Mask from one annotation.

On this page