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

Video bug fix #1547

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
- Preserve end_frame information of a video when it is zero.
(<https://github.com/openvinotoolkit/datumaro/pull/1541>)
- Changed the Datumaro format to ensure exported videos have relative paths and to prevent the same video from being overwritten.
(<https://github.com/openvinotoolkit/datumaro/pull/1547>)

## Q2 2024 Release 1.7.0
### New features
Expand Down
14 changes: 8 additions & 6 deletions src/datumaro/components/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,14 @@ def save_video(
path = osp.join(basedir, fname)
path = osp.abspath(path)

os.makedirs(osp.dirname(path), exist_ok=True)

if isinstance(item.media, VideoFrame):
item.media.video.save(path, crypter=NULL_CRYPTER)
else: # Video
item.media.save(path, crypter=NULL_CRYPTER)
# To prevent the video from being overwritten
# (A video can have same path but different start/end frames)
if not osp.exists(path):
os.makedirs(osp.dirname(path), exist_ok=True)
if isinstance(item.media, VideoFrame):
item.media.video.save(path, crypter=NULL_CRYPTER)
else: # Video
item.media.save(path, crypter=NULL_CRYPTER)

@property
def images_dir(self) -> str:
Expand Down
10 changes: 3 additions & 7 deletions src/datumaro/plugins/data_formats/datumaro/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,9 @@ def context_save_media(

if context.save_media:
fname = context.make_video_filename(item)
# To prevent the video from being overwritten
# (A video can have same path but different start/end frames)
if not osp.exists(fname):
context.save_video(item, fname=fname, subdir=item.subset)
context.save_video(item, fname=fname, subdir=item.subset)
item.media = Video(
path=video.path,
path=fname,
step=video._step,
start_frame=video._start_frame,
end_frame=video._end_frame,
Expand All @@ -202,8 +199,7 @@ def context_save_media(

if context.save_media:
fname = context.make_video_filename(item)
if not osp.exists(fname):
context.save_video(item, fname=fname, subdir=item.subset)
context.save_video(item, fname=fname, subdir=item.subset)
item.media = VideoFrame(Video(fname), video_frame.index)

yield
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/data_formats/datumaro/test_datumaro_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

import os
import os.path as osp
import shutil
import sys
from functools import partial
from unittest.mock import patch

import numpy as np
import pytest
Expand Down Expand Up @@ -152,6 +155,18 @@ def test_can_save_and_load(
stream=stream,
)

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
def test_export_video_only_once(
self,
fxt_test_datumaro_format_video_dataset: Dataset,
test_dir,
):
with patch(
"datumaro.components.media.shutil.copyfile", wraps=shutil.copyfile
) as mocked_save:
fxt_test_datumaro_format_video_dataset.export(test_dir, "datumaro", save_media=True)
assert mocked_save.call_count == 2 # train/video.avi, test/video.avi

@mark_requirement(Requirements.DATUM_GENERAL_REQ)
@pytest.mark.parametrize(
"fxt_dataset_pair, compare, require_media, dimension",
Expand Down
11 changes: 8 additions & 3 deletions tests/utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,13 @@ def _change_path_in_items(dataset, source_path, target_path):
for item in dataset:
if item.media:
if hasattr(item.media, "path") and item.media.path:
path = item.media.path.replace(source_path, target_path)
item.media = item.media.from_self(path=path)
if isinstance(item.media, VideoFrame):
path = (
item.media.video._path
) # _path includes the OS-specific directory separator
else:
path = item.media._path
item.media = item.media.from_self(path=path.replace(source_path, target_path))
if isinstance(item.media, PointCloud):
new_images = []
for image in item.media.extra_images:
Expand All @@ -346,7 +351,7 @@ def _change_path_in_items(dataset, source_path, target_path):
if move_save_dir:
save_dir = tmp_dir
for file in os.listdir(test_dir):
os.symlink(osp.join(test_dir, file), osp.join(save_dir, file))
shutil.move(osp.join(test_dir, file), save_dir)
else:
save_dir = test_dir

Expand Down
Loading