Repository navigation
[bugfix] Reject all-zero --weights instead of producing NaN frame scores - #613
EnesSoydan wants to merge 2 commits into
Conversation
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
8bd5806 to
8c7184c
Compare
Breakthrough
left a comment
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
We should probably reject any negative (or non-finite) values here as well if we are going to lock this down.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
| if weights is None: | ||
| return None | ||
| try: | ||
| return ScoreWeightsValue(ContentDetector.Components(*weights)).value |
There was a problem hiding this comment.
Can this return ScoreWeightsValue directly? get_value should handle "unwrapping" it via the .value accessor.
There was a problem hiding this comment.
Yes, it now returns the ScoreWeightsValue and get_value unwraps it.
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.
Fixes #612
With all four weights set to 0,
ContentDetectordivides by the sum of the weights, so every frame score is NaN anddetect-content/detect-adaptivefinish without finding any scenes. The only sign of it is a numpyRuntimeWarning.Changes:
ContentDetector.__init__raisesValueError("At least one weight must be non-zero."), next to the existingkernel_sizecheck. This also coversAdaptiveDetector.luma_only=Truestill works, since it replaces the weights before the check.ScoreWeightsValuerejects all-zero weights, so a config file withweights = 0, 0, 0, 0fails to load with the existing weights error message (now mentioning the non-zero requirement).-w/--weightsgoes throughScoreWeightsValuevia a_weights_valuehelper (same approach as_kernel_size_valuefrom [bugfix] Reject invalid --kernel-size with a usage error instead of a traceback (#604) #605), so it gives a usage error:The old
try/except ValueErroraroundContentDetector.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 intests/test_detectors.py. Changelog entry added.