Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion scenedetect/_cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""

import logging
import math
import os
import os.path
import typing as ty
Expand Down Expand Up @@ -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:
Expand All @@ -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


Expand Down
39 changes: 20 additions & 19 deletions scenedetect/_cli/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
ConfigRegistry,
CropValue,
KernelSizeValue,
ScoreWeightsValue,
)
from scenedetect.backends import VideoStreamAv
from scenedetect.common import MAX_FPS_DELTA, FrameTimecode
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
),
Expand All @@ -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)
),
Expand Down
3 changes: 2 additions & 1 deletion scenedetect/detectors/adaptive_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
5 changes: 5 additions & 0 deletions scenedetect/detectors/content_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably reject any negative (or non-finite) values here as well if we are going to lock this down.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. ContentDetector and ScoreWeightsValue now also reject negative, NaN and inf weights, with tests for the detectors, the -w flag and the config file.

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:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
[
Expand Down
16 changes: 16 additions & 0 deletions tests/test_detectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
1 change: 1 addition & 0 deletions website/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading