Edit Labels

Editing labels after they have been created involves the following steps:
  1. Find the label instance you want to edit.
  2. Edit the value for the label.
  3. Save the changes.
The script below shows how to update the location of an object’s bounding box, and attribute values. Ensure you substitute the following values:
  • <private_key_path> with the full path to your private key.
  • <project_hash> with the name of the project you want to update labels in.
  • <file_name> with the name of the file / data unit you want to edit labels for.
  • <object_name> with the name of the object.
  • <attribute_name> with the name of the attribute.
  • <option_name> with the name of the radio button option of the attribute.
# Import dependencies
from typing import List

from encord import EncordUserClient
from encord.objects import LabelRowV2, Object, ObjectInstance, OntologyStructure
from encord.objects.coordinates import BoundingBoxCoordinates

# Instantiate Encord client by replacing <private_key_path> with the path to your private key
user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path="<private_key_path>"
)

# Specify the project. Replace <project_hash> with the hash of your Project
project = user_client.get_project("<project_hash>")

# Specify the label row you want to edit labels for. This example takes the first label row
# Replace the <file_name.mp4> with the name of the file you want to change labels for
first_label_row = project.list_label_rows_v2(
    data_title_eq="<file_name>"
)[0]

# Download the existing labels 
first_label_row.initialise_labels()

# Get the first object instance in the label row
ontology_structure = first_label_row.ontology_structure

# Gets the object instance we want to edit the label for. This example uses the first
first_obj_instance = first_label_row.get_object_instances()[0]

# Specifies which Ontology object to change labels for
# Replace <object_name> with the name of your object. For example 'Flower'
my_object = ontology_structure.get_child_by_title(title="<object_name>")

# Sets coordinates for the specified object instance
# This example changes the location of a bounding box
first_obj_instance.set_for_frames(
    coordinates = BoundingBoxCoordinates(
        height=0.1474,
        width =0.1154,
        top_left_x=0.1097,
        top_left_y=0.3209
    ),
    # Overwrites if annotations are already present
    manual_annotation=False, overwrite=True
)

# Selects a Radio button attribute
# Replace <attribute_name> with the name of the attribute you want to edit. For example 'Flower species'
my_attribute = my_object.get_child_by_title(title="<attribute_name>", type_=RadioAttribute)

# Selects the radio button option of the attribute
# Replace <option_name> with the name of the option. For example 'Sunflower' 
attribute_option = my_attribute.get_child_by_title(title="<option_name>", type_= Option)

# Sets the attribute to the attribute_option defined above
first_object_instance.set_answer(attribute_option, overwrite=True)

# Once you have updated labels and attributes, save your changes
first_label_row.save()

Delete Labels/Classifications

The following code deletes specific labels/classifications from data units.

from encord import EncordUserClient

SSH_PATH = "/Users/chris-encord/ssh-private-key.txt"
PROJECT_ID = "41397c50-d4d5-4c73-bf5d-cb2f72c0dc31"
DATA_UNIT_NAME = "cherry-002.jpg"

# Instantiate client
user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path=SSH_PATH,
    # If you use the US Encord platform, change domain to https://api.us.encord.com
    domain="https://api.encord.com",
)

# Specify Project
project = user_client.get_project(PROJECT_ID)

# Specify the data unit you want to modify labels for
specific_label_row = project.list_label_rows_v2(
    data_title_eq=DATA_UNIT_NAME
)[0]

# Find the object to remove
object_to_remove = None
specific_label_row.initialise_labels()
for object_instance in specific_label_row.get_object_instances():
    # Specify the label ID for the label to remove
    if object_instance.object_hash == '0pw0bHXR':
        object_to_remove = object_instance

# Remove the object
specific_label_row.remove_object(object_to_remove)

# Save the changes
specific_label_row.save()

Delete All Labels/Classifications

The following code deletes all labels/classifications from the specified data unit.

from encord import EncordUserClient

# Authentication
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt"
PROJECT_ID = "41397c50-d4d5-4c73-bf5d-cb2f72c0dc31"

# Authenticate
user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path=SSH_PATH,
    # If you use the US Encord platform, change domain to https://api.us.encord.com
    domain="https://api.encord.com",
    )

# Get Project
project = Project = user_client.get_project(PROJECT_ID)

# Get label rows
label_rows = project.list_label_rows_v2()
BUNDLE_SIZE = 100

# Initialize label rows using bundles
with project.create_bundle(bundle_size=BUNDLE_SIZE) as bundle:
    for label_row in label_rows:
        label_row.initialise_labels(bundle=bundle)

# Delete all objects from specific images
for label_row in label_rows:
    # Check if this is an image you want to modify
    if label_row.data_title in ["cherry-002.jpg"]:  # Replace with your image names
        # Get all object instances
        objects_to_remove = label_row.get_object_instances()
        # Remove all objects
        for obj in objects_to_remove:
            label_row.remove_object(obj)

# Save changes using bundles
with project.create_bundle(bundle_size=BUNDLE_SIZE) as bundle:
    for label_row in label_rows:
        label_row.save(bundle=bundle)

Delete All Specific Labels/Classifications

The following code removes all labels/classifications of a specific type from data units.

from encord import EncordUserClient
from encord.objects import Object

# Authentication
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt"
PROJECT_ID = "41397c50-d4d5-4c73-bf5d-cb2f72c0dc31"
TARGET_CLASS_NAME = "Keypoint"
DATA_UNIT_NAME = "cherry-002.jpg"

# Authenticate
user_client = EncordUserClient.create_with_ssh_private_key(
    ssh_private_key_path=SSH_PATH,
    # If you use the US Encord platform, change domain to https://api.us.encord.com
    domain="https://api.encord.com",
)

# Get the project
project = user_client.get_project(PROJECT_ID)

# Get all label rows in the project
label_rows = project.list_label_rows_v2()

BUNDLE_SIZE = 100

# Initialize label rows using bundles
with project.create_bundle(bundle_size=BUNDLE_SIZE) as bundle:
    for label_row in label_rows:
        label_row.initialise_labels(bundle=bundle)

# Retrieve the ontology object matching the target class name
ontology_object = project.ontology_structure.get_child_by_title(
    title=TARGET_CLASS_NAME,
    type_=Object
)

# Loop through the label rows and delete only the object instances matching the ontology object
for label_row in label_rows:
    if label_row.data_title == DATA_UNIT_NAME:
        
        # This returns only the instances of the specific ontology object
        objects_to_remove = label_row.get_object_instances(
            filter_ontology_object=ontology_object
        )

        print(f"Found {len(objects_to_remove)} '{TARGET_CLASS_NAME}' objects in '{DATA_UNIT_NAME}'")

        # Remove those objects
        for obj in objects_to_remove:
            label_row.remove_object(obj)

# Save changes using bundles
with project.create_bundle(bundle_size=BUNDLE_SIZE) as bundle:
    for label_row in label_rows:
        label_row.save(bundle=bundle)

print(f"Finished! Removed all '{TARGET_CLASS_NAME}' objects from '{DATA_UNIT_NAME}'.")