Skip to content

[bugfix] Reject all-zero --weights instead of producing NaN frame scores - #613

Open
EnesSoydan wants to merge 2 commits into
Breakthrough:mainfrom
EnesSoydan:fix/612-zero-weights
Open

EnesSoydan wants to merge 2 commits into
Breakthrough:mainfrom
EnesSoydan:fix/612-zero-weights

Conversation

@EnesSoydan

Copy link
Copy Markdown
Contributor

Fixes #612

With all four weights set to 0, ContentDetector divides by the sum of the weights, so every frame score is NaN and detect-content / detect-adaptive finish without finding any scenes. The only sign of it is a numpy RuntimeWarning.

Changes:

  • ContentDetector.__init__ raises ValueError("At least one weight must be non-zero."), next to the existing kernel_size check. This also covers AdaptiveDetector. luma_only=True still works, since it replaces the weights before the check.
  • ScoreWeightsValue rejects all-zero weights, so a config file with weights = 0, 0, 0, 0 fails to load with the existing weights error message (now mentioning the non-zero requirement).
  • -w/--weights goes through ScoreWeightsValue via a _weights_value helper (same approach as _kernel_size_value from [bugfix] Reject invalid --kernel-size with a usage error instead of a traceback (#604) #605), so it gives a usage error:
Error: Invalid value for weights: At least one weight must be non-zero.

The old try/except ValueError around ContentDetector.Components(*weights) is replaced by the helper. Building the named tuple from four floats couldn't raise there, so behavior for valid weights is unchanged.

Tests: CLI and config file cases for both commands in tests/test_cli.py, and a constructor test for both detectors in tests/test_detectors.py. Changelog entry added.

All-zero weights made ContentDetector divide by zero for every frame, so
detect-content/detect-adaptive ran without finding any scenes. Reject them in
ContentDetector, and give a usage error for -w and a config file error.

Fixes Breakthrough#612

@Breakthrough Breakthrough left a comment

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.

Thanks for the PR! Just a few minor comments.

self._weights: ContentDetector.Components = weights
if luma_only:
self._weights = ContentDetector.LUMA_ONLY_WEIGHTS
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.

min_scene_len: Once a cut is detected, this much time must pass before a new one can
be added to the scene list. Accepts an int (frames), float (seconds), or
str (e.g. ``"0.6s"``, ``"00:00:00.600"``).
weights: Weight to place on each component when calculating frame score

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.

Might be worth updating the docstr here to explicitly call out now that at least one weight must be non-zero now. (the same will need to be done for AdaptiveDetector)

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.

Updated the weights docs on both ContentDetector and AdaptiveDetector: each weight must be finite and non-negative, and at least one must be non-zero.

Comment thread scenedetect/_cli/context.py Outdated
if weights is None:
return None
try:
return ScoreWeightsValue(ContentDetector.Components(*weights)).value

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.

Can this return ScoreWeightsValue directly? get_value should handle "unwrapping" it via the .value accessor.

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.

Yes, it now returns the ScoreWeightsValue and get_value unwraps it.

@Breakthrough Breakthrough added this to the 0.7.4 milestone Nov 18, 2026
Address review: weights must be finite and non-negative, documented on
ContentDetector and AdaptiveDetector. _weights_value returns the
ScoreWeightsValue and lets get_value unwrap it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

detect-content / detect-adaptive with all weights set to 0 silently produce NaN scores and detect nothing

2 participants