Skip to content

Commit

Permalink
ignore deleted frames when converting tracks to shapes (#8834)
Browse files Browse the repository at this point in the history
<!-- Raise an issue to propose your change
(https://github.com/cvat-ai/cvat/issues).
It helps to avoid duplication of efforts from multiple independent
contributors.
Discuss your ideas with maintainers to be sure that changes will be
approved and merged.
Read the [Contribution guide](https://docs.cvat.ai/docs/contributing/).
-->

<!-- Provide a general summary of your changes in the Title above -->

### Motivation and context
<!-- Why is this change required? What problem does it solve? If it
fixes an open
issue, please link to the issue here. Describe your changes in detail,
add
screenshots. -->
When a frame which contains a keyframe of a track is deleted, the
keyframe continues to exist. When a dataset is exported (in all formats
except CVAT for video), tracks are interpolated and keyframe from
deleted frame is not ignored.
Fixing it.

### How has this been tested?
<!-- Please describe in detail how you tested your changes.
Include details of your testing environment, and the tests you ran to
see how your change affects other areas of the code, etc. -->

### Checklist
<!-- Go over all the following points, and put an `x` in all the boxes
that apply.
If an item isn't applicable for some reason, then ~~explicitly
strikethrough~~ the whole
line. If you don't do that, GitHub will show incorrect progress for the
pull request.
If you're unsure about any of these, don't hesitate to ask. We're here
to help! -->
- [ ] I submit my changes into the `develop` branch
- [ ] I have created a changelog fragment <!-- see top comment in
CHANGELOG.md -->
- [ ] I have updated the documentation accordingly
- [ ] I have added tests to cover my changes
- [ ] I have linked related issues (see [GitHub docs](

https://help.github.com/en/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword))
- [ ] I have increased versions of npm packages if it is necessary

([cvat-canvas](https://github.com/cvat-ai/cvat/tree/develop/cvat-canvas#versioning),

[cvat-core](https://github.com/cvat-ai/cvat/tree/develop/cvat-core#versioning),

[cvat-data](https://github.com/cvat-ai/cvat/tree/develop/cvat-data#versioning)
and

[cvat-ui](https://github.com/cvat-ai/cvat/tree/develop/cvat-ui#versioning))

### License

- [ ] I submit _my code changes_ under the same [MIT License](
https://github.com/cvat-ai/cvat/blob/develop/LICENSE) that covers the
project.
  Feel free to contact the maintainers if that's a concern.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced shape management by allowing exclusion of shapes based on
deleted frames in both `AnnotationManager` and `TrackManager`.
- Introduced a method for deleting specified frames from job data and
verifying the integrity of subsequent annotations in tests.

- **Bug Fixes**
- Improved handling of deleted frames by transitioning from a dictionary
to a set for better performance and clarity.

- **Tests**
- Added tests to ensure frame deletion functionality works as intended
without affecting subsequent annotations.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
Eldies authored Dec 18, 2024
1 parent c99f8a4 commit 851fb7d
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Fixed

- Fixed handling of tracks keyframes from deleted frames on export
(<https://github.com/cvat-ai/cvat/pull/8834>)
3 changes: 3 additions & 0 deletions cvat/apps/dataset_manager/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ def _unite_objects(obj0, obj1):
def _modify_unmatched_object(self, obj, end_frame):
pass


class TrackManager(ObjectManager):
def to_shapes(self, end_frame: int, *,
included_frames: Optional[Sequence[int]] = None,
Expand Down Expand Up @@ -929,6 +930,8 @@ def propagate(shape, end_frame, *, included_frames=None):
prev_shape = None
for shape in sorted(track["shapes"], key=lambda shape: shape["frame"]):
curr_frame = shape["frame"]
if included_frames is not None and curr_frame not in included_frames:
continue
if prev_shape and end_frame <= curr_frame:
# If we exceed the end_frame and there was a previous shape,
# we still need to interpolate up to the next keyframe,
Expand Down
69 changes: 69 additions & 0 deletions cvat/apps/dataset_manager/tests/test_formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
get_paginated_collection, ForceLogin, generate_image_file, ApiTestBase
)


class _DbTestBase(ApiTestBase):
def setUp(self):
super().setUp()
Expand Down Expand Up @@ -87,6 +88,7 @@ def _create_task(self, data, image_data):

return task


class TaskExportTest(_DbTestBase):
def _generate_custom_annotations(self, annotations, task):
self._put_api_v2_task_id_annotations(task["id"], annotations)
Expand Down Expand Up @@ -520,6 +522,72 @@ def test_frames_outside_are_not_generated(self):
self.assertTrue(frame.frame in range(6, 10))
self.assertEqual(i + 1, 4)

def _delete_job_frames(self, job_id: int, deleted_frames: list[int]):
with ForceLogin(self.user, self.client):
response = self.client.patch(
f"/api/jobs/{job_id}/data/meta?org=",
data=dict(deleted_frames=deleted_frames),
format="json"
)
assert response.status_code == status.HTTP_200_OK, response.status_code

def test_track_keyframes_on_deleted_frames_do_not_affect_later_frames(self):
images = self._generate_task_images(4)
task = self._generate_task(images)
job = self._get_task_jobs(task["id"])[0]

annotations = {
"version": 0,
"tags": [],
"shapes": [],
"tracks": [
{
"frame": 0,
"label_id": task["labels"][0]["id"],
"group": None,
"source": "manual",
"attributes": [],
"shapes": [
{
"frame": 0,
"points": [1, 2, 3, 4],
"type": "rectangle",
"occluded": False,
"outside": False,
"attributes": [],
},
{
"frame": 1,
"points": [5, 6, 7, 8],
"type": "rectangle",
"occluded": False,
"outside": True,
"attributes": [],
},
{
"frame": 2,
"points": [9, 10, 11, 12],
"type": "rectangle",
"occluded": False,
"outside": False,
"attributes": [],
},
]
},
]
}
self._put_api_v2_job_id_annotations(job["id"], annotations)
self._delete_job_frames(job["id"], [2])

task_ann = TaskAnnotation(task["id"])
task_ann.init_from_db()
task_data = TaskData(task_ann.ir_data, Task.objects.get(pk=task["id"]))
extractor = CvatTaskOrJobDataExtractor(task_data)
dm_dataset = Dataset.from_extractors(extractor)

assert len(dm_dataset.get("image_3").annotations) == 0


class FrameMatchingTest(_DbTestBase):
def _generate_task_images(self, paths): # pylint: disable=no-self-use
f = BytesIO()
Expand Down Expand Up @@ -612,6 +680,7 @@ def test_dataset_root(self):
root = find_dataset_root(dataset, task_data)
self.assertEqual(expected, root)


class TaskAnnotationsImportTest(_DbTestBase):
def _generate_custom_annotations(self, annotations, task):
self._put_api_v2_task_id_annotations(task["id"], annotations)
Expand Down

0 comments on commit 851fb7d

Please sign in to comment.