Skip to content

Commit

Permalink
E2E tests for converting annotations (#949)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWilkie authored Nov 4, 2024
1 parent 67e0866 commit 81dd1e6
Show file tree
Hide file tree
Showing 23 changed files with 1,300 additions and 18 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ data/
!tests/darwin/data
darwin_py.egg-info/PKG-INFO

*.png
*.jpeg
*.jpg
*.bpm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

import orjson as json
import pytest
import xml.etree.ElementTree as ET

from e2e_tests.helpers import assert_cli, run_cli_command


class TestExportCli:
this_file_path = Path(dirname(__file__)).absolute()
data_path = (this_file_path / ".." / ".." / "data").resolve()
data_path = (this_file_path / ".." / "data" / "convert").resolve()

@pytest.fixture(autouse=True)
def config(self) -> None:
Expand All @@ -33,10 +34,10 @@ def compare_directories(self, path: Path, expected_path: Path) -> None:
continue

# Compare files
with file.open("r") as f:
with file.open("rb") as f:
content = f.read()

with Path(expected_path / file.name).open() as f:
with Path(expected_path / file.name).open("rb") as f:
expected_content = f.read()

if content != expected_content:
Expand All @@ -52,6 +53,14 @@ def compare_directories(self, path: Path, expected_path: Path) -> None:
[
("yolo_segmented", data_path / "yolov8/from", data_path / "yolov8/to"),
("yolo", data_path / "yolo/from", data_path / "yolo/to"),
("cvat", data_path / "cvat/from", data_path / "cvat/to"),
("pascalvoc", data_path / "pascalvoc/from", data_path / "pascalvoc/to"),
("nifti", data_path / "nifti/from", data_path / "nifti/to"),
(
"instance_mask",
data_path / "instance_mask/from",
data_path / "instance_mask/to",
),
pytest.param(
"coco",
data_path / "coco/from",
Expand Down Expand Up @@ -87,30 +96,64 @@ def test_darwin_convert(
result = run_cli_command(
f"darwin convert {format} {str(input_path)} {str(tmp_path)}"
)
if format == "coco":
self.patch_coco(tmp_path / "output.json")
self.patch_format(format, tmp_path)
assert_cli(result, 0)
self.compare_directories(expectation_path, tmp_path)

def patch_format(self, format: str, path: Path) -> None:
"""
Patch files based on format to match the expected output.
"""
patch_methods = {
"coco": self.patch_coco,
"cvat": self.patch_cvat,
}
patch_method = patch_methods.get(format)
if patch_method:
patch_method(path)

def patch_coco(self, path: Path) -> None:
"""
Patch coco file to match the expected output, includes changes to year and date_created,
wrapped in try except so that format errors are still caught later with correct error messages
"""
try:
with open(path, "r") as f:
with open(path / "output.json", "r") as f:
contents = f.read()
temp = json.loads(contents)
temp["info"]["year"] = 2023
temp["info"]["date_created"] = "2023/12/05"
with open(path, "w") as f:
with open(path / "output.json", "w") as f:
op = json.dumps(
temp, option=json.OPT_INDENT_2 | json.OPT_SERIALIZE_NUMPY
).decode("utf-8")
f.write(op)
except Exception:
print(f"Error patching {path}")

def patch_cvat(self, path: Path) -> None:
"""
Patch cvat file to match the expected output.
"""
try:
tree = ET.parse(path / "output.xml")
root = tree.getroot()
# Adjust the required fields
dumped_elem = root.find(".//meta/dumped")
if dumped_elem is not None:
dumped_elem.text = "2024-10-25 10:33:01.789498+00:00"
created_elem = root.find(".//meta/task/created")
if created_elem is not None:
created_elem.text = "2024-10-25 10:33:01.789603+00:00"
updated_elem = root.find(".//meta/task/updated")
if updated_elem is not None:
updated_elem.text = "2024-10-25 10:33:01.789608+00:00"
tree.write(path / "output.xml")
except ET.ParseError:
print(f"Error parsing XML in {path}")
except Exception as e:
print(f"Error patching {path}: {e}")


if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
File renamed without changes.
67 changes: 67 additions & 0 deletions e2e_tests/data/convert/cvat/from/000000021295.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"version": "2.0",
"schema_ref": "https://darwin-public.s3.eu-west-1.amazonaws.com/darwin_json/2.0/schema.json",
"item": {
"name": "000000021295.jpg",
"path": "/",
"source_info": {
"item_id": "0192c338-2dc8-bb9f-8dde-9c4a8714c957",
"dataset": {
"name": "tmp",
"slug": "tmp",
"dataset_management_url": "https://staging.v7labs.com/datasets/426200/dataset-management"
},
"team": {
"name": "E2E Testing",
"slug": "e2e-testing"
},
"workview_url": "https://staging.v7labs.com/workview?dataset=426200&item=0192c338-2dc8-bb9f-8dde-9c4a8714c957"
},
"slots": [
{
"type": "image",
"slot_name": "0",
"width": 640,
"height": 427,
"thumbnail_url": "https://staging.v7labs.com/api/v2/teams/e2e-testing/files/6c554c31-8bf6-4fc7-9f31-394fd775b2a6/thumbnail",
"source_files": [
{
"file_name": "000000021295.jpg",
"url": "https://staging.v7labs.com/api/v2/teams/e2e-testing/uploads/ade5f1e3-a165-4a07-989f-86ee342499c9"
}
]
}
]
},
"annotations": [
{
"bounding_box": {
"h": 43.7181,
"w": 71.2665,
"x": 166.0884,
"y": 113.1879
},
"id": "46b33c9c-7453-4722-8b43-91bbb3fc247f",
"name": "test_bounding_box_basic",
"properties": [],
"slot_names": [
"0"
]
},
{
"bounding_box": {
"h": 31.7405,
"w": 58.0912,
"x": 360.1248,
"y": 259.913
},
"id": "2b7c85ed-74b3-4111-a5d6-073da56d0072",
"name": "test_bounding_box_basic",
"properties": [],
"slot_names": [
"0"
]
}
],
"properties": []
}
1 change: 1 addition & 0 deletions e2e_tests/data/convert/cvat/to/output.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<annotations><version>1.1</version><meta><dumped>2024-10-25 10:33:01.789498+00:00</dumped><task><id>1</id><name>exported_task_from_darwin</name><size>1</size><mode>annotation</mode><overlapp>0</overlapp><bugtracker>None</bugtracker><flipped>False</flipped><created>2024-10-25 10:33:01.789603+00:00</created><updated>2024-10-25 10:33:01.789608+00:00</updated><labels><label><name>test_bounding_box_basic</name><attributes /></label></labels><segments><segment><id>1</id><start>1</start><end>1</end><url>not applicable</url></segment></segments><owner><username>example_username</username><email>[email protected]</email></owner></task></meta><image id="1" name="000000021295.jpg" width="640" height="427"><box label="test_bounding_box_basic" xtl="166.088" ytl="113.188" xbr="237.35399999999998" ybr="156.906" occluded="0" /><box label="test_bounding_box_basic" xtl="360.125" ytl="259.913" xbr="418.216" ybr="291.654" occluded="0" /></image></annotations>
155 changes: 155 additions & 0 deletions e2e_tests/data/convert/instance_mask/from/000000021295.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
{
"version": "2.0",
"schema_ref": "https://darwin-public.s3.eu-west-1.amazonaws.com/darwin_json/2.0/schema.json",
"item": {
"name": "000000021295.jpg",
"path": "/",
"source_info": {
"item_id": "0192c338-2dc8-bb9f-8dde-9c4a8714c957",
"team": {
"name": "E2E Testing",
"slug": "e2e-testing"
},
"dataset": {
"name": "tmp",
"slug": "tmp",
"dataset_management_url": "https://staging.v7labs.com/datasets/426200/dataset-management"
},
"workview_url": "https://staging.v7labs.com/workview?dataset=426200&item=0192c338-2dc8-bb9f-8dde-9c4a8714c957"
},
"slots": [
{
"type": "image",
"slot_name": "0",
"width": 640,
"height": 427,
"thumbnail_url": "https://staging.v7labs.com/api/v2/teams/e2e-testing/files/6c554c31-8bf6-4fc7-9f31-394fd775b2a6/thumbnail",
"source_files": [
{
"file_name": "000000021295.jpg",
"url": "https://staging.v7labs.com/api/v2/teams/e2e-testing/uploads/ade5f1e3-a165-4a07-989f-86ee342499c9"
}
]
}
]
},
"annotations": [
{
"bounding_box": {
"h": 123.8166,
"w": 147.2414,
"x": 105.8307,
"y": 63.5815
},
"id": "4f23f7d2-2f83-45bc-927a-d03b9f642619",
"name": "test_polygon_basic",
"polygon": {
"paths": [
[
{
"x": 189.4906,
"y": 63.5815
},
{
"x": 105.8307,
"y": 182.0439
},
{
"x": 249.0564,
"y": 187.3981
},
{
"x": 196.1834,
"y": 105.0768
},
{
"x": 253.0721,
"y": 99.7226
}
]
]
},
"properties": [],
"slot_names": [
"0"
]
},
{
"bounding_box": {
"h": 99.05329999999998,
"w": 155.2727,
"x": 201.5376,
"y": 287.1207
},
"id": "b4248df6-5390-488a-9190-edfa397458a6",
"name": "test_polygon_basic",
"polygon": {
"paths": [
[
{
"x": 247.0486,
"y": 287.1207
},
{
"x": 201.5376,
"y": 386.174
},
{
"x": 293.8981,
"y": 386.174
},
{
"x": 356.8103,
"y": 384.1661
},
{
"x": 356.8103,
"y": 329.9545
},
{
"x": 319.3307,
"y": 316.569
}
]
]
},
"properties": [],
"slot_names": [
"0"
]
},
{
"bounding_box": {
"h": 163.3041,
"w": 171.33550000000002,
"x": 338.0705,
"y": 50.1959
},
"id": "5092f929-ec75-4eb9-8b56-e6bbc472e272",
"name": "test_polygon_basic",
"polygon": {
"paths": [
[
{
"x": 429.7618,
"y": 50.1959
},
{
"x": 338.0705,
"y": 212.8307
},
{
"x": 509.406,
"y": 213.5
}
]
]
},
"properties": [],
"slot_names": [
"0"
]
}
],
"properties": []
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 81dd1e6

Please sign in to comment.