Core Reference
encord_agents.core.data_model
Frame
dataclass
A dataclass to hold the content of one frame in a video.
Source code in encord_agents/core/data_model.py
b64_encoding
b64_encoding(image_format: Literal['.jpeg', '.jpg', '.png'] = '.jpeg', output_format: Literal['url', 'openai', 'anthropic', 'raw'] = 'url') -> str | dict
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.
Parameters:
-
image_format
(Literal['.jpeg', '.jpg', '.png']
, default:'.jpeg'
) –Which type of image encoding to use.
-
output_format
(Literal['url', 'openai', 'anthropic', 'raw']
, default:'url'
) –Different common formats. -
raw
: the image content as a raw b64 string -url
: 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 on output_format
.
Source code in encord_agents/core/data_model.py
FrameData
Bases: BaseModel
Holds the data sent from the Encord Label Editor at the time of triggering the agent.
Source code in encord_agents/core/data_model.py
data_hash
class-attribute
instance-attribute
The identifier of the given data asset.
frame
class-attribute
instance-attribute
The frame number. If single image, it's default 0.
InstanceCrop
dataclass
Bases: Frame
A dataclass to hold the frame content of one object instance in a video or image.
Source code in encord_agents/core/data_model.py
encord_agents.core.dependencies
shares
DataLookup
Source code in encord_agents/core/dependencies/shares.py
backing_item_uuids
property
Get all backing item uuids for all data rows in the data lookup.
get_storage_item
get_storage_item(data_hash: str | UUID, dataset_hash: str | UUID | None = None, sign_url: bool = False) -> StorageItem
Parameters:
-
data_hash
(str | UUID
) –Data hash for the asset for which you need the underlying storage item.
-
dataset_hash
(str | UUID | None
, default:None
) –If you didn't provide the associated dataset hash in the constructor, this is your last chance.
-
sign_url
(bool
, default:False
) –If
True
, 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:
-
StorageItem
–The underlying storage item from which, e.g., client metadata can be updated.
Source code in encord_agents/core/dependencies/shares.py
get_storage_items
get_storage_items(data_hashes: list[str | UUID], dataset_hash: str | UUID | None = None, sign_urls: bool = False) -> list[StorageItem]
Parameters:
-
data_hashes
(list[str | UUID]
) –Data hashes for the assets for which you need the underlying storage items.
-
dataset_hash
(str | UUID | None
, default:None
) –If you didn't provide the associated dataset hash in the constructor, this is your last chance.
-
sign_urls
(bool
, default:False
) –If
True
, 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[StorageItem]
–list of underlying storage items from which, e.g., client metadata can be updated.
Source code in encord_agents/core/dependencies/shares.py
encord_agents.core.ontology
FieldType
module-attribute
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.
GenericFieldModel
Bases: BaseModel
Source code in encord_agents/core/ontology.py
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.
OntologyDataModel
Bases: Generic[OntologyType]
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 respone from an LLM.
Example:
from pydantic import ValidationError
classifications = project.ontology_structure.classifications
objects = project.ontology_structure.classifications
data_model = OntologyDataModel([objects])
# or
data_model = OntologyDataModel([classifications])
# Get a json schema for the ontology
print(data_model.model_json_schema_str)
# Parse json following the schema into label instances
json_str = my_favourite_llm(
f"what is this? pls follow {schema}", img
)
try:
instances = data_model(json_str)
except ValidationError:
# invalid json
...
for ins in instances:
label_row.add_classification_instance(ins)
label_row.save()
For a concrete example, please see
Attributes:
-
ontology
– -
DataModel
(BaseModel
) –
Source code in encord_agents/core/ontology.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
__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".
Parameters:
-
answer_str
–The json object as a raw string.
a list of classification / object instances that you will then
-
list[ClassificationInstance] | ObjectInstance
–have to add to a label row.
Source code in encord_agents/core/ontology.py
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".
Parameters:
-
answer_str
(str
) –The json object as a raw string.
a list of classification / object instances that you will then
-
list[ClassificationInstance] | ObjectInstance
–have to add to a label row.
Source code in encord_agents/core/ontology.py
encord_agents.core.settings
Settings used throughout the module.
Note that central settings will be read via environment variables.
Settings
Bases: BaseSettings
Source code in encord_agents/core/settings.py
ssh_key_content
class-attribute
instance-attribute
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.
ssh_key_file
class-attribute
instance-attribute
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.
encord_agents.core.utils
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:
with download_asset(lr, 10) as asset_path:
# In here the file exists
pixel_values = np.asarray(Image.open(asset_path))
# outside, it will be cleaned up
Parameters:
-
lr
(LabelRowV2
) –The label row for which you want to download the associated asset.
-
frame
(int | None
) –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:
-
Path
–The file path for the requested asset.
Source code in encord_agents/core/utils.py
get_initialised_label_row
Get an initialised label row from the frame_data information.
Parameters:
-
frame_data
(FrameData
) –The data pointing to the data asset.
Raises:
-
Exception
–If the
frame_data
cannot be matched to a label row
Returns:
-
LabelRowV2
–The initialized label row.
Source code in encord_agents/core/utils.py
get_user_client
cached
Generate an user client to access Encord.
Returns:
-
EncordUserClient
–An EncordUserClient authenticated with the credentials from the encord_agents.core.settings.Settings.
Source code in encord_agents/core/utils.py
encord_agents.core.video
get_frame
Extract an exact frame from a video.
Parameters:
-
video_path
(Path
) –The file path to where the video is stored.
-
desired_frame
(int
) –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:
-
NDArray[uint8]
–Numpy array of shape [h, w, c] where channels are RGB.
Source code in encord_agents/core/video.py
iter_video
Iterate video frame by frame.
Parameters:
-
video_path
(Path
) –The file path to the video you wish to iterate.
Raises:
-
Exception
–If the video file could not be opened properly.
Yields:
-
Frame
–Frames from the video.