Agents Reference Core
Core
b64_encode_image
Encode an image to a base64 string.
Arguments:
img
- The image to encode. Expects [RGB] channelsformat
- The format of the image.
Returns:
The base64 encoded image.
TaskSpeedColumn Objects
Renders human readable transfer speed.
render
Show data transfer speed.
LabelRowMetadataIncludeArgs Objects
Warning, including metadata via label rows is good for reading metadata not for writing to the metadata.
If you need to write to metadata, use the dep_storage_item
dependencies instead.
LabelRowInitialiseLabelsArgs Objects
Arguments used to specify how to initialise labels via the SDK.
The arguments are passed to LabelRowV2.initialise_labels
.
FrameData Objects
Holds the data sent from the Encord Label Editor at the time of triggering the agent.
project_hash
The identifier of the given project.
data_hash
The identifier of the given data asset.
frame
The frame number. If single image, it’s default 0.
object_hashes
Object hashes if the request was made on particular objects from the App
Frame Objects
A dataclass to hold the content of one frame in a video.
frame
The frame number within the video
content
An [h,w,c] np.array with color channels RGB.
b64_encoding
Get a base64 representation of the image content.
This method allows you to convert the content into a base64 representation based on various different image encodings. This is useful, e.g., for prompting LLMs with image content.
Please see details for formats below.
Arguments:
image_format
- Which type of image encoding to use.output_format
- Different common formats.raw
: the image content as a raw b64 stringurl
: url encoded image content. Compatible with, e.g.,<img src="<the_encoding>" />
openai
: a dict withtype
andimage_url
keys _anthropic
: a dict withmedia_type
,type
, anddata
keys.
Returns
- a dict or string depending onoutput_format
.
InstanceCrop Objects
A dataclass to hold the frame content of one object instance in a video or image.
instance
The ObjectInstance associated to the crop.
EditorAgentResponse Objects
A base class for all return types of editor agent functions.
message
A message to be displayed to the user.
get_user_client
Generate an user client to access Encord.
Returns:
An EncordUserClient authenticated with the credentials from the encord_agents.core.settings.Settings.
get_initialised_label_row
Get an initialised label row from the frame_data information.
Arguments:
frame_data
- The data pointing to the data asset.
Raises:
Exception
- If theframe_data
cannot be matched to a label row
Returns:
The initialized label row.
download_asset
Download the asset associated to a label row to disk.
This function is a context manager. Data will be cleaned up when the context is left.
Example usage:
Arguments:
storage_item
- The Storage item for which you want to download the associated asset.frame
- The frame that you need. If frame is none for a video, you will get the video path.
Raises:
NotImplementedError
- If you try to get all frames of an image group.ValueError
- If you try to download an unsupported data type (e.g., DICOM).
Yields:
The file path for the requested asset.
get_frame_count
Get the number of frames in a video.
batch_iterator
Yield batches of items from an iterator.
Arguments:
iterator
- The source iteratorbatch_size
- Size of each batch > 0
Returns:
Iterable of lists, each containing up to batch_size items
GenericFieldModel Objects
set_answer
This function will be called from the parsing loop to allow the model to set it self as answer on the classification instance.
FieldType
Field from pydantic can be anything so hard to type. This is supposed to indicate that you should use the
pydantic.Field
function to construct this var.
OntologyDataModel Objects
Class to create a pydantic model equivalent to an arbitrary classification ontology.
The model can be used to form a json schema based on the ontology. This is useful if you are, e.g., trying to get a structured response from an LLM.
Example:
Attributes:
ontology: DataModel:
__call__
Validate a json response in accordance to the pydantic model.
This function allows you to convert from a json object (e.g., coming from an llm) back to the encord “instance format”.
Arguments:
-
answer
- The json object as a raw string. -
Returns
- a list of classification / object instances that you will then have to add to a label row.
validate_json
Validate a json response in accordance to the pydantic model.
This function allows you to convert from a json object (e.g., coming from an llm) back to the encord “instance format”.
Arguments:
-
answer_str
- The json object as a raw string. -
Returns
- a list of classification / object instances that you will then have to add to a label row.
Settings used throughout the module.
Note that central settings will be read via environment variables.
Settings Objects
ssh_key_file
The path to the private ssh key file to authenticate with Encord.
Either this or the ENCORD_SSH_KEY
needs to be set for most use-cases.
To setup a key with Encord, please see
the platform docs.
ssh_key_content
The content of the private ssh key file to authenticate with Encord.
Either this or the ENCORD_SSH_KEY
needs to be set for most use-cases.
To setup a key with Encord, please see
the platform docs.
get_frame
Extract an exact frame from a video.
Arguments:
video_path
- The file path to where the video is stored.desired_frame
- The frame to extract
Raises:
Exception
- If the video cannot be opened properly or the requested frame could not be retrieved from the video.
Returns:
Numpy array of shape [h, w, c] where channels are BGR.
write_frame
Write a frame to a file.
Arguments:
frame_path
- The file path to write the frame to.frame
- The frame to write.
iter_video
Iterate video frame by frame.
Arguments:
video_path
- The file path to the video you wish to iterate.
Raises:
Exception
- If the video file could not be opened properly.
Yields:
Frames from the video.
iter_video_with_indices
Iterate video frame by frame with specified frame indices.
Arguments:
video_path
- The file path to the video you wish to iterate.frame_indices
- The frame indices to iterate over.
Yields:
Frames from the video.
This module defines dependencies available for injection within serverless Editor Agents. These dependencies can be used independently, even when reliant on other dependencies.
Note: The injection mechanism necessitates the presence of type annotations for the following parameters to ensure proper resolution.
FrameData
is automatically injected via the api request body.Project
is automatically loaded based on the frame data.label_row_v2
is automatically loaded based on the frame data.
dep_client
Dependency to provide an authenticated user client.
Example:
dep_single_frame
Dependency to inject the first frame of the underlying asset.
The downloaded asset will be named lr.data_hash.{suffix}
.
When the function has finished running, the downloaded file is removed from the file system.
Example:
Arguments:
storage_item
- The Storage item. Automatically injected (see example above).
Returns:
Numpy array of shape [h, w, 3] RGB colors.
dep_asset
Returns a local file path to the data asset, temporarily stored for the duration of the agent’s execution.
This dependency fetches the underlying data asset using a signed URL.
The asset is temporarily stored on disk for the duration of the task and is automatically removed once the task completes.
Example:
Returns:
The path to the asset.
Raises:
ValueError
- if the underlying assets are not videos, images, or audio.EncordException
- if data type not supported by SDK yet.
dep_video_iterator
Dependency to inject a video frame iterator for performing operations over many frames.
Example:
Arguments:
storage_item
- Automatically injected storage item dependency.
Raises:
NotImplementedError
- Fails for data types other than video.
Yields:
An iterator.
dep_data_lookup
Returns a lookup for easily retrieving data rows and storage items associated with the given task.
!!! warning “Deprecated”
dep_data_lookup
is deprecated and will be removed in version 0.2.10.
Use dep_storage_item
instead for accessing storage items.
Migration Guide:
Arguments:
lookup
- The object that you can use to lookup data rows and storage items. Automatically injected.
Returns:
The (shared) lookup object.
dep_storage_item
Get the storage item associated with the underlying agent task.
The StorageItem
is useful for multiple things like
- Updating client metadata
- Reading file properties like storage location, fps, duration, DICOM tags, etc.
Example
dep_object_crops
Returns a list of object instances and frame crops associated with each object.
One example use-case is to run each crop against a model.
Example:
Arguments:
-
filter_ontology_objects
- Specify a list of ontology objects to include. If provided, only instances of these object types are included. Strings are matched againstfeature_node_hashes
. -
Returns
- The dependency to be injected into the cloud function.
DEncordClient
Get an authenticated user client.
DObjectsInstances
Get all object instances that the agent was triggered on. No pixels, just the annotation.
DObjectCrops
Get all object crops that the agent was triggered on. The instance crop contains the object instance, the frame content (pixel values), and the frame.
DSingleFrame
Get the single frame that the agent was triggered on.
DAssetPath
Get a local file path to data asset temporarily stored till end of agent execution.
DVideoIterator
Get a video frame iterator for doing things over many frames.
DStorageItem
Get the storage item associated with the underlying agent task to, for example, read/write client metadata or read data properties.
DataLookup Objects
!!! warning “Deprecated”
DataLookup
is deprecated and will be removed in version 0.2.10.
Migration Guide:
- For accessing storage items, use
dep_storage_item
instead:
backing_item_uuids
Get all backing item uuids for all data rows in the data lookup.
!!! warning “Deprecated” This property is deprecated and will be removed in version 0.2.10. Use the EncordUserClient directly to access backing item UUIDs from label rows.
get_storage_item
!!! warning “Deprecated”
This method is deprecated and will be removed in version 0.2.10.
Use dep_storage_item
dependency instead.
Arguments:
data_hash
- Data hash for the asset for which you need the underlying storage item.dataset_hash
- If you didn’t provide the associated dataset hash in the constructor, this is your last chance.sign_url
- IfTrue
, pre-fetch a signed URLs for the items (otherwise the URLs will be signed on demand).
Raises:
ValueError
- Mainly if underlying data row cannot be found.
Returns:
The underlying storage item from which, e.g., client metadata can be updated.
get_storage_items
!!! warning “Deprecated” This method is deprecated and will be removed in version 0.2.10. Use the EncordUserClient directly for bulk storage item access.
Arguments:
data_hashes
- Data hashes for the assets for which you need the underlying storage items.dataset_hash
- If you didn’t provided the associated dataset hash in the constructor, this is your last chance.sign_urls
- IfTrue
, pre-fetch a signed URLs for the items (otherwise the URLs will be signed on demand).
Raises:
ValueError
- Mainly if underlying data row cannot be found.
Returns:
list of underlying storage items from which, e.g., client metadata can be updated.