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

Fixing #2302 #2304

Merged
merged 2 commits into from
Nov 14, 2022
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
16 changes: 14 additions & 2 deletions fiftyone/utils/data/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def add_labeled_images(
label_field=None,
tags=None,
expand_schema=True,
dynamic=False,
):
"""Adds the given labeled images to the dataset.

Expand Down Expand Up @@ -125,6 +126,8 @@ def add_labeled_images(
expand_schema (True): whether to dynamically add new sample fields
encountered to the dataset schema. If False, an error is raised
if a sample's schema is not a subset of the dataset schema
dynamic (False): whether to declare dynamic attributes of embedded
document fields that are encountered

Returns:
a list of IDs of the samples that were added to the dataset
Expand Down Expand Up @@ -196,7 +199,10 @@ def parse_sample(sample):

_samples = map(parse_sample, samples)
return dataset.add_samples(
_samples, expand_schema=expand_schema, num_samples=num_samples
_samples,
expand_schema=expand_schema,
dynamic=dynamic,
num_samples=num_samples,
)


Expand Down Expand Up @@ -266,6 +272,7 @@ def add_labeled_videos(
label_field=None,
tags=None,
expand_schema=True,
dynamic=False,
):
"""Adds the given labeled videos to the dataset.

Expand Down Expand Up @@ -295,6 +302,8 @@ def add_labeled_videos(
expand_schema (True): whether to dynamically add new sample fields
encountered to the dataset schema. If False, an error is raised
if a sample's schema is not a subset of the dataset schema
dynamic (False): whether to declare dynamic attributes of embedded
document fields that are encountered

Returns:
a list of IDs of the samples that were added to the dataset
Expand Down Expand Up @@ -364,7 +373,10 @@ def parse_sample(sample):

_samples = map(parse_sample, samples)
return dataset.add_samples(
_samples, expand_schema=expand_schema, num_samples=num_samples
_samples,
expand_schema=expand_schema,
dynamic=dynamic,
num_samples=num_samples,
)


Expand Down
116 changes: 116 additions & 0 deletions tests/unittests/dataset_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from fiftyone import ViewField as F
import fiftyone.core.fields as fof
import fiftyone.core.odm as foo
import fiftyone.utils.data as foud

from decorators import drop_datasets, skip_windows

Expand Down Expand Up @@ -3783,6 +3784,121 @@ class _LabelMetadata(foo.DynamicEmbeddedDocument):
model_name = fof.StringField()


class DatasetFactoryTests(unittest.TestCase):
@drop_datasets
def test_from_images(self):
filepaths = ["image.jpg"]
dataset = fo.Dataset.from_images(filepaths)

self.assertEqual(len(dataset), 1)

samples = [{"filepath": "image.jpg"}]
sample_parser = _ImageSampleParser()
dataset = fo.Dataset.from_images(samples, sample_parser=sample_parser)

self.assertEqual(len(dataset), 1)

@drop_datasets
def test_from_videos(self):
filepaths = ["image.jpg"]
dataset = fo.Dataset.from_videos(filepaths)

self.assertEqual(len(dataset), 1)

samples = [{"filepath": "video.mp4"}]
sample_parser = _VideoSampleParser()
dataset = fo.Dataset.from_videos(samples, sample_parser=sample_parser)

self.assertEqual(len(dataset), 1)

@drop_datasets
def test_from_labeled_images(self):
samples = [{"filepath": "image.jpg", "label": "label"}]
sample_parser = _LabeledImageSampleParser()
dataset = fo.Dataset.from_labeled_images(
samples, sample_parser, label_field="ground_truth"
)

self.assertEqual(dataset.values("ground_truth.label"), ["label"])

@drop_datasets
def test_from_labeled_videos(self):
samples = [{"filepath": "video.mp4", "label": "label"}]
sample_parser = _LabeledVideoSampleParser()
dataset = fo.Dataset.from_labeled_videos(
samples, sample_parser, label_field="ground_truth"
)

self.assertEqual(dataset.values("ground_truth.label"), ["label"])


class _ImageSampleParser(foud.ImageSampleParser):
@property
def has_image_path(self):
return True

@property
def has_image_metadata(self):
return False

def get_image_path(self):
return self.current_sample["filepath"]


class _VideoSampleParser(foud.VideoSampleParser):
@property
def has_video_metadata(self):
return False

def get_video_path(self):
return self.current_sample["filepath"]


class _LabeledImageSampleParser(foud.LabeledImageSampleParser):
@property
def has_image_path(self):
return True

@property
def has_image_metadata(self):
return False

def get_image_path(self):
return self.current_sample["filepath"]

@property
def label_cls(self):
return fo.Classification

def get_label(self):
label = self.current_sample["label"]
return fo.Classification(label=label)


class _LabeledVideoSampleParser(foud.LabeledVideoSampleParser):
@property
def has_video_metadata(self):
return False

def get_video_path(self):
return self.current_sample["filepath"]

@property
def label_cls(self):
return fo.Classification

@property
def frame_label_cls(self):
return None

def get_label(self):
label = self.current_sample["label"]
return fo.Classification(label=label)

def get_frame_labels(self):
return None


if __name__ == "__main__":
fo.config.show_progress_bars = False
unittest.main(verbosity=2)