Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC-3689 COCO Exporter #602

Merged
merged 2 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions encord/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,45 @@ def import_coco_labels(
branch_name=branch_name,
)

def export_coco_labels(
self,
label_hashes: Optional[List[str]] = None,
include_object_feature_hashes: Optional[Set[str]] = None,
include_classification_feature_hashes: Optional[Set[str]] = None,
branch_name: Optional[str] = None,
) -> Dict[str, Any]:
"""Export labels from the project to the COCO format.
This method requires the 'coco' extra to be installed. Install it using:
`pip install encord[coco]`.
Args:
label_hashes: List of label hashes to include. If not provided, all label rows will be included.
include_object_feature_hashes: If `None`, all objects will be included.
Otherwise, only objects with the specified feature hashes will be included.
include_classification_feature_hashes: If `None`, all classifications will be included.
Otherwise, only classifications with the specified feature hashes will be included.
branch_name: Optionally specify a branch name. Defaults to the `main` branch.
Returns:
Dict[str, Any]: A dictionary in the COCO format containing the exported labels,
including annotations and metadata conforming to COCO standards.
The dictionary also includes additional fields specific to Encord,
providing supplementary information not defined in the COCO standard.
Raises:
ImportError: If the 'coco' extra dependencies are not installed.
"""
from encord.utilities.coco.exporter import CocoExporter

label_rows = self.list_label_rows_v2(label_hashes=label_hashes, branch_name=branch_name)
with self.create_bundle() as bundle:
for row in label_rows:
row.initialise_labels(
include_object_feature_hashes=include_object_feature_hashes,
include_classification_feature_hashes=include_classification_feature_hashes,
bundle=bundle,
)
labels = [row.to_encord_dict() for row in label_rows]
coco_labels = CocoExporter(labels, ontology=self.ontology_structure).export()
return coco_labels

def get_collection(self, collection_uuid: Union[str, UUID]) -> ProjectCollection:
return ProjectCollection._get_collection(
project_client=self._client,
Expand Down
Loading
Loading