Exporting Combined Annotation Arrays and Mapping Task IDs to Image Names in Label Studio

Question

Is there a way within Label Studio to export a single annotation array for each task when using tools like the brush and magic wand?

Answer:

Label Studio does not natively provide a feature to automatically combine multiple annotations into a single numpy array per task upon export. However, you can achieve this by writing a custom script in Python that processes the exported annotations. The script would iterate over each task, collect all associated annotations, and merge them into one array. Here’s a skeleton of how you’d do that:

import numpy as np
import json

# Assume you have an exported JSON file with annotations from Label Studio
with open('annotations.json', 'r') as file:
    annotations = json.load(file)

combined_annotations = {}
for ann in annotations:
    task_id = ann['task']
    annotation_array = np.array(ann['result'])
    
    # Update combined_annotations[task_id] with the new array
    # You might need to use np.vstack or np.hstack depending on your annotation structure

# You now have combined_annotations dict with task_id as keys and unified numpy arrays as values

Follow-up Question

What is the best way to map task IDs back to the original image name after exporting from Label Studio?

Answer

To associate task IDs with their original image names, you can reference the export file that contains the task information. You would extract the image names from the ‘data’ field where they are stored. Here’s a brief example:

with open('tasks.json', 'r') as file:
    tasks = json.load(file)

task_to_image = {}
for task in tasks:
    task_id = task['id']
    image_name = task['data']['image']  # Make sure to use the correct key that holds the image name
    task_to_image[task_id] = image_name

# task_to_image dictionary maps task IDs to their original image names

Please note that you will need to adjust the key values (‘result’, ‘data’, ‘image’) in the snippets accordingly based on the actual export format of your Label Studio annotations and tasks.