Skip to content
Merged
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
27 changes: 1 addition & 26 deletions scenedetect/output/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,32 +586,7 @@ def save_images(
image_extension,
)
image_filenames[i].append(file_path)
# TODO: Combine this resize with the ones below.
if aspect_ratio is not None:
frame_im = cv2.resize(
frame_im, (0, 0), fx=aspect_ratio, fy=1.0, interpolation=interpolation.value
)
frame_height = frame_im.shape[0]
frame_width = frame_im.shape[1]

# Figure out what kind of resizing needs to be done
if height or width:
if height and not width:
factor = height / float(frame_height)
width = int(factor * frame_width)
elif width and not height:
factor = width / float(frame_width)
height = int(factor * frame_height)
assert height is not None
assert width is not None
assert height > 0 and width > 0
frame_im = cv2.resize(
frame_im, (width, height), interpolation=interpolation.value
)
elif scale:
frame_im = cv2.resize(
frame_im, (0, 0), fx=scale, fy=scale, interpolation=interpolation.value
)
frame_im = _scale_image(frame_im, aspect_ratio, height, width, scale, interpolation)
path = Path(get_and_create_path(file_path, output_dir))
(is_ok, encoded) = cv2.imencode(f".{image_extension}", frame_im, imwrite_param)
if is_ok:
Expand Down
41 changes: 41 additions & 0 deletions tests/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
from fractions import Fraction
from io import StringIO
from pathlib import Path
from unittest.mock import Mock
from xml.etree import ElementTree

import cv2
import numpy as np
import pytest

from scenedetect import (
Expand All @@ -44,6 +47,7 @@
write_scene_list_ffmetadata,
write_scene_list_otio,
)
from scenedetect.video_stream import VideoStream

FFMPEG_ARGS = (
"-vf crop=128:128:0:0 -map 0:v:0 -c:v libx264 -preset ultrafast -qp 0 -tune zerolatency"
Expand Down Expand Up @@ -179,6 +183,43 @@ def test_save_images_singlethreaded(test_video_file, tmp_path: Path):
assert total_images == len([path for path in tmp_path.glob(image_name_glob)])


@pytest.mark.parametrize("threading", [False, True])
@pytest.mark.parametrize(
"resize, expected_shapes",
[
({"width": 80}, [(40, 80), (60, 80)]),
({"height": 60}, [(60, 120), (60, 80)]),
],
)
def test_save_images_resizes_each_frame(tmp_path: Path, threading, resize, expected_shapes):
video = Mock(spec=VideoStream)
video.name = "variable-resolution"
video.aspect_ratio = 1.0
video.read.side_effect = [
np.zeros((80, 160, 3), dtype=np.uint8),
np.zeros((120, 160, 3), dtype=np.uint8),
]
scenes = [(FrameTimecode(0, 10.0), FrameTimecode(2, 10.0))]

filenames = save_images(
scenes,
video,
num_images=2,
image_extension="png",
output_dir=tmp_path,
threading=threading,
**resize,
)

shapes = []
for filename in filenames[0]:
data = np.fromfile(tmp_path / filename, dtype=np.uint8)
image = cv2.imdecode(data, cv2.IMREAD_UNCHANGED)
assert image is not None, f"Could not decode {filename}"
shapes.append(image.shape[:2])
assert shapes == expected_shapes


@pytest.mark.parametrize("frame_margin", [1, 0.1, "0.1s", "00:00:00.100"])
def test_save_images_frame_margin_accepts_time_values(
test_video_file, tmp_path: Path, frame_margin
Expand Down
1 change: 1 addition & 0 deletions website/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ 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] `save-images` no longer takes the last image of a scene from the first frame of the next scene. `--frame-margin` is counted from the last frame of the scene again, as in v0.6 (`-m 0` now selects the last frame itself)
- [bugfix] Single-threaded `save_images()` preserves each frame's aspect ratio when the video changes resolution and only a width or height is specified [#615](https://github.com/Breakthrough/PySceneDetect/pull/615)
- [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)
- [bugfix] `FrameTimecode.get_timecode()` with `use_rounding=False` now truncates instead of sometimes rounding up the last digit. Previously 0.4996 seconds with `precision=2` gave `00:00:00.50`, and 1.99997 seconds gave `00:00:01.000` instead of `00:00:01.999`. [#611](https://github.com/Breakthrough/PySceneDetect/issues/611)
Expand Down