|
| 1 | +from datetime import datetime, timedelta |
1 | 2 | from pathlib import Path |
2 | 3 | from unittest.mock import Mock |
3 | 4 |
|
4 | 5 | import pytest |
5 | 6 |
|
6 | 7 | from OTVision.application.config import ( |
7 | 8 | Config, |
| 9 | + DetectConfig, |
8 | 10 | StreamConfig, |
9 | 11 | TrackConfig, |
| 12 | + YoloConfig, |
10 | 13 | _TrackIouConfig, |
11 | 14 | ) |
12 | 15 | from OTVision.application.config_parser import ConfigParser, InvalidOtvisionConfigError |
| 16 | +from OTVision.plugin.ffmpeg_video_writer import ( |
| 17 | + ConstantRateFactor, |
| 18 | + EncodingSpeed, |
| 19 | + VideoCodec, |
| 20 | +) |
| 21 | + |
| 22 | +# Ensure paths are translated to the respective platform (unix, windows) |
| 23 | +VIDEO_1 = str(Path("tests/data/video1.mp4")) |
| 24 | +VIDEO_2 = str(Path("tests/data/video2.mp4")) |
| 25 | + |
| 26 | + |
| 27 | +class TestConfigParser: |
| 28 | + @pytest.fixture |
| 29 | + def given_deserializer(self) -> Mock: |
| 30 | + return Mock() |
| 31 | + |
| 32 | + @pytest.fixture |
| 33 | + def given_config_parser(self, given_deserializer: Mock) -> ConfigParser: |
| 34 | + return ConfigParser(given_deserializer) |
| 35 | + |
| 36 | + def test_parse_detect_config_with_fully_specified_config( |
| 37 | + self, given_config_parser: ConfigParser |
| 38 | + ) -> None: |
| 39 | + detect_dict = { |
| 40 | + "PATHS": [VIDEO_1, VIDEO_2], |
| 41 | + "RUN_CHAINED": False, |
| 42 | + "YOLO": { |
| 43 | + "WEIGHTS": "yolov8m.pt", |
| 44 | + "CONF": 0.35, |
| 45 | + "IOU": 0.5, |
| 46 | + "IMGSIZE": 1280, |
| 47 | + "NORMALIZED": False, |
| 48 | + }, |
| 49 | + "EXPECTED_DURATION": 3600, |
| 50 | + "OVERWRITE": False, |
| 51 | + "HALF_PRECISION": True, |
| 52 | + "START_TIME": "2025-10-15_14-30-00", |
| 53 | + "DETECT_START": 10, |
| 54 | + "DETECT_END": 100, |
| 55 | + "WRITE_VIDEO": True, |
| 56 | + "VIDEO_CODEC": "h264_nvenc", |
| 57 | + "ENCODING_SPEED": "medium", |
| 58 | + "CRF": "HIGH_QUALITY", |
| 59 | + } |
| 60 | + |
| 61 | + result = given_config_parser.parse_detect_config(detect_dict) |
| 62 | + |
| 63 | + expected = DetectConfig( |
| 64 | + paths=[VIDEO_1, VIDEO_2], |
| 65 | + run_chained=False, |
| 66 | + yolo_config=YoloConfig( |
| 67 | + weights="yolov8m.pt", |
| 68 | + conf=0.35, |
| 69 | + iou=0.5, |
| 70 | + img_size=1280, |
| 71 | + normalized=False, |
| 72 | + ), |
| 73 | + expected_duration=timedelta(seconds=3600), |
| 74 | + overwrite=False, |
| 75 | + half_precision=True, |
| 76 | + start_time=datetime(2025, 10, 15, 14, 30, 0), |
| 77 | + detect_start=10, |
| 78 | + detect_end=100, |
| 79 | + write_video=True, |
| 80 | + video_codec=VideoCodec.H264_NVENC, |
| 81 | + encoding_speed=EncodingSpeed.MEDIUM, |
| 82 | + crf=ConstantRateFactor.HIGH_QUALITY, |
| 83 | + ) |
| 84 | + assert result == expected |
| 85 | + |
| 86 | + def test_parse_detect_config_with_empty_config( |
| 87 | + self, given_config_parser: ConfigParser |
| 88 | + ) -> None: |
| 89 | + detect_dict: dict = {} |
| 90 | + |
| 91 | + result = given_config_parser.parse_detect_config(detect_dict) |
| 92 | + |
| 93 | + expected = DetectConfig() |
| 94 | + assert result == expected |
13 | 95 |
|
14 | 96 |
|
15 | 97 | class TestConfigParserValidateFlushBufferSupportTrackLifecycle: |
|
0 commit comments