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

Add utility to convert classifications to detections #1842

Merged
merged 2 commits into from
Jun 9, 2022
Merged
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
50 changes: 50 additions & 0 deletions fiftyone/utils/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,53 @@ def classification_to_detections(sample_collection, in_field, out_field):
image[out_field] = fol.Detections(detections=[detection])

sample.save()


def classifications_to_detections(sample_collection, in_field, out_field):
"""Converts the :class:`fiftyone.core.labels.Classifications` field of the
collection into a :class:`fiftyone.core.labels.Detections` field containing
detections whose bounding boxes span the entire image with one detection
for each classification.

Args:
sample_collection: a
:class:`fiftyone.core.collections.SampleCollection`
in_field: the name of the :class:`fiftyone.core.labels.Classifications`
field
out_field: the name of the :class:`fiftyone.core.labels.Detections`
field to populate
"""
fov.validate_collection_label_fields(
sample_collection, in_field, fol.Classifications
)

samples = sample_collection.select_fields(in_field)
in_field, processing_frames = samples._handle_frame_field(in_field)
out_field, _ = samples._handle_frame_field(out_field)

for sample in samples.iter_samples(progress=True):
if processing_frames:
images = sample.frames.values()
else:
images = [sample]

for image in images:
detections = []
classifications = image[in_field]
if classifications is None:
continue

for label in classifications.classifications:
if label is None:
continue

detection = fol.Detection(
label=label.label,
bounding_box=[0, 0, 1, 1], # entire image
confidence=label.confidence,
)
detections.append(detection)

image[out_field] = fol.Detections(detections=detections)

sample.save()