diff --git a/e2e_tests/cli/convert/test_convert.py b/e2e_tests/cli/convert/test_convert.py index 206f82dcd..eddab8dbc 100644 --- a/e2e_tests/cli/convert/test_convert.py +++ b/e2e_tests/cli/convert/test_convert.py @@ -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 @@ -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 """ @@ -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 @@ -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( @@ -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__]) diff --git a/e2e_tests/data/coco/from/base_annotation.json b/e2e_tests/data/coco/from/base_annotation.json new file mode 100644 index 000000000..b6b6e2d3c --- /dev/null +++ b/e2e_tests/data/coco/from/base_annotation.json @@ -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 + } + ] + ] + } + } + ] +} \ No newline at end of file diff --git a/e2e_tests/data/coco/to/output.json b/e2e_tests/data/coco/to/output.json new file mode 100644 index 000000000..69e009d94 --- /dev/null +++ b/e2e_tests/data/coco/to/output.json @@ -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": [] +} \ No newline at end of file