|
| 1 | +from datetime import timedelta |
| 2 | +from pathlib import Path |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from OTVision import dataformat, version |
| 8 | +from OTVision.detect.otdet import OtdetBuilder, OtdetBuilderConfig, OtdetBuilderError |
| 9 | +from OTVision.track.preprocess import Detection |
| 10 | + |
| 11 | + |
| 12 | +def create_expected_video_metadata(config: OtdetBuilderConfig) -> dict: |
| 13 | + """Create the expected video metadata based on the given config.""" |
| 14 | + video_metadata = { |
| 15 | + dataformat.FILENAME: str(config.video.stem), |
| 16 | + dataformat.FILETYPE: str(config.video.suffix), |
| 17 | + dataformat.WIDTH: config.video_width, |
| 18 | + dataformat.HEIGHT: config.video_height, |
| 19 | + dataformat.RECORDED_FPS: config.recorded_fps, |
| 20 | + dataformat.ACTUAL_FPS: config.actual_fps, |
| 21 | + dataformat.NUMBER_OF_FRAMES: config.actual_frames, |
| 22 | + } |
| 23 | + if config.expected_duration: |
| 24 | + video_metadata[dataformat.EXPECTED_DURATION] = int( |
| 25 | + config.expected_duration.total_seconds() |
| 26 | + ) |
| 27 | + return video_metadata |
| 28 | + |
| 29 | + |
| 30 | +def create_expected_detection_metadata(config: OtdetBuilderConfig) -> dict: |
| 31 | + """Create the expected detection metadata based on the given config.""" |
| 32 | + return { |
| 33 | + dataformat.OTVISION_VERSION: version.otvision_version(), |
| 34 | + dataformat.MODEL: { |
| 35 | + dataformat.NAME: "YOLOv8", |
| 36 | + dataformat.WEIGHTS: str(config.detection_model), |
| 37 | + dataformat.IOU_THRESHOLD: config.iou, |
| 38 | + dataformat.IMAGE_SIZE: config.detection_img_size, |
| 39 | + dataformat.MAX_CONFIDENCE: config.conf, |
| 40 | + dataformat.HALF_PRECISION: config.half_precision, |
| 41 | + dataformat.CLASSES: config.classifications, |
| 42 | + }, |
| 43 | + dataformat.CHUNKSIZE: config.chunksize, |
| 44 | + dataformat.NORMALIZED_BBOX: config.normalized, |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | +def create_expected_metadata(config: OtdetBuilderConfig) -> dict: |
| 49 | + """Create the full expected metadata based on the given config.""" |
| 50 | + return { |
| 51 | + dataformat.OTDET_VERSION: version.otdet_version(), |
| 52 | + dataformat.VIDEO: create_expected_video_metadata(config), |
| 53 | + dataformat.DETECTION: create_expected_detection_metadata(config), |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | +def create_expected_data( |
| 58 | + detections: list[list[Detection]], |
| 59 | +) -> dict: |
| 60 | + """Create the expected data dictionary based on input detections.""" |
| 61 | + data = {} |
| 62 | + for frame, detection_list in enumerate(detections, start=1): |
| 63 | + data[str(frame)] = { |
| 64 | + dataformat.DETECTIONS: [d.to_otdet() for d in detection_list] |
| 65 | + } |
| 66 | + return data |
| 67 | + |
| 68 | + |
| 69 | +class TestOtdetBuilder: |
| 70 | + @pytest.fixture |
| 71 | + def builder(self) -> OtdetBuilder: |
| 72 | + """Fixture to provide a new instance of OtdetBuilder for every test.""" |
| 73 | + return OtdetBuilder() |
| 74 | + |
| 75 | + @pytest.fixture |
| 76 | + def config(self) -> OtdetBuilderConfig: |
| 77 | + """Fixture to provide a predefined valid OtdetBuilderConfig.""" |
| 78 | + return OtdetBuilderConfig( |
| 79 | + conf=0.5, |
| 80 | + iou=0.4, |
| 81 | + video=Path("video.mp4"), |
| 82 | + video_width=1920, |
| 83 | + video_height=1080, |
| 84 | + expected_duration=timedelta(seconds=300), |
| 85 | + recorded_fps=30.0, |
| 86 | + actual_fps=29.97, |
| 87 | + actual_frames=1000, |
| 88 | + detection_img_size=640, |
| 89 | + normalized=True, |
| 90 | + detection_model=Path("model.pt"), |
| 91 | + half_precision=False, |
| 92 | + chunksize=32, |
| 93 | + classifications={0: "person", 1: "car"}, |
| 94 | + ) |
| 95 | + |
| 96 | + @pytest.fixture |
| 97 | + def mock_detection(self) -> MagicMock: |
| 98 | + """Fixture to provide a mocked Detection object.""" |
| 99 | + detection: MagicMock = MagicMock(spec=Detection) |
| 100 | + detection.to_otdet.return_value = { |
| 101 | + dataformat.CLASS: "person", |
| 102 | + dataformat.CONFIDENCE: 0.9, |
| 103 | + } |
| 104 | + return detection |
| 105 | + |
| 106 | + def test_builder_without_config_raises_error(self, builder: OtdetBuilder) -> None: |
| 107 | + """Test that accessing config without setting it raises an error. |
| 108 | +
|
| 109 | + #Requirement https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 110 | +
|
| 111 | + """ # noqa |
| 112 | + with pytest.raises(OtdetBuilderError, match="Otdet builder config is not set"): |
| 113 | + _ = builder.config |
| 114 | + |
| 115 | + def test_builder_add_config_successfully( |
| 116 | + self, builder: OtdetBuilder, config: OtdetBuilderConfig |
| 117 | + ) -> None: |
| 118 | + """Test that configuration can be added to the builder. |
| 119 | +
|
| 120 | + #Requirement https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 121 | +
|
| 122 | + """ # noqa |
| 123 | + actual = builder.add_config(config) |
| 124 | + assert builder.config == config |
| 125 | + assert actual == builder |
| 126 | + |
| 127 | + def test_build_metadata( |
| 128 | + self, builder: OtdetBuilder, config: OtdetBuilderConfig |
| 129 | + ) -> None: |
| 130 | + """Test the metadata generation. |
| 131 | +
|
| 132 | + https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 133 | +
|
| 134 | + """ # noqa |
| 135 | + builder.add_config(config) |
| 136 | + actual = builder._build_metadata() |
| 137 | + |
| 138 | + expected = create_expected_metadata(config) |
| 139 | + assert actual == expected |
| 140 | + |
| 141 | + def test_build_data( |
| 142 | + self, |
| 143 | + builder: OtdetBuilder, |
| 144 | + config: OtdetBuilderConfig, |
| 145 | + mock_detection: MagicMock, |
| 146 | + ) -> None: |
| 147 | + """Test the data generation with detection objects. |
| 148 | +
|
| 149 | + https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 150 | +
|
| 151 | + """ # noqa |
| 152 | + builder.add_config(config) |
| 153 | + |
| 154 | + # Provide mock detections as input |
| 155 | + given: list[list[Detection]] = [[mock_detection] * 2, [mock_detection]] |
| 156 | + actual = builder._build_data(given) |
| 157 | + |
| 158 | + expected = create_expected_data(given) |
| 159 | + assert actual == expected |
| 160 | + |
| 161 | + def test_build_full_result( |
| 162 | + self, |
| 163 | + builder: OtdetBuilder, |
| 164 | + config: OtdetBuilderConfig, |
| 165 | + mock_detection: MagicMock, |
| 166 | + ) -> None: |
| 167 | + """Test the full build method. |
| 168 | +
|
| 169 | +
|
| 170 | + https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 171 | + """ # noqa |
| 172 | + builder.add_config(config) |
| 173 | + |
| 174 | + # Provide mock detections |
| 175 | + given: list[list[Detection]] = [[mock_detection] * 3] |
| 176 | + actual = builder.build(given) |
| 177 | + |
| 178 | + expected_metadata = create_expected_metadata(config) |
| 179 | + expected_data = create_expected_data(given) |
| 180 | + expected = { |
| 181 | + dataformat.METADATA: expected_metadata, |
| 182 | + dataformat.DATA: expected_data, |
| 183 | + } |
| 184 | + assert actual == expected |
| 185 | + |
| 186 | + def test_reset_builder( |
| 187 | + self, builder: OtdetBuilder, config: OtdetBuilderConfig |
| 188 | + ) -> None: |
| 189 | + """Test that the builder is reset after building. |
| 190 | +
|
| 191 | + https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 192 | +
|
| 193 | + """ # noqa |
| 194 | + builder.add_config(config) |
| 195 | + assert builder.config == config |
| 196 | + |
| 197 | + actual = builder.reset() |
| 198 | + assert actual == builder |
| 199 | + assert builder._config is None |
| 200 | + |
| 201 | + def test_empty_detections_builds_valid_data( |
| 202 | + self, builder: OtdetBuilder, config: OtdetBuilderConfig |
| 203 | + ) -> None: |
| 204 | + """Test that an empty detection list builds valid data. |
| 205 | +
|
| 206 | + https://openproject.platomo.de/projects/001-opentrafficcam-live/work_packages/7188 |
| 207 | +
|
| 208 | + """ # noqa |
| 209 | + builder.add_config(config) |
| 210 | + actual = builder.build([]) |
| 211 | + |
| 212 | + expected = { |
| 213 | + dataformat.METADATA: create_expected_metadata(config), |
| 214 | + dataformat.DATA: {}, |
| 215 | + } |
| 216 | + |
| 217 | + assert actual == expected |
0 commit comments