|
| 1 | +# |
| 2 | +# PySceneDetect: Python-Based Video Scene Detector |
| 3 | +# ------------------------------------------------------------------- |
| 4 | +# [ Site: https://scenedetect.com ] |
| 5 | +# [ Docs: https://scenedetect.com/docs/ ] |
| 6 | +# [ Github: https://github.com/Breakthrough/PySceneDetect/ ] |
| 7 | +# |
| 8 | +# Copyright (C) 2014-2024 Brandon Castellano <http://www.bcastell.com>. |
| 9 | +# PySceneDetect is licensed under the BSD 3-Clause License; see the |
| 10 | +# included LICENSE file, or visit one of the above pages for details. |
| 11 | +# |
| 12 | +""":class:`KoalaDetector` uses the detection method described by Koala-36M. |
| 13 | +See https://koala36m.github.io/ for details. |
| 14 | +
|
| 15 | +TODO: Cite correctly. |
| 16 | +
|
| 17 | +This detector is available from the command-line as the `detect-koala` command. |
| 18 | +""" |
| 19 | + |
| 20 | +import typing as ty |
| 21 | + |
| 22 | +import cv2 |
| 23 | +import numpy as np |
| 24 | +from skimage.metrics import structural_similarity |
| 25 | + |
| 26 | +from scenedetect.scene_detector import SceneDetector |
| 27 | + |
| 28 | + |
| 29 | +class KoalaDetector(SceneDetector): |
| 30 | + def __init__(self, min_scene_len: int = None): |
| 31 | + self._start_frame_num: int = None |
| 32 | + self._min_scene_len: int = min_scene_len if min_scene_len else 0 |
| 33 | + self._last_histogram: np.ndarray = None |
| 34 | + self._last_edges: np.ndarray = None |
| 35 | + self._scores: ty.List[ty.List[int]] = [] |
| 36 | + |
| 37 | + # Tunables (TODO: Make these config params): |
| 38 | + |
| 39 | + # Boxcar filter size (should be <= window size) |
| 40 | + self._filter_size: int = 3 |
| 41 | + # Window to use for calculating threshold (should be >= filter size). |
| 42 | + self._window_size: int = 8 |
| 43 | + # Multiplier for standard deviations when calculating threshold. |
| 44 | + self._deviation: float = 3.0 |
| 45 | + |
| 46 | + def process_frame(self, frame_num: int, frame_img: np.ndarray) -> ty.List[int]: |
| 47 | + # TODO: frame_img is already downscaled here. The same problem exists in HashDetector. |
| 48 | + # For now we can just set downscale factor to 1 in SceneManager to work around the issue. |
| 49 | + frame_img = cv2.resize(frame_img, (256, 256)) |
| 50 | + histogram = np.asarray( |
| 51 | + [cv2.calcHist([c], [0], None, [254], [1, 255]) for c in cv2.split(frame_img)] |
| 52 | + ) |
| 53 | + # TODO: Make the parameters below tunable. |
| 54 | + frame_gray = cv2.resize(cv2.cvtColor(frame_img, cv2.COLOR_BGR2GRAY), (128, 128)) |
| 55 | + edges = np.maximum(frame_gray, cv2.Canny(frame_gray, 100, 200)) |
| 56 | + if self._start_frame_num is not None: |
| 57 | + delta_histogram = cv2.compareHist(self._last_histogram, histogram, cv2.HISTCMP_CORREL) |
| 58 | + delta_edges = structural_similarity(self._last_edges, edges, data_range=255) |
| 59 | + score = 4.61480465 * delta_histogram + 3.75211168 * delta_edges - 5.485968377115124 |
| 60 | + self._scores.append(score) |
| 61 | + if self._start_frame_num is None: |
| 62 | + self._start_frame_num = frame_num |
| 63 | + self._last_histogram = histogram |
| 64 | + self._last_edges = edges |
| 65 | + return [] |
| 66 | + |
| 67 | + def post_process(self, frame_num: int) -> ty.List[int]: |
| 68 | + cut_found = [score < 0.0 for score in self._scores] |
| 69 | + cut_found.append(True) |
| 70 | + filter = [1] * self._filter_size |
| 71 | + cutoff = float(self._filter_size) / float(self._filter_size + 1) |
| 72 | + filtered = np.convolve(self._scores, filter, mode="same") |
| 73 | + for frame_num in range(len(self._scores)): |
| 74 | + if frame_num >= self._window_size and filtered[frame_num] < cutoff: |
| 75 | + # TODO: Should we discard the N most extreme values before calculating threshold? |
| 76 | + window = filtered[frame_num - self._window_size : frame_num] |
| 77 | + threshold = window.mean() - (self._deviation * window.std()) |
| 78 | + if filtered[frame_num] < threshold: |
| 79 | + cut_found[frame_num] = True |
| 80 | + |
| 81 | + cuts = [] |
| 82 | + last_cut = 0 |
| 83 | + for frame_num in range(len(cut_found)): |
| 84 | + if cut_found[frame_num]: |
| 85 | + if (frame_num - last_cut) > self._window_size: |
| 86 | + cuts.append(last_cut) |
| 87 | + last_cut = frame_num + 1 |
| 88 | + return [cut + self._start_frame_num for cut in cuts][1:] |
0 commit comments