-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: detach uai BoundingBox2d tests from .json file
- Loading branch information
Showing
2 changed files
with
123 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 116 additions & 33 deletions
149
tests/test_raillabel/format/understand_ai/test_uai_bounding_box_2d.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,131 @@ | ||
# Copyright DB Netz AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import os | ||
import sys | ||
from pathlib import Path | ||
from uuid import UUID | ||
|
||
import pytest | ||
|
||
sys.path.insert(1, str(Path(__file__).parent.parent.parent.parent)) | ||
sys.path.insert(1, str(Path(__file__).parent.parent.parent.parent.parent)) | ||
|
||
import raillabel.format.understand_ai as uai_format | ||
from raillabel.format.understand_ai._translation import translate_class_id, translate_sensor_id | ||
|
||
# == Fixtures ========================= | ||
|
||
@pytest.fixture | ||
def bounding_box_uai_dict(sensor_camera_uai_dict, attributes_uai_dict) -> dict: | ||
return { | ||
"id": "2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965", | ||
"objectId": "48c988bd-76f1-423f-b46d-7e7acb859f31", | ||
"className": "test_class", | ||
"geometry": { | ||
"xMin": 1, | ||
"yMin": 2, | ||
"xMax": 3, | ||
"yMax": 4 | ||
}, | ||
"attributes": attributes_uai_dict, | ||
"sensor": sensor_camera_uai_dict | ||
} | ||
|
||
@pytest.fixture | ||
def bounding_box_uai(attributes_uai, sensor_camera_uai) -> dict: | ||
return uai_format.BoundingBox2d( | ||
id=UUID("2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965"), | ||
object_id=UUID("48c988bd-76f1-423f-b46d-7e7acb859f31"), | ||
class_name="test_class", | ||
x_min=1, | ||
y_min=2, | ||
x_max=3, | ||
y_max=4, | ||
attributes=attributes_uai, | ||
sensor=sensor_camera_uai, | ||
) | ||
|
||
@pytest.fixture | ||
def bounding_box_raillabel_dict(attributes_raillabel_dict, sensor_camera_uai) -> dict: | ||
return { | ||
"name": "2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965", | ||
"val": [ | ||
2.0, | ||
3.0, | ||
2.0, | ||
2.0 | ||
], | ||
"coordinate_system": sensor_camera_uai.type, | ||
"attributes": attributes_raillabel_dict, | ||
} | ||
|
||
|
||
# == Tests ============================ | ||
|
||
def test_fromdict( | ||
attributes_uai_dict, attributes_uai, | ||
sensor_camera_uai_dict, sensor_camera_uai | ||
): | ||
bounding_box_2d = uai_format.BoundingBox2d.fromdict( | ||
{ | ||
"id": "2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965", | ||
"objectId": "48c988bd-76f1-423f-b46d-7e7acb859f31", | ||
"className": "test_class", | ||
"geometry": { | ||
"xMin": 1, | ||
"yMin": 2, | ||
"xMax": 3, | ||
"yMax": 4 | ||
}, | ||
"attributes": attributes_uai_dict, | ||
"sensor": sensor_camera_uai_dict | ||
} | ||
) | ||
|
||
assert bounding_box_2d.id == UUID("2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965") | ||
assert bounding_box_2d.object_id == UUID("48c988bd-76f1-423f-b46d-7e7acb859f31") | ||
assert bounding_box_2d.class_name == "test_class" | ||
assert bounding_box_2d.x_min == 1 | ||
assert bounding_box_2d.y_min == 2 | ||
assert bounding_box_2d.x_max == 3 | ||
assert bounding_box_2d.y_max == 4 | ||
assert bounding_box_2d.attributes == attributes_uai | ||
assert bounding_box_2d.sensor == sensor_camera_uai | ||
|
||
|
||
def test_to_raillabel( | ||
attributes_uai, attributes_raillabel_dict, | ||
sensor_camera_uai, sensor_camera_raillabel_dict, | ||
): | ||
bounding_box_2d = uai_format.BoundingBox2d( | ||
id=UUID("2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965"), | ||
object_id=UUID("48c988bd-76f1-423f-b46d-7e7acb859f31"), | ||
class_name="test_class", | ||
x_min=1, | ||
y_min=2, | ||
x_max=3, | ||
y_max=4, | ||
attributes=attributes_uai, | ||
sensor=sensor_camera_uai, | ||
) | ||
|
||
data_dict, object_id, translated_class_id, sensor_reference = bounding_box_2d.to_raillabel() | ||
|
||
assert data_dict == { | ||
"name": "2f2a1d7f-56d1-435c-a3ec-d6b8fdaaa965", | ||
"val": [ | ||
2.0, | ||
3.0, | ||
2.0, | ||
2.0 | ||
], | ||
"coordinate_system": sensor_camera_uai.type, | ||
"attributes": attributes_raillabel_dict, | ||
} | ||
assert object_id == str(bounding_box_2d.object_id) | ||
assert translated_class_id == translate_class_id(bounding_box_2d.class_name) | ||
assert sensor_reference == sensor_camera_raillabel_dict | ||
|
||
def test_fromdict(json_data): | ||
input_data = json_data["_understand_ai_t4_format/bounding_box_2d"] | ||
input_data["sensor"] = json_data["_understand_ai_t4_format/sensor_reference_camera"] | ||
bounding_box = uai_format.BoundingBox2d.fromdict(input_data) | ||
|
||
assert bounding_box.id == UUID(input_data["id"]) | ||
assert bounding_box.object_id == UUID(input_data["objectId"]) | ||
assert bounding_box.class_name == input_data["className"] | ||
assert bounding_box.x_min == input_data["geometry"]["xMin"] | ||
assert bounding_box.y_min == input_data["geometry"]["yMin"] | ||
assert bounding_box.x_max == input_data["geometry"]["xMax"] | ||
assert bounding_box.y_max == input_data["geometry"]["yMax"] | ||
assert bounding_box.attributes == input_data["attributes"] | ||
assert bounding_box.sensor == uai_format.SensorReference.fromdict(input_data["sensor"]) | ||
|
||
def test_to_raillabel(json_data): | ||
input_data = json_data["_understand_ai_t4_format/bounding_box_2d"] | ||
input_data["sensor"] = json_data["_understand_ai_t4_format/sensor_reference_camera"] | ||
bounding_box = uai_format.BoundingBox2d.fromdict(input_data) | ||
output_data, object_id, class_name, sensor_ref = bounding_box.to_raillabel() | ||
ground_truth = json_data["_understand_ai_t4_format/bounding_box_2d_raillabel"] | ||
|
||
assert output_data["name"] == ground_truth["name"] | ||
assert output_data["val"] == ground_truth["val"] | ||
assert output_data["attributes"] == ground_truth["attributes"] | ||
assert object_id == input_data["objectId"] | ||
assert class_name == input_data["className"] | ||
assert sensor_ref == bounding_box.sensor.to_raillabel()[1] | ||
|
||
# Executes the test if the file is called | ||
if __name__ == "__main__": | ||
import os | ||
os.system("clear") | ||
pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear"]) | ||
pytest.main([__file__, "--disable-pytest-warnings", "--cache-clear", "-v"]) |