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

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
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,8 @@
### Added <!-- pick one -->

Added parameter `conv_mask_to_poly` to the following SDK methods to control mask-to-polygon conversion:
- `project.export_dataset`
- `project.import_dataset`
- `project.create_from_dataset`
- `DatasetUploader.upload_file_and_wait`
([#8823](https://github.com/cvat-ai/cvat/pull/8823))
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
6 changes: 6 additions & 0 deletions cvat-sdk/cvat_sdk/core/uploading.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,17 @@ 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:
value = "true" if conv_mask_to_poly else "false"
params["conv_mask_to_poly"] = value

response = self.upload_file(
url, filename, pbar=pbar, query_params=params, meta={"filename": params["filename"]}
)
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} ROI\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
26 changes: 26 additions & 0 deletions tests/python/sdk/test_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,32 @@ 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() == ""
Copy link
Contributor

@zhiltsov-max zhiltsov-max Jan 10, 2025

Choose a reason for hiding this comment

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

Suggested change
assert self.stdout.getvalue() == ""
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])

Consider doing something like this to check if it works as intended. You will need to upload a COCO dataset with an RLE-encoded annotation, the current one has just one bbox inside.


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