Skip to content

Commit

Permalink
[PY-546] Coco e2e test (#739)
Browse files Browse the repository at this point in the history
* coco test

* changes to test asserts

* changes for coco specific

* removing useless imports

---------

Co-authored-by: saurbhc <[email protected]>
  • Loading branch information
Nathanjp91 and saurbhc authored Dec 7, 2023
1 parent 9967a56 commit 0c38b9c
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 10 deletions.
49 changes: 39 additions & 10 deletions e2e_tests/cli/convert/test_convert.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import sys
from os.path import dirname
from pathlib import Path
from typing import Tuple

import orjson as json
import pytest

from e2e_tests.helpers import assert_cli, run_cli_command
Expand All @@ -15,7 +16,7 @@ class TestExportCli:
def config(self) -> None:
assert self.data_path.exists(), "Data path does not exist, tests cannot run"

def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, str]:
def compare_directories(self, path: Path, expected_path: Path) -> None:
"""
Compare two directories recursively
"""
Expand All @@ -25,9 +26,7 @@ def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, st
for file in path.iterdir():
if file.is_dir():
# Recursively compare directories
result = self.compare_directories(file, expected_path / file.name)
if not result[0]:
return result
self.compare_directories(file, expected_path / file.name)
else:
if file.name.startswith("."):
# Ignore hidden files
Expand All @@ -41,15 +40,27 @@ def compare_directories(self, path: Path, expected_path: Path) -> Tuple[bool, st
expected_content = f.read()

if content != expected_content:
return (False, f"File {file} does not match expected file")

return (True, "")
print(f"Expected file: {expected_path / file.name}")
print(f"Expected Content: \n{expected_content}")
print("---------------------")
print(f"Actual file: {file}")
print(f"Actual Content: \n{content}")
assert False, f"File {file} does not match expected file"

@pytest.mark.parametrize(
"format, input_path, expectation_path",
[
("yolo_segmented", data_path / "yolov8/from", data_path / "yolov8/to"),
("yolo", data_path / "yolo/from", data_path / "yolo/to"),
pytest.param(
"coco",
data_path / "coco/from",
data_path / "coco/to",
marks=pytest.mark.skipif(
sys.platform == "win32",
reason="File paths are different on Windows, leading to test failure",
),
),
],
)
def test_darwin_convert(
Expand All @@ -71,10 +82,28 @@ 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")
assert_cli(result, 0)
assert self.compare_directories(expectation_path, tmp_path)[0]
self.compare_directories(expectation_path, tmp_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:
contents = f.read()
temp = json.loads(contents)
temp["info"]["year"] = 2023
temp["info"]["date_created"] = "2023/12/05"
with open(path, "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}")

if __name__ == "__main__":
pytest.main(["-vv", "-s", __file__])
67 changes: 67 additions & 0 deletions e2e_tests/data/coco/from/base_annotation.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": "",
"path": "/"
},
"annotations": [
{
"bounding_box": {
"h": 1.0,
"w": 1.0,
"x": 0.0,
"y": 0.0
},
"id": "007882ff-99c4-4c6f-b71b-79cfc147fef6",
"name": "test_bb"
},
{
"ellipse": {
"angle": 0.0,
"center": {
"x": 1.0,
"y": 1.0
},
"radius": {
"x": 1.0,
"y": 1.0
}
},
"id": "320a60f2-643b-4d74-a117-0ea2fdfe7a61",
"name": "test_ellipse"
},
{
"bounding_box": {
"h": 1.0,
"w": 1.0,
"x": 0.0,
"y": 0.0
},
"id": "012dcc6c-5b77-406b-8cd7-d9567c8b00b7",
"name": "test_poly",
"polygon": {
"paths": [
[
{
"x": 0.0,
"y": 0.0
},
{
"x": 1.0,
"y": 0.0
},
{
"x": 1.0,
"y": 1.0
},
{
"x": 0.0,
"y": 1.0
}
]
]
}
}
]
}
99 changes: 99 additions & 0 deletions e2e_tests/data/coco/to/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"info": {
"description": "Exported from Darwin",
"url": "n/a",
"version": "n/a",
"year": 2023,
"contributor": "n/a",
"date_created": "2023/12/05"
},
"licenses": [
{
"url": "n/a",
"id": 0,
"name": "placeholder license"
}
],
"images": [
{
"license": 0,
"file_name": "",
"coco_url": "n/a",
"height": null,
"width": null,
"date_captured": "",
"flickr_url": "n/a",
"darwin_url": null,
"darwin_workview_url": null,
"id": 2043925204,
"tag_ids": []
}
],
"annotations": [
{
"id": 1,
"image_id": 2043925204,
"category_id": 348813479,
"segmentation": [
[
0.0,
0.0,
1.0,
0.0,
1.0,
1.0,
0.0,
1.0
]
],
"area": 1.0,
"bbox": [
0.0,
0.0,
1.0,
1.0
],
"iscrowd": 0,
"extra": {}
},
{
"id": 3,
"image_id": 2043925204,
"category_id": 3961009249,
"segmentation": [
[
0.0,
0.0,
1.0,
0.0,
1.0,
1.0,
0.0,
1.0
]
],
"area": 1.0,
"bbox": [
0.0,
0.0,
1.0,
1.0
],
"iscrowd": 0,
"extra": {}
}
],
"categories": [
{
"id": 3961009249,
"name": "test_poly",
"supercategory": "root"
},
{
"id": 348813479,
"name": "test_bb",
"supercategory": "root"
}
],
"tag_categories": []
}

0 comments on commit 0c38b9c

Please sign in to comment.