Skip to content

Add option to SDK allowing upload of annotation as masks instead of polygon. #8823

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

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Added

- Added parameter `conv_mask_to_poly` support for importing annotations in projects,tasks and jobs
(<https://github.com/cvat-ai/cvat/pull/8823>)
2 changes: 2 additions & 0 deletions cvat-sdk/cvat_sdk/core/proxies/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def import_annotations(
format_name: str,
filename: StrPath,
*,
conv_mask_to_poly: Optional[bool] = None,
status_check_period: Optional[int] = None,
pbar: Optional[ProgressReporter] = None,
):
Expand All @@ -62,6 +63,7 @@ def import_annotations(
self.api.create_annotations_endpoint,
filename,
format_name,
conv_mask_to_poly=conv_mask_to_poly,
url_params={"id": self.id},
pbar=pbar,
status_check_period=status_check_period,
Expand Down
4 changes: 4 additions & 0 deletions cvat-sdk/cvat_sdk/core/proxies/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def import_dataset(
format_name: str,
filename: StrPath,
*,
conv_mask_to_poly: Optional[bool] = None,
status_check_period: Optional[int] = None,
pbar: Optional[ProgressReporter] = None,
):
Expand All @@ -63,6 +64,7 @@ def import_dataset(
filename,
format_name,
url_params={"id": self.id},
conv_mask_to_poly=conv_mask_to_poly,
pbar=pbar,
status_check_period=status_check_period,
)
Expand Down Expand Up @@ -110,6 +112,7 @@ def create_from_dataset(
dataset_format: str = "CVAT XML 1.1",
status_check_period: int = None,
pbar: Optional[ProgressReporter] = None,
conv_mask_to_poly: Optional[bool] = None,
) -> Project:
"""
Create a new project with the given name and labels JSON and
Expand All @@ -126,6 +129,7 @@ def create_from_dataset(
filename=dataset_path,
pbar=pbar,
status_check_period=status_check_period,
conv_mask_to_poly=conv_mask_to_poly,
)

project.fetch()
Expand Down
2 changes: 2 additions & 0 deletions cvat-sdk/cvat_sdk/core/proxies/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def import_annotations(
format_name: str,
filename: StrPath,
*,
conv_mask_to_poly: Optional[bool] = None,
status_check_period: Optional[int] = None,
pbar: Optional[ProgressReporter] = None,
):
Expand All @@ -179,6 +180,7 @@ def import_annotations(
filename,
format_name,
url_params={"id": self.id},
conv_mask_to_poly=conv_mask_to_poly,
pbar=pbar,
status_check_period=status_check_period,
)
Expand Down
9 changes: 9 additions & 0 deletions cvat-sdk/cvat_sdk/core/uploading.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def upload_file_and_wait(
filename: Path,
format_name: str,
*,
conv_mask_to_poly: Optional[bool] = None,
url_params: Optional[dict[str, Any]] = None,
pbar: Optional[ProgressReporter] = None,
status_check_period: Optional[int] = None,
Expand All @@ -267,6 +268,9 @@ def upload_file_and_wait(
url, filename, pbar=pbar, query_params=params, meta={"filename": params["filename"]}
)

if conv_mask_to_poly is not None:
params["conv_mask_to_poly"] = "true" if conv_mask_to_poly else "false"

rq_id = json.loads(response.data).get("rq_id")
assert rq_id, "The rq_id was not found in the response"

Expand All @@ -281,11 +285,16 @@ def upload_file_and_wait(
format_name: str,
*,
url_params: Optional[dict[str, Any]] = None,
conv_mask_to_poly: Optional[bool] = None,
pbar: Optional[ProgressReporter] = None,
status_check_period: Optional[int] = None,
):
url = self._client.api_map.make_endpoint_url(upload_endpoint.path, kwsub=url_params)
params = {"format": format_name, "filename": filename.name}

if conv_mask_to_poly is not None:
params["conv_mask_to_poly"] = "true" if conv_mask_to_poly else "false"

response = self.upload_file(
url, filename, pbar=pbar, query_params=params, meta={"filename": params["filename"]}
)
Expand Down
5 changes: 3 additions & 2 deletions cvat/apps/engine/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
import PIL.Image
import PIL.ImageOps
import rq
from rq.job import JobStatus as RQJobStatus
from django.conf import settings
from django.core.cache import caches
from django.db import models as django_models
from django.utils import timezone as django_tz
from redis.exceptions import LockError
from rest_framework.exceptions import NotFound, ValidationError
from rq.job import JobStatus as RQJobStatus

from cvat.apps.engine import models
from cvat.apps.engine.cloud_provider import (
Expand Down Expand Up @@ -91,7 +91,8 @@ def enqueue_create_chunk_job(
# Enqueue the job if the chunk was deleted but the RQ job still exists.
# This can happen in cases involving jobs with honeypots and
# if the job wasn't collected by the requesting process for any reason.
rq_job.get_status(refresh=False) in {RQJobStatus.FINISHED, RQJobStatus.FAILED, RQJobStatus.CANCELED}
rq_job.get_status(refresh=False)
in {RQJobStatus.FINISHED, RQJobStatus.FAILED, RQJobStatus.CANCELED}
):
rq_job = queue.enqueue(
create_callback,
Expand Down
36 changes: 36 additions & 0 deletions tests/python/sdk/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,42 @@ def fxt_login(admin_user: str, restore_db_per_class):
yield (client, user)


@pytest.fixture
def fxt_camvid_dataset(tmp_path: Path):
img_path = tmp_path / "img.png"
with img_path.open("wb") as f:
f.write(generate_image_file(filename=str(img_path), size=(5, 10)).getvalue())

annot_path = tmp_path / "annot.png"
r, g, b = (127, 0, 0)
annot = generate_image_file(
filename=str(annot_path),
size=(5, 10),
color=(r, g, b),
).getvalue()
with annot_path.open("wb") as f:
f.write(annot)

label_colors_path = tmp_path / "label_colors.txt"
with open(label_colors_path, "w") as f:
f.write(f"{r} {g} {b} car\n")

dataset_img_path = "default/img.png"
dataset_annot_path = "default/annot.png"
default_txt_path = tmp_path / "default.txt"
with open(default_txt_path, "w") as f:
f.write(f"/{dataset_img_path} {dataset_annot_path}")

dataset_path = tmp_path / "camvid_dataset.zip"
with ZipFile(dataset_path, "x") as f:
f.write(img_path, arcname=dataset_img_path)
f.write(annot_path, arcname=dataset_annot_path)
f.write(default_txt_path, arcname="default.txt")
f.write(label_colors_path, arcname="label_colors.txt")

yield dataset_path


@pytest.fixture
def fxt_coco_dataset(tmp_path: Path, fxt_image_file: Path, fxt_coco_file: Path):
dataset_path = tmp_path / "coco_dataset.zip"
Expand Down
23 changes: 23 additions & 0 deletions tests/python/sdk/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,29 @@ def test_can_download_frames(self, fxt_new_task: Task, quality: str, image_exten
assert (self.tmp_path / f"frame-0.{expected_frame_ext}").is_file()
assert self.stdout.getvalue() == ""

@pytest.mark.parametrize("convert", [True, False])
def test_can_convert_annotations_polygons_to_masks_param(
self, fxt_new_task: Task, fxt_camvid_dataset: Path, convert: bool
):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)

fxt_new_task.get_jobs()[0].import_annotations(
format_name="CamVid 1.0",
filename=fxt_camvid_dataset,
pbar=pbar,
conv_mask_to_poly=convert,
)

assert "uploaded" in self.logger_stream.getvalue()
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""

imported_annotations = fxt_new_task.get_jobs()[0].get_annotations()
assert all(
[s.type.value == "polygon" if convert else "mask" for s in imported_annotations.shapes]
)

def test_can_upload_annotations(self, fxt_new_task: Task, fxt_coco_file: Path):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
Expand Down
24 changes: 24 additions & 0 deletions tests/python/sdk/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,30 @@ def test_can_create_project_from_dataset(self, fxt_coco_dataset: Path):
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""

@pytest.mark.parametrize("convert", [True, False])
def test_can_create_project_from_dataset_with_polygons_to_masks_param(
self, fxt_camvid_dataset: Path, convert: bool
):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
project = self.client.projects.create_from_dataset(
spec=models.ProjectWriteRequest(name="project with data"),
dataset_path=fxt_camvid_dataset,
dataset_format="CamVid 1.0",
conv_mask_to_poly=convert,
pbar=pbar,
)

assert project.get_tasks()[0].size == 1
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""

task = project.get_tasks()[0]
imported_annotations = task.get_annotations()
assert all(
[s.type.value == "polygon" if convert else "mask" for s in imported_annotations.shapes]
)

def test_can_retrieve_project(self, fxt_new_project: Project):
project_id = fxt_new_project.id

Expand Down
17 changes: 15 additions & 2 deletions tests/python/sdk/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,16 +397,29 @@ def test_can_download_chunk(self, fxt_new_task: Task, quality: str):
assert len(chunk_zip.infolist()) == 1
assert self.stdout.getvalue() == ""

def test_can_upload_annotations(self, fxt_new_task: Task, fxt_coco_file: Path):
@pytest.mark.parametrize("convert", [True, False])
def test_can_upload_annotations(
self, fxt_new_task: Task, fxt_camvid_dataset: Path, convert: bool
):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)

fxt_new_task.import_annotations(format_name="COCO 1.0", filename=fxt_coco_file, pbar=pbar)
fxt_new_task.import_annotations(
format_name="CamVid 1.0",
filename=fxt_camvid_dataset,
conv_mask_to_poly=convert,
pbar=pbar,
)

assert "uploaded" in self.logger_stream.getvalue()
assert "100%" in pbar_out.getvalue().strip("\r").split("\r")[-1]
assert self.stdout.getvalue() == ""

imported_annotations = fxt_new_task.get_jobs()[0].get_annotations()
assert all(
[s.type.value == "polygon" if convert else "mask" for s in imported_annotations.shapes]
)

def _test_can_create_from_backup(self, fxt_new_task: Task, fxt_backup_file: Path):
pbar_out = io.StringIO()
pbar = make_pbar(file=pbar_out)
Expand Down
Loading