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

Expand group dataset metadata field for slice media types #4101

Merged
merged 9 commits into from
Mar 7, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 16 additions & 1 deletion fiftyone/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""

from collections import defaultdict
import contextlib
from datetime import datetime
Expand Down Expand Up @@ -6588,6 +6589,20 @@ def _expand_schema(self, samples, dynamic):
field_name, value.name, sample.media_type
)

if (
self.media_type == fom.GROUP
and sample.media_type
not in set(self.group_media_types.values())
):
expanded |= self._sample_doc_cls.merge_field_schema(
{
"metadata": fo.EmbeddedDocumentField(
fome.get_metadata_cls(sample.media_type)
)
},
validate=False,
)

if not dynamic and field_name in schema:
continue

Expand Down Expand Up @@ -8548,7 +8563,7 @@ def _finalize_frames(sample_collection, key_field, frame_key_field):


def _get_media_type(sample):
for field, value in sample.iter_fields():
for _, value in sample.iter_fields():
if isinstance(value, fog.Group):
return fom.GROUP

Expand Down
18 changes: 18 additions & 0 deletions fiftyone/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""

import itertools
import logging
import multiprocessing.dummy
Expand Down Expand Up @@ -229,6 +230,23 @@ def compute_sample_metadata(sample, overwrite=False, skip_failures=False):
sample.save()


def get_metadata_cls(media_type):
"""Get the ``metadata`` class for a media_type

Args:
media_type (str): a media type value

Returns:
a :class:`Metadata` class
"""
if media_type == fom.IMAGE:
return ImageMetadata
elif media_type == fom.VIDEO:
return VideoMetadata

return Metadata


def compute_metadata(
sample_collection,
overwrite=False,
Expand Down
32 changes: 32 additions & 0 deletions tests/unittests/group_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
| `voxel51.com <https://voxel51.com/>`_
|
"""

from itertools import groupby
import json
import os
Expand All @@ -21,6 +22,7 @@
import fiftyone.utils.data as foud
import fiftyone.utils.groups as foug
import fiftyone.core.media as fom
import fiftyone.core.metadata as fome
from fiftyone import ViewExpression as E, ViewField as F

from decorators import drop_datasets
Expand Down Expand Up @@ -2103,6 +2105,36 @@ def test_group_by_clips(self):
self.assertTrue(also_view.is_saved)
self.assertEqual(len(also_view), 2)

@drop_datasets
def test_expand_group_metadata(self):
dataset: fo.Dataset = fo.Dataset()

group = fo.Group()
samples = [
fo.Sample(filepath="video.mp4", group=group.element("video")),
fo.Sample(filepath="image.png", group=group.element("image")),
]
dataset.add_samples(samples)

# assert that slices have their media type metadata fields populated
for (
name,
field,
) in fome.ImageMetadata._fields.items(): # pylint: disable=no-member
self.assertIsInstance(
dataset.get_field(f"metadata.{name}", include_private=True),
field.__class__,
)

for (
name,
field,
) in fome.VideoMetadata._fields.items(): # pylint: disable=no-member
self.assertIsInstance(
dataset.get_field(f"metadata.{name}", include_private=True),
field.__class__,
)


def _make_group_by_dataset():
sample_id1 = ObjectId()
Expand Down
Loading