diff --git a/scenedetect/_cli/config.py b/scenedetect/_cli/config.py index 794f8762..6dea3251 100644 --- a/scenedetect/_cli/config.py +++ b/scenedetect/_cli/config.py @@ -15,6 +15,7 @@ """ import logging +import math import os import os.path import typing as ty @@ -221,6 +222,10 @@ def __init__(self, value: str | ContentDetector.Components): if not len(values) == 4: raise ValueError("Score weights must be specified as four numbers!") self._value = ContentDetector.Components(*(float(val) for val in values)) + if not all(math.isfinite(weight) and weight >= 0.0 for weight in self._value): + raise ValueError("Weights must be finite and non-negative.") + if not any(self._value): + raise ValueError("At least one weight must be non-zero.") @property def value(self) -> ContentDetector.Components: @@ -237,7 +242,9 @@ def from_config(config_value: str, default: "ScoreWeightsValue") -> "ScoreWeight except ValueError as ex: raise OptionParseFailure( "Score weights must be specified as four numbers in the form (H,S,L,E)," - " e.g. (0.9, 0.2, 2.0, 0.5). Commas/brackets/slashes are ignored." + " e.g. (0.9, 0.2, 2.0, 0.5). Weights must be non-negative, with at least one" + " non-zero." + " Commas/brackets/slashes are ignored." ) from ex diff --git a/scenedetect/_cli/context.py b/scenedetect/_cli/context.py index 088be9fe..c367e08d 100644 --- a/scenedetect/_cli/context.py +++ b/scenedetect/_cli/context.py @@ -25,6 +25,7 @@ ConfigRegistry, CropValue, KernelSizeValue, + ScoreWeightsValue, ) from scenedetect.backends import VideoStreamAv from scenedetect.common import MAX_FPS_DELTA, FrameTimecode @@ -181,6 +182,19 @@ def _kernel_size_value(kernel_size: int | None) -> KernelSizeValue | None: "Kernel size must be an odd integer >= 3, or -1 for auto.", param_hint="kernel-size" ) from ex + @staticmethod + def _weights_value( + weights: tuple[float, float, float, float] | None, + ) -> ScoreWeightsValue | None: + """Validate `--weights` like the config file does, so invalid weights give a usage error + instead of NaN frame scores.""" + if weights is None: + return None + try: + return ScoreWeightsValue(ContentDetector.Components(*weights)) + except ValueError as ex: + raise click.BadParameter(str(ex), param_hint="weights") from ex + def parse_timecode(self, value: str | None, correct_pts: bool = False) -> FrameTimecode | None: """Parses a user input string into a FrameTimecode assuming the given framerate. If `value` is None it will be passed through without processing. @@ -388,17 +402,10 @@ def get_detect_content_params( """Get a dict containing user options to construct a ContentDetector with.""" min_scene_len_frames = self._resolve_min_scene_len("detect-content", min_scene_len) - if weights is not None: - try: - weights = ContentDetector.Components(*weights) - except ValueError as ex: - if DEBUG_MODE: - raise - logger.debug(str(ex)) - raise click.BadParameter(str(ex), param_hint="weights") from None - return { - "weights": self.config.get_value("detect-content", "weights", weights), + "weights": self.config.get_value( + "detect-content", "weights", self._weights_value(weights) + ), "kernel_size": self.config.get_value( "detect-content", "kernel-size", self._kernel_size_value(kernel_size) ), @@ -422,17 +429,11 @@ def get_detect_adaptive_params( min_scene_len_frames = self._resolve_min_scene_len("detect-adaptive", min_scene_len) - if weights is not None: - try: - weights = ContentDetector.Components(*weights) - except ValueError as ex: - if DEBUG_MODE: - raise - logger.debug(str(ex)) - raise click.BadParameter(str(ex), param_hint="weights") from None return { "adaptive_threshold": self.config.get_value("detect-adaptive", "threshold", threshold), - "weights": self.config.get_value("detect-adaptive", "weights", weights), + "weights": self.config.get_value( + "detect-adaptive", "weights", self._weights_value(weights) + ), "kernel_size": self.config.get_value( "detect-adaptive", "kernel-size", self._kernel_size_value(kernel_size) ), diff --git a/scenedetect/detectors/adaptive_detector.py b/scenedetect/detectors/adaptive_detector.py index 3b65ba73..655cdf51 100644 --- a/scenedetect/detectors/adaptive_detector.py +++ b/scenedetect/detectors/adaptive_detector.py @@ -58,7 +58,8 @@ def __init__( calculates frame score based on `weights`/`luma_only`/`kernel_size`. weights: Weight to place on each component when calculating frame score (`content_val` in a statsfile, the value `threshold` is compared against). - If omitted, the default ContentDetector weights are used. + If omitted, the default ContentDetector weights are used. Each weight must be + finite and non-negative, and at least one must be non-zero. luma_only: If True, only considers changes in the luminance channel of the video. Equivalent to specifying `weights` as :attr:`~scenedetect.detectors.content_detector.ContentDetector.LUMA_ONLY_WEIGHTS`. diff --git a/scenedetect/detectors/content_detector.py b/scenedetect/detectors/content_detector.py index 5592fd1e..6ad39045 100644 --- a/scenedetect/detectors/content_detector.py +++ b/scenedetect/detectors/content_detector.py @@ -118,6 +118,7 @@ def __init__( str (e.g. ``"0.6s"``, ``"00:00:00.600"``). weights: Weight to place on each component when calculating frame score (`content_val` in a statsfile, the value `threshold` is compared against). + Each weight must be finite and non-negative, and at least one must be non-zero. luma_only: If True, only considers changes in the luminance channel of the video. Equivalent to specifying `weights` as :attr:`~scenedetect.detectors.content_detector.ContentDetector.LUMA_ONLY_WEIGHTS`. @@ -132,6 +133,10 @@ def __init__( self._weights: ContentDetector.Components = weights if luma_only: self._weights = ContentDetector.LUMA_ONLY_WEIGHTS + if not all(math.isfinite(weight) and weight >= 0.0 for weight in self._weights): + raise ValueError("Weights must be finite and non-negative.") + if not any(self._weights): + raise ValueError("At least one weight must be non-zero.") self._kernel: numpy.ndarray | None = None if kernel_size is not None: if kernel_size < 3 or kernel_size % 2 == 0: diff --git a/tests/test_cli.py b/tests/test_cli.py index 4f570820..e333b7a7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -463,6 +463,45 @@ def test_cli_kernel_size_config_file_rejects_one(tmp_path: Path, command: str): assert any("odd integer >= 3" in message for _, message in ex.value.init_log) +@pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) +def test_cli_weights_rejects_all_zero(command: str): + """All-zero weights must give a usage error instead of NaN frame scores (#612).""" + exit_code, output = invoke_cli( + ["-i", DEFAULT_VIDEO_PATH, command, "-w", "0", "0", "0", "0", "time", "-e", "1"] + ) + assert exit_code != 0 + assert "At least one weight must be non-zero." in output + + +@pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) +def test_cli_weights_config_file_rejects_all_zero(tmp_path: Path, command: str): + """A config file with all-zero weights must be rejected when loading (#612).""" + config_path = tmp_path.joinpath("config.cfg") + config_path.write_text(f"[{command}]\nweights = 0, 0, 0, 0\n") + with pytest.raises(ConfigLoadFailure) as ex: + ConfigRegistry(str(config_path)) + assert any("non-zero" in message for _, message in ex.value.init_log) + + +@pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) +@pytest.mark.parametrize("bad_weight", ["-1", "nan", "inf"]) +def test_cli_weights_rejects_negative_or_non_finite(command: str, bad_weight: str): + exit_code, output = invoke_cli( + ["-i", DEFAULT_VIDEO_PATH, command, "-w", "1", "1", bad_weight, "0", "time", "-e", "1"] + ) + assert exit_code != 0 + assert "Weights must be finite and non-negative." in output + + +@pytest.mark.parametrize("command", ["detect-content", "detect-adaptive"]) +def test_cli_weights_config_file_rejects_negative(tmp_path: Path, command: str): + config_path = tmp_path.joinpath("config.cfg") + config_path.write_text(f"[{command}]\nweights = 1, 1, -1, 0\n") + with pytest.raises(ConfigLoadFailure) as ex: + ConfigRegistry(str(config_path)) + assert any("non-negative" in message for _, message in ex.value.init_log) + + @pytest.mark.parametrize( "cli_args", [ diff --git a/tests/test_detectors.py b/tests/test_detectors.py index 6754d910..102eb178 100644 --- a/tests/test_detectors.py +++ b/tests/test_detectors.py @@ -292,3 +292,19 @@ def test_adaptive_detector_min_scene_len_uses_target_frame(): cut_frames = [cut.frame_num for cut in cuts] assert cut_frames == [100, 140] + + +@pytest.mark.parametrize("detector_type", [ContentDetector, AdaptiveDetector]) +def test_detector_rejects_all_zero_weights(detector_type): + """All-zero weights would make every frame score NaN (#612).""" + with pytest.raises(ValueError, match="At least one weight must be non-zero"): + detector_type(weights=ContentDetector.Components(0.0, 0.0, 0.0, 0.0)) + # luma_only overrides weights, so this is still valid. + detector_type(weights=ContentDetector.Components(0.0, 0.0, 0.0, 0.0), luma_only=True) + + +@pytest.mark.parametrize("detector_type", [ContentDetector, AdaptiveDetector]) +@pytest.mark.parametrize("bad_weight", [-1.0, float("nan"), float("inf")]) +def test_detector_rejects_negative_or_non_finite_weights(detector_type, bad_weight: float): + with pytest.raises(ValueError, match="Weights must be finite and non-negative"): + detector_type(weights=ContentDetector.Components(1.0, 1.0, bad_weight, 0.0)) diff --git a/website/pages/changelog.md b/website/pages/changelog.md index bb086d23..09c92ca2 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -864,3 +864,4 @@ Development - [bugfix] The CLI now exits with 1 when no frames could be read from the input video, or seeking to the `time -s`/`--start` time fails, instead of 0 - [bugfix] `--kernel-size`/`-k` values the detectors cannot use (even numbers, 1, 0 and negatives other than -1) now give a usage error instead of a traceback, and `-k -1` now means auto, like the config file. A config file with `kernel-size = 1` is now an error [#604](https://github.com/Breakthrough/PySceneDetect/issues/604) - [bugfix] Out of range values for `detect-adaptive --frame-window` (below 1) and `save-images` `--num-images` (below 1), `--scale`, `--height` and `--width` (negative) now give a usage error instead of a traceback, and the same values are rejected in a config file [#609](https://github.com/Breakthrough/PySceneDetect/issues/609) + - [bugfix] `detect-content` / `detect-adaptive` `--weights` set to all zeros, or with a negative or non-finite weight (also in a config file), now give an error instead of NaN frame scores and no detected scenes, and `ContentDetector` / `AdaptiveDetector` raise a `ValueError` for them [#612](https://github.com/Breakthrough/PySceneDetect/issues/612)