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
7 changes: 4 additions & 3 deletions scenedetect/scene_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,8 @@ def detect_scenes(
logger.debug(
f"Crop set: top left = {self._crop[0:2]}, bottom right = {self._crop[2:4]}"
)
x0, y0, x1, y1 = self._crop
# Use the public (inclusive) coordinates, `_crop` is stored one-past-the-end.
x0, y0, x1, y1 = self.crop
min_x, min_y = (min(x0, x1), min(y0, y1))
max_x, max_y = (max(x0, x1), max(y0, y1))
frame_width, frame_height = video.frame_size
Expand All @@ -517,8 +518,8 @@ def detect_scenes(
if max_x >= frame_width or max_y >= frame_height:
logger.warning("Warning: crop ends outside of video boundary.")
effective_frame_size = (
1 + min(max_x, frame_width) - min_x,
1 + min(max_y, frame_height) - min_y,
1 + min(max_x, frame_width - 1) - min_x,
1 + min(max_y, frame_height - 1) - min_y,
)
# Calculate downscale factor and log effective resolution.
if self.auto_downscale:
Expand Down
35 changes: 35 additions & 0 deletions tests/test_scene_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,38 @@ def test_expand_scenes_to_bounds_does_not_mutate_input():
expand_scenes_to_bounds(scenes, start=t0, end=t300)

assert scenes == original


def _write_blank_video(path: str, width: int, height: int, num_frames: int = 3) -> None:
import cv2
import numpy as np

writer = cv2.VideoWriter(path, cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (width, height))
for _ in range(num_frames):
writer.write(np.zeros((height, width, 3), dtype=np.uint8))
writer.release()


@pytest.mark.parametrize(
"crop,warns",
[
# Bounds are inclusive, so these all lie inside of a 64x48 frame.
((0, 0, 63, 47), False),
((0, 24, 63, 47), False),
((10, 10, 20, 20), False),
# One pixel past the right or bottom edge.
((0, 0, 64, 47), True),
((0, 0, 63, 48), True),
],
)
def test_crop_boundary_warning(tmp_path, caplog, crop, warns):
"""The warning about a crop ending outside of the video must use the inclusive bounds."""
path = str(tmp_path / "blank.avi")
_write_blank_video(path, 64, 48)
video = VideoStreamCv2(path)
sm = SceneManager()
sm.crop = crop
sm.add_detector(ContentDetector())
with caplog.at_level("WARNING", logger="pyscenedetect"):
sm.detect_scenes(video=video)
assert any("crop ends outside" in r.getMessage() for r in caplog.records) == warns
Loading