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 relative mask_path and make paths valid for used OS (#3257) #3704

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions fiftyone/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,41 @@ def safe_relpath(path, start=None, default=None):
return relpath


def expand_mask_paths(sample, rel_path):
"""Finds all `mask_path` keys inside a serialized sample recursively and expand relative paths.
It is assumed that `mask_path` can only exist in `dict`.

Args:
sample: a serialized sample
rel_path: part of path to make relative paths absolute
"""
if isinstance(sample, dict):
for key, value in sample.items():
if key == "mask_path" and not os.path.isabs(key):
expanded = os.path.join(rel_path, value)
sample[key] = make_path_os_safe(expanded)
elif isinstance(value, (dict, list, tuple)):
expand_mask_paths(value, rel_path)
elif isinstance(sample, (list, tuple)):
for item in sample:
expand_mask_paths(item, rel_path)


def make_path_os_safe(path):
"""Convert path delimiters depending on OS.

Args:
path: path to make OS safe

Returns:
valid path
"""
if sys.platform == "win32":
return path.replace("/", "\\")
else:
return path.replace("\\", "/")


def compute_filehash(filepath, method=None, chunk_size=None):
"""Computes the hash of the given file.

Expand Down
5 changes: 5 additions & 0 deletions fiftyone/utils/data/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,7 @@ def _import_samples(self, dataset, dataset_dict, tags=None):
def _parse_sample(sd):
if not os.path.isabs(sd["filepath"]):
sd["filepath"] = os.path.join(rel_dir, sd["filepath"])
sd["filepath"] = fou.make_path_os_safe(sd["filepath"])
Copy link
Contributor

@brimoor brimoor Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually have a fiftyone.core.storage.normpath() utility that does this. Can you use that instead?


if tags is not None:
sd["tags"].extend(tags)
Expand All @@ -1856,6 +1857,10 @@ def _parse_sample(sd):
_parse_media_fields(sd, media_fields, rel_dir)

sd["_dataset_id"] = dataset_id

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _parse_media_fields() method call on line 1857 is intended to handle this for you. I'd like to understand a bit more about your use case to see why it's not working for you 🤔

# Find all mask_path keys recursively and expand relative paths
fou.expand_mask_paths(sd, rel_dir)

return sd

sample_ids = foo.insert_documents(
Expand Down
Loading