|
| 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 | + def process_frame(self, frame_num: int, frame_img: np.ndarray) -> ty.List[int]: |
| 38 | + # TODO: frame_img is already downscaled here. The same problem exists in HashDetector. |
| 39 | + # For now we can just set downscale factor to 1 in SceneManager to work around the issue. |
| 40 | + frame_img = cv2.resize(frame_img, (256, 256)) |
| 41 | + histogram = np.asarray( |
| 42 | + [cv2.calcHist([c], [0], None, [254], [1, 255]) for c in cv2.split(frame_img)] |
| 43 | + ) |
| 44 | + frame_gray = cv2.resize(cv2.cvtColor(frame_img, cv2.COLOR_BGR2GRAY), (128, 128)) |
| 45 | + edges = np.maximum(frame_gray, cv2.Canny(frame_gray, 100, 200)) |
| 46 | + if self._start_frame_num is not None: |
| 47 | + delta_histogram = cv2.compareHist(self._last_histogram, histogram, cv2.HISTCMP_CORREL) |
| 48 | + delta_edges = structural_similarity(self._last_edges, edges, data_range=255) |
| 49 | + score = 4.61480465 * delta_histogram + 3.75211168 * delta_edges - 5.485968377115124 |
| 50 | + self._scores.append(score) |
| 51 | + if self._start_frame_num is None: |
| 52 | + self._start_frame_num = frame_num |
| 53 | + self._last_histogram = histogram |
| 54 | + self._last_edges = edges |
| 55 | + return [] |
| 56 | + |
| 57 | + def post_process(self, frame_num: int) -> ty.List[int]: |
| 58 | + self._scores = np.asarray(self._scores) |
| 59 | + num_frames = len(self._scores) |
| 60 | + convolution = self._scores.copy() |
| 61 | + convolution[1:-1] = np.convolve(convolution, np.array([1, 1, 1]) / 3.0, mode="valid") |
| 62 | + cut_found = np.zeros(num_frames + 1, bool) |
| 63 | + cut_found[-1] = True |
| 64 | + |
| 65 | + WINDOW_SIZE = 8 |
| 66 | + for frame_num in range(num_frames): |
| 67 | + if self._scores[frame_num] < 0: |
| 68 | + cut_found[frame_num] = True |
| 69 | + elif frame_num >= 8: |
| 70 | + last_cut = max(frame_num - WINDOW_SIZE, 0) |
| 71 | + if convolution[frame_num] < 0.75: |
| 72 | + M = len(convolution[last_cut:frame_num]) |
| 73 | + arr = np.sort(convolution[last_cut:frame_num]) |
| 74 | + arr = arr[int(M * 0.2) : int(M * 0.8)] |
| 75 | + mu = arr.mean() |
| 76 | + std = arr.std() |
| 77 | + if convolution[frame_num] < mu - 3 * max(0.2, std): |
| 78 | + cut_found[frame_num] = True |
| 79 | + |
| 80 | + cuts = [] |
| 81 | + last_cut = 0 |
| 82 | + for frame_num in range(len(cut_found)): |
| 83 | + if cut_found[frame_num]: |
| 84 | + if (frame_num - last_cut) > WINDOW_SIZE: |
| 85 | + cuts.append(last_cut) |
| 86 | + last_cut = frame_num + 1 |
| 87 | + return [cut + self._start_frame_num for cut in cuts][1:] |
0 commit comments