Skip to content

Commit

Permalink
Throw an error on unknown frames (cvat-ai#1728)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiltsov-max authored and Fernando Martínez González committed Aug 3, 2020
1 parent fb485af commit d3bb54e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cvat/apps/dataset_manager/bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,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'):
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))

0 comments on commit d3bb54e

Please sign in to comment.