Critical Information
Using classifications on an audio file is similar to using labels or classifications on a video file. There are some critical differences though:- 1 “frame” = 1 millisecond for audio files when importing classifications.
-
When creating a classification for an audio file, the
classification_instanceuses therange_only=Trueargument.
classification_instance = ontology_classification.create_instance(range_only=True), where ontology_classification is the Classification from your Project’s Ontology.
Audio Classification ranges
ranges = []
for range in classification_instance.range_list:
ranges.append([range.start, range.end])
Audio Regions on Audio Files
Export Audio Regions on Audio Files
# Import dependencies
from pathlib import Path
from encord import EncordUserClient, Project
import json
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Get the Project
project: Project = user_client.get_project(PROJECT_ID)
# Get label rows for your Project
label_rows = project.list_label_rows_v2()
# Initialize label rows using bundles
with project.create_bundle() as bundle:
for label_row in label_rows:
label_row.initialise_labels(bundle=bundle)
# Output directory for JSON files
output_dir = Path("./label_exports")
output_dir.mkdir(exist_ok=True)
# Export each label row to a JSON file
for label_row in label_rows:
# Convert label row to dictionary
label_data = label_row.to_encord_dict()
# Save the label data to a JSON file
file_name = f"{label_row.data_title}.json"
output_path = output_dir / file_name
with open(output_path, "w", encoding="utf-8") as json_file:
json.dump(label_data, json_file, indent=4)
print(f"Exported {label_row.data_title} to {output_path}")
print("All labels have been exported!")
# Import dependencies
from pathlib import Path
from encord import EncordUserClient, Project
import json
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Get the Project
project: Project = user_client.get_project(PROJECT_ID)
# Get label rows for your Project
label_rows = project.list_label_rows_v2()
# Initialize label rows using bundles
with project.create_bundle() as bundle:
for label_row in label_rows:
label_row.initialise_labels(bundle=bundle)
# Output directory for JSON files
output_dir = Path("./label_exports")
output_dir.mkdir(exist_ok=True)
# Export each label row to a JSON file
for label_row in label_rows:
# Convert label row to dictionary
label_data = label_row.to_encord_dict()
# Save the label data to a JSON file
file_name = f"{label_row.data_title}.json"
output_path = output_dir / file_name
with open(output_path, "w", encoding="utf-8") as json_file:
json.dump(label_data, json_file, indent=4)
print(f"Exported {label_row.data_title} to {output_path}")
print("All labels have been exported!")
Import Audio Regions to Audio Files
# Import dependencies
from encord import EncordUserClient, Project
from encord.objects import (
Object,
ObjectInstance,
)
from encord.objects.coordinates import AudioCoordinates
from encord.objects.frames import Range
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Get the Project
project: Project = user_client.get_project(PROJECT_ID)
# Example mapping of data unit titles to start and end frame ranges
# Replace with your audio file titles and the (start, end) range of each Audio Region, in milliseconds
audio_ranges = {
"<audio-file-01>": [(0, 1000), (2000, 3000), (4000, 5000)],
"<audio-file-02>": [(0, 1000), (2000, 3000)],
"<audio-file-03>": [(0, 1000)],
}
# Find the ontology object for your Audio Region
audio_ontology_object: Object = project.ontology_structure.get_child_by_title(
title="<audio-region-name>", type_=Object
)
# Get the label row for each audio file in audio_ranges
label_rows = []
for data_title in audio_ranges:
rows = project.list_label_rows_v2(data_title_eq=data_title)
if not rows:
print(f"Skipping: No label row found for {data_title}")
continue
label_rows.append(rows[0])
# Initialize label rows using bundles
with project.create_bundle() as bundle:
for label_row in label_rows:
label_row.initialise_labels(bundle=bundle)
# Loop through each label row and apply updates
for label_row in label_rows:
label_row_title = label_row.data_title
# Apply all ranges as separate objects
for start, end in audio_ranges.get(label_row_title, []):
# Instantiate an object instance from the ontology node
audio_object_instance: ObjectInstance = audio_ontology_object.create_instance()
# Set object instance with specific start and end frames
audio_object_instance.set_for_frames(
coordinates=AudioCoordinates(range=[Range(start=start, end=end)]), # Adjust as needed
frames=0,
manual_annotation=True,
confidence=1.0,
)
# Link the object instance to the label row
label_row.add_object_instance(audio_object_instance)
print(f"Added range ({start}, {end}) to label row: {label_row_title}")
# Upload the label to the server after adding all instances
label_row.save()
print(f"Saved label row: {label_row_title}")
print("Updates applied with predefined frame ranges for each data unit!")
# Import dependencies
from encord import EncordUserClient, Project
from encord.objects import (
Object,
ObjectInstance,
)
from encord.objects.coordinates import AudioCoordinates
from encord.objects.frames import Range
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Get the Project
project: Project = user_client.get_project(PROJECT_ID)
# Example mapping of data unit titles to start and end frame ranges
audio_ranges = {
"my-audio-file.mp3": [(8000, 15000), (20000, 25000), (31000, 43003)],
"your-audio-file.mp3": [(2500, 4500), (6543, 9876)],
"their-audio-file.mp3": [(4321, 10987)],
}
# Find the ontology object for the Audio Region "Speaking"
audio_ontology_object: Object = project.ontology_structure.get_child_by_title(
title="Speaking", type_=Object
)
# Get the label row for each audio file in audio_ranges
label_rows = []
for data_title in audio_ranges:
rows = project.list_label_rows_v2(data_title_eq=data_title)
if not rows:
print(f"Skipping: No label row found for {data_title}")
continue
label_rows.append(rows[0])
# Initialize label rows using bundles
with project.create_bundle() as bundle:
for label_row in label_rows:
label_row.initialise_labels(bundle=bundle)
# Loop through each label row and apply updates
for label_row in label_rows:
label_row_title = label_row.data_title
# Apply all ranges as separate objects
for start, end in audio_ranges.get(label_row_title, []):
# Instantiate an object instance from the ontology node
audio_object_instance: ObjectInstance = audio_ontology_object.create_instance()
# Set object instance with specific start and end frames
audio_object_instance.set_for_frames(
coordinates=AudioCoordinates(range=[Range(start=start, end=end)]), # Adjust as needed
frames=0,
manual_annotation=True,
confidence=1.0,
)
# Link the object instance to the label row
label_row.add_object_instance(audio_object_instance)
print(f"Added range ({start}, {end}) to label row: {label_row_title}")
# Upload the label to the server after adding all instances
label_row.save()
print(f"Saved label row: {label_row_title}")
print("Updates applied with predefined frame ranges for each data unit!")
Classifications on Audio Files
View Classifications on Audio Files
View Audio Classifications
# Import dependencies
from encord import EncordUserClient
from encord.objects.options import Option
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Specify Project
project = user_client.get_project(PROJECT_ID)
# Get a readable value and hash for any classification answer: an Option for radio buttons,
# a list of Options for checklists, a string for text, or None when there is no answer
def describe_answer(answer):
if isinstance(answer, Option):
return answer.value, answer.feature_node_hash
if isinstance(answer, list):
return [option.value for option in answer], [option.feature_node_hash for option in answer]
return answer, None
# Downloads a local copy of all the labels
label_rows = project.list_label_rows_v2(include_all_label_branches=True)
# Initialize label rows using bundles
with project.create_bundle() as bundle:
for label_row in label_rows:
label_row.initialise_labels(bundle=bundle)
for label_row in label_rows:
print(f"Title: {label_row.data_title}, branch: {label_row.branch_name}")
# Print all essential classification information
for classification_instance in label_row.get_classification_instances():
print(f"classificationHash: {classification_instance.classification_hash}")
print(f"Classification name: {classification_instance.classification_name}")
print(f"featureHash: {classification_instance.feature_hash}")
answer, answer_hash = describe_answer(classification_instance.get_answer())
print(f"Classification answer: {answer}")
print(f"Classification answer hash: {answer_hash}")
Export Classifications from Audio Files
Export Audio Classifications
# Import dependencies
from encord import EncordUserClient
from encord.objects.options import Option
import json
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Specify Project
project = user_client.get_project(PROJECT_ID)
# Get a readable value and hash for any classification answer: an Option for radio buttons,
# a list of Options for checklists, a string for text, or None when there is no answer
def describe_answer(answer):
if isinstance(answer, Option):
return answer.value, answer.feature_node_hash
if isinstance(answer, list):
return [option.value for option in answer], [option.feature_node_hash for option in answer]
return answer, None
# Downloads a local copy of all the labels
label_rows = project.list_label_rows_v2(include_all_label_branches=True)
# Initialize label rows using bundles
with project.create_bundle() as bundle:
for label_row in label_rows:
label_row.initialise_labels(bundle=bundle)
for label_row in label_rows:
print(f"Title: {label_row.data_title}, branch: {label_row.branch_name}")
# Export classifications
classifications = []
for classification_instance in label_row.get_classification_instances():
answer, answer_hash = describe_answer(classification_instance.get_answer())
classification_data = {
"classificationHash": classification_instance.classification_hash,
"name": classification_instance.classification_name,
"featureHash": classification_instance.feature_hash,
"answer": answer,
"answerHash": answer_hash,
}
classifications.append(classification_data)
# Print or save the exported classifications
print(json.dumps(classifications, indent=2))
# Alternatively, you can save to a file:
# with open(f"{label_row.data_title}_classifications.json", "w") as f:
# json.dump(classifications, f, indent=2)
Import Classifications to Audio Files
# Import dependencies
from encord import EncordUserClient, Project
from encord.objects import Classification
from encord.objects.options import Option
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Specify the project
project = user_client.get_project(PROJECT_ID)
# Define a mapping where each file has a list of classifications and options
file_classification_mapping = {
"<audio-file-01.file-extension>": [
{"classification_name": "<classification-name-01>", "option_title": "<option-value>"},
{"classification_name": "<classification-name-02>", "option_title": "<option-value>"},
],
"<audio-file-02.file-extension>": [
{"classification_name": "<classification-name-01>", "option_title": "<option-value>"},
],
"<audio-file-03.file-extension>": [
{"classification_name": "<classification-name-01>", "option_title": "<option-value>"},
],
# Add more mappings as needed
}
# Loop through the mapping to apply classifications to each unique file
for file_name, classifications in file_classification_mapping.items():
try:
# Fetch the label row for the given file name
label_rows = project.list_label_rows_v2(data_title_eq=file_name)
if not label_rows:
print(f"No label row found for {file_name}, skipping")
continue
label_row = label_rows[0]
print(label_row.data_hash)
label_row.initialise_labels()
for classification_data in classifications:
# Fetch the classification from the ontology
ontology_classification: Classification = project.ontology_structure.get_child_by_title(
title=classification_data["classification_name"],
type_=Classification,
)
# Fetch the classification option
option = ontology_classification.get_child_by_title(
title=classification_data["option_title"], type_=Option
)
# Create a classification instance
# range_only=True must be used when creating the classification
classification_instance = ontology_classification.create_instance(range_only=True)
classification_instance.set_answer(answer=option)
# Set classification to apply to frame 0
classification_instance.set_for_frames(
frames=0,
manual_annotation=True,
confidence=1.0,
)
# Add classification instance to the label row
label_row.add_classification_instance(classification_instance)
# Save the updated label row after all classifications are added
label_row.save()
print(f"Successfully updated classifications for {file_name}.")
except Exception as e:
print(f"Failed to update classifications for {file_name}: {e}")
# Import dependencies
from encord import EncordUserClient, Project
from encord.objects import Classification
from encord.objects.options import Option
# User input
SSH_PATH = "/Users/chris-encord/ssh-private-key.txt" # Replace with the file path to your SSH private key
PROJECT_ID = "00000000-0000-0000-0000-000000000000" # Replace with the unique Project ID
# Authenticate with Encord using the path to your private key
user_client: EncordUserClient = EncordUserClient.create_with_ssh_private_key(
ssh_private_key_path=SSH_PATH,
# For US platform users use domain="https://api.us.encord.com"
domain="https://api.encord.com",
)
# Specify the project
project = user_client.get_project(PROJECT_ID)
# Define a mapping where each file has a list of classifications and options
file_classification_mapping = {
"sh-b-p.mp3": [
{"classification_name": "Person speaking", "option_title": "Yes"},
{"classification_name": "Volume", "option_title": "Loud"},
],
"superhero-trailer-110450.mp3": [
{"classification_name": "Person speaking", "option_title": "No"},
],
"stomping-rock-four-shots-111444.mp3": [
{"classification_name": "Person speaking", "option_title": "No"},
],
# Add more mappings as needed
}
# Loop through the mapping to apply classifications to each unique file
for file_name, classifications in file_classification_mapping.items():
try:
# Fetch the label row for the given file name
label_rows = project.list_label_rows_v2(data_title_eq=file_name)
if not label_rows:
print(f"No label row found for {file_name}, skipping")
continue
label_row = label_rows[0]
print(label_row.data_hash)
label_row.initialise_labels()
for classification_data in classifications:
# Fetch the classification from the ontology
ontology_classification: Classification = project.ontology_structure.get_child_by_title(
title=classification_data["classification_name"],
type_=Classification,
)
# Fetch the classification option
option = ontology_classification.get_child_by_title(
title=classification_data["option_title"], type_=Option
)
# Create a classification instance
# range_only=True must be used when creating the classification
classification_instance = ontology_classification.create_instance(range_only=True)
classification_instance.set_answer(answer=option)
# Set classification to apply to frame 0
classification_instance.set_for_frames(
frames=0,
manual_annotation=True,
confidence=1.0,
)
# Add classification instance to the label row
label_row.add_classification_instance(classification_instance)
# Save the updated label row after all classifications are added
label_row.save()
print(f"Successfully updated classifications for {file_name}.")
except Exception as e:
print(f"Failed to update classifications for {file_name}: {e}")

