|
| 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): |
| 31 | + self._start_frame_num: int = None |
| 32 | + self._min_scene_len: int = min_scene_len |
| 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 | + frame_img = cv2.resize(frame_img, (256, 256)) |
| 39 | + histogram = np.asarray( |
| 40 | + [cv2.calcHist([c], [0], None, [254], [1, 255]) for c in cv2.split(frame_img)] |
| 41 | + ) |
| 42 | + frame_gray = cv2.resize(cv2.cvtColor(frame_img, cv2.COLOR_BGR2GRAY), (128, 128)) |
| 43 | + edges = np.maximum(frame_gray, cv2.Canny(frame_gray, 100, 200)) |
| 44 | + if self._start_frame_num is not None: |
| 45 | + delta_histogram = cv2.compareHist(self._last_histogram, histogram, cv2.HISTCMP_CORREL) |
| 46 | + delta_edges = structural_similarity(self._last_edges, edges, data_range=255) |
| 47 | + score = 4.61480465 * delta_histogram + 3.75211168 * delta_edges - 5.485968377115124 |
| 48 | + self._scores.append(score) |
| 49 | + if self._start_frame_num is None: |
| 50 | + self._start_frame_num = frame_num |
| 51 | + self._last_histogram = histogram |
| 52 | + self._last_edges = edges |
| 53 | + return [] |
| 54 | + |
| 55 | + def post_process(self, frame_num: int) -> ty.List[int]: |
| 56 | + self._scores = np.asarray(self._scores) |
| 57 | + num_frames = len(self._scores) |
| 58 | + convolution = self._scores.copy() |
| 59 | + convolution[1:-1] = np.convolve(convolution, np.array([1, 1, 1]) / 3.0, mode="valid") |
| 60 | + cut_found = np.zeros(num_frames + 1, bool) |
| 61 | + cut_found[-1] = True |
| 62 | + WINDOW_SIZE = 8 |
| 63 | + for cut in range(num_frames): |
| 64 | + if self._scores[cut] < 0 or cut < WINDOW_SIZE: |
| 65 | + cut_found[cut] = True |
| 66 | + continue |
| 67 | + if convolution[cut] < 0.75: |
| 68 | + window = convolution[cut - WINDOW_SIZE : cut] |
| 69 | + window = np.sort(window)[int(WINDOW_SIZE * 0.2) : int(WINDOW_SIZE * 0.8)] |
| 70 | + mu = window.mean() |
| 71 | + std = window.std() |
| 72 | + if convolution[cut] < mu - 3 * max(0.2, std): |
| 73 | + cut_found[cut] = True |
| 74 | + cuts = [] |
| 75 | + last_cut = 0 |
| 76 | + last_filtered_cut = self._start_frame_num |
| 77 | + for cut in range(WINDOW_SIZE, len(cut_found)): |
| 78 | + if cut_found[cut]: |
| 79 | + if (cut - last_cut) > WINDOW_SIZE: |
| 80 | + cut = self._start_frame_num + last_cut |
| 81 | + if (cut - last_filtered_cut) >= self._min_scene_len: |
| 82 | + cuts.append(cut) |
| 83 | + last_filtered_cut = cut |
| 84 | + last_cut = cut + 1 |
| 85 | + return cuts |
0 commit comments