From 14b1a9735a75c8b81db9c5ec7d10071c13b99457 Mon Sep 17 00:00:00 2001 From: Zhiltsov Max Date: Tue, 16 Jun 2020 21:26:27 +0300 Subject: [PATCH] Throw an error on unknown frames --- cvat/apps/dataset_manager/bindings.py | 8 +++- .../dataset_manager/tests/_test_formats.py | 39 +++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/cvat/apps/dataset_manager/bindings.py b/cvat/apps/dataset_manager/bindings.py index 7c8de445e458..e19e43902912 100644 --- a/cvat/apps/dataset_manager/bindings.py +++ b/cvat/apps/dataset_manager/bindings.py @@ -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): + raise ValueError("Unknown frame %s" % absolute_id) + return d def _init_frame_info(self): if hasattr(self._db_task.data, 'video'): diff --git a/cvat/apps/dataset_manager/tests/_test_formats.py b/cvat/apps/dataset_manager/tests/_test_formats.py index 6fdc9f3dca64..659b9c71c7bd 100644 --- a/cvat/apps/dataset_manager/tests/_test_formats.py +++ b/cvat/apps/dataset_manager/tests/_test_formats.py @@ -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() @@ -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))