Skip to content

Commit 6b0aef9

Browse files
committed
[detectors] Implement Koala-36M
Work in progress. #441
1 parent 2c55a55 commit 6b0aef9

File tree

7 files changed

+106
-1
lines changed

7 files changed

+106
-1
lines changed

dist/requirements_windows.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
av==13.1.0
33
click>=8.0
44
opencv-python-headless==4.10.0.84
5+
scikit-image==0.24.0
56

67
imageio-ffmpeg
78
moviepy

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ opencv-python
88
platformdirs
99
pytest>=7.0
1010
tqdm
11+
scikit-image

requirements_headless.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ numpy
77
opencv-python-headless
88
platformdirs
99
pytest>=7.0
10-
tqdm
10+
scikit-image
11+
tqdm

scenedetect/_cli/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
ContentDetector,
4343
HashDetector,
4444
HistogramDetector,
45+
KoalaDetector,
4546
ThresholdDetector,
4647
)
4748
from scenedetect.platform import get_cv2_imwrite_params, get_system_version_info
@@ -1577,3 +1578,16 @@ def save_qp_command(
15771578
scenedetect.add_command(list_scenes_command)
15781579
scenedetect.add_command(save_images_command)
15791580
scenedetect.add_command(split_video_command)
1581+
1582+
1583+
@click.command("detect-koala", cls=Command, help="""WIP""")
1584+
@click.pass_context
1585+
def detect_koala_command(
1586+
ctx: click.Context,
1587+
):
1588+
ctx = ctx.obj
1589+
assert isinstance(ctx, CliContext)
1590+
ctx.add_detector(KoalaDetector, {"min_scene_len": None})
1591+
1592+
1593+
scenedetect.add_command(detect_koala_command)

scenedetect/detectors/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from scenedetect.detectors.adaptive_detector import AdaptiveDetector
4141
from scenedetect.detectors.hash_detector import HashDetector
4242
from scenedetect.detectors.histogram_detector import HistogramDetector
43+
from scenedetect.detectors.koala_detector import KoalaDetector
4344

4445
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
4546
# #
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

tests/test_detectors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ContentDetector,
3030
HashDetector,
3131
HistogramDetector,
32+
KoalaDetector,
3233
ThresholdDetector,
3334
)
3435

@@ -37,6 +38,7 @@
3738
ContentDetector,
3839
HashDetector,
3940
HistogramDetector,
41+
KoalaDetector,
4042
)
4143

4244
ALL_DETECTORS: ty.Tuple[ty.Type[SceneDetector]] = (*FAST_CUT_DETECTORS, ThresholdDetector)

0 commit comments

Comments
 (0)