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

Throw an error on unknown frames #1728

Merged
merged 1 commit into from
Jun 17, 2020
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
8 changes: 7 additions & 1 deletion cvat/apps/dataset_manager/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ def _get_immutable_attribute_id(self, label_id, attribute_name):
return self._get_attribute_id(label_id, attribute_name, 'immutable')

def abs_frame_id(self, relative_id):
if relative_id not in range(0, self._db_task.data.size):
raise ValueError("Unknown internal frame id %s" % relative_id)
return relative_id * self._frame_step + self._db_task.data.start_frame

def rel_frame_id(self, absolute_id):
return (absolute_id - self._db_task.data.start_frame) // self._frame_step
d, m = divmod(
absolute_id - self._db_task.data.start_frame, self._frame_step)
if m or d not in range(0, self._db_task.data.size):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can m or not(0 <= d < self._db_task.data.size) condition be used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It can, but I'm not a big fan of it. We also could write abs_id in range(start_frame, stop_frame, frame_step).

raise ValueError("Unknown frame %s" % absolute_id)
return d

def _init_frame_info(self):
if hasattr(self._db_task.data, 'video'):
Expand Down
39 changes: 39 additions & 0 deletions cvat/apps/dataset_manager/tests/_test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def _setUpModule():

_setUpModule()

from cvat.apps.dataset_manager.annotation import AnnotationIR
from cvat.apps.dataset_manager.bindings import TaskData
from cvat.apps.engine.models import Task


def generate_image_file(filename, size=(100, 50)):
f = BytesIO()
Expand Down Expand Up @@ -397,3 +401,38 @@ def load_dataset(src):
self.assertEqual(len(dataset), task["size"])
self._test_export(check, task, format_name, save_images=False)

def test_cant_make_rel_frame_id_from_unknown(self):
images = self._generate_task_images(3)
images['frame_filter'] = 'step=2'
task = self._generate_task(images)
task_data = TaskData(AnnotationIR(), Task.objects.get(pk=task['id']))

with self.assertRaisesRegex(ValueError, r'Unknown'):
task_data.rel_frame_id(1) # the task has only 0 and 2 frames

def test_can_make_rel_frame_id_from_known(self):
images = self._generate_task_images(6)
images['frame_filter'] = 'step=2'
images['start_frame'] = 1
task = self._generate_task(images)
task_data = TaskData(AnnotationIR(), Task.objects.get(pk=task['id']))

self.assertEqual(2, task_data.rel_frame_id(5))

def test_cant_make_abs_frame_id_from_unknown(self):
images = self._generate_task_images(3)
images['frame_filter'] = 'step=2'
task = self._generate_task(images)
task_data = TaskData(AnnotationIR(), Task.objects.get(pk=task['id']))

with self.assertRaisesRegex(ValueError, r'Unknown'):
task_data.abs_frame_id(2) # the task has only 0 and 1 indices

def test_can_make_abs_frame_id_from_known(self):
images = self._generate_task_images(6)
images['frame_filter'] = 'step=2'
images['start_frame'] = 1
task = self._generate_task(images)
task_data = TaskData(AnnotationIR(), Task.objects.get(pk=task['id']))

self.assertEqual(5, task_data.abs_frame_id(2))