Delete Specific 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}'.")