-
Notifications
You must be signed in to change notification settings - Fork 431
[detectors] Add Koala-36M Based Detector #459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Breakthrough
wants to merge
1
commit into
main
Choose a base branch
from
issue-441-koala
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−6
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ opencv-python | |
platformdirs | ||
pytest>=7.0 | ||
tqdm | ||
scikit-image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ numpy | |
opencv-python-headless | ||
platformdirs | ||
pytest>=7.0 | ||
tqdm | ||
scikit-image | ||
tqdm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# | ||
# PySceneDetect: Python-Based Video Scene Detector | ||
# ------------------------------------------------------------------- | ||
# [ Site: https://scenedetect.com ] | ||
# [ Docs: https://scenedetect.com/docs/ ] | ||
# [ Github: https://github.com/Breakthrough/PySceneDetect/ ] | ||
# | ||
# Copyright (C) 2014-2024 Brandon Castellano <http://www.bcastell.com>. | ||
# PySceneDetect is licensed under the BSD 3-Clause License; see the | ||
# included LICENSE file, or visit one of the above pages for details. | ||
# | ||
""":class:`KoalaDetector` uses the detection method described by Koala-36M. | ||
See https://koala36m.github.io/ for details. | ||
|
||
TODO: Cite correctly. | ||
|
||
This detector is available from the command-line as the `detect-koala` command. | ||
""" | ||
|
||
import typing as ty | ||
|
||
import cv2 | ||
import numpy as np | ||
from skimage.metrics import structural_similarity | ||
|
||
from scenedetect.scene_detector import SceneDetector | ||
|
||
|
||
class KoalaDetector(SceneDetector): | ||
def __init__(self, min_scene_len: int = None): | ||
self._start_frame_num: int = None | ||
self._min_scene_len: int = min_scene_len if min_scene_len else 0 | ||
self._last_histogram: np.ndarray = None | ||
self._last_edges: np.ndarray = None | ||
self._scores: ty.List[ty.List[int]] = [] | ||
|
||
# Tunables (TODO: Make these config params): | ||
|
||
# Boxcar filter size (should be <= window size) | ||
self._filter_size: int = 3 | ||
# Window to use for calculating threshold (should be >= filter size). | ||
self._window_size: int = 8 | ||
# Multiplier for standard deviations when calculating threshold. | ||
self._deviation: float = 3.0 | ||
|
||
def process_frame(self, frame_num: int, frame_img: np.ndarray) -> ty.List[int]: | ||
# TODO: frame_img is already downscaled here. The same problem exists in HashDetector. | ||
# For now we can just set downscale factor to 1 in SceneManager to work around the issue. | ||
frame_img = cv2.resize(frame_img, (256, 256)) | ||
histogram = np.asarray( | ||
[cv2.calcHist([c], [0], None, [254], [1, 255]) for c in cv2.split(frame_img)] | ||
) | ||
# TODO: Make the parameters below tunable. | ||
frame_gray = cv2.resize(cv2.cvtColor(frame_img, cv2.COLOR_BGR2GRAY), (128, 128)) | ||
edges = np.maximum(frame_gray, cv2.Canny(frame_gray, 100, 200)) | ||
if self._start_frame_num is not None: | ||
delta_histogram = cv2.compareHist(self._last_histogram, histogram, cv2.HISTCMP_CORREL) | ||
delta_edges = structural_similarity(self._last_edges, edges, data_range=255) | ||
score = 4.61480465 * delta_histogram + 3.75211168 * delta_edges - 5.485968377115124 | ||
self._scores.append(score) | ||
if self._start_frame_num is None: | ||
self._start_frame_num = frame_num | ||
self._last_histogram = histogram | ||
self._last_edges = edges | ||
return [] | ||
|
||
def post_process(self, frame_num: int) -> ty.List[int]: | ||
cut_found = [score < 0.0 for score in self._scores] | ||
cut_found.append(True) | ||
filter = [1] * self._filter_size | ||
cutoff = float(self._filter_size) / float(self._filter_size + 1) | ||
filtered = np.convolve(self._scores, filter, mode="same") | ||
for frame_num in range(len(self._scores)): | ||
if frame_num >= self._window_size and filtered[frame_num] < cutoff: | ||
# TODO: Should we discard the N most extreme values before calculating threshold? | ||
window = filtered[frame_num - self._window_size : frame_num] | ||
threshold = window.mean() - (self._deviation * window.std()) | ||
if filtered[frame_num] < threshold: | ||
cut_found[frame_num] = True | ||
|
||
cuts = [] | ||
last_cut = 0 | ||
for frame_num in range(len(cut_found)): | ||
if cut_found[frame_num]: | ||
if (frame_num - last_cut) > self._window_size: | ||
cuts.append(last_cut) | ||
last_cut = frame_num + 1 | ||
return [cut + self._start_frame_num for cut in cuts][1:] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, interesting. Even though Koala detector uses ML-based methods, it does not achieve good performance compared with rule-based detectors?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible I have a bug somewhere in the implementation. This was a first-pass at implementing this with just a single video test case. I haven't reviewed the code in some time - if you spot anything that looks wrong, please let me know!
Unfortunately I haven't had time to work on this PR lately. If you want to develop this detector further, I would be happy to clean this PR up and get it ready for review.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. To be honest, I am not sure about the performance of Koala-37M because the authors do not evaluate the shot detection accuracy in the paper. Because they released the code (https://github.com/KwaiVGI/Koala-36M/blob/main/trainsition_detect/VideoTransitionAnalyzer.py), let me check the performance on BBC and AutoShot. I will report it in this thread.