Skip to content

Commit

Permalink
Merge pull request #151 from mhashim6/live_callback
Browse files Browse the repository at this point in the history
live callbacks for live-streams.

Resolves #5.
  • Loading branch information
Breakthrough committed Jan 10, 2021
1 parent b22fe99 commit 8b79bc8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ PySceneDetect Releases
* [enhancement] Removed first row from statsfile to comply with RFC 4180, includes backwards compatibility so existing statsfiles can still be loaded (resolves [#136](https://github.com/Breakthrough/PySceneDetect/issues/136))
* [api] Removed unused argument base_timecode from `StatsManager.load_from_csv()` method
* [api] Make the `base_timecode` argument optional on the `SceneManager` methods `get_scene_list()`, `get_cut_list()`, and `get_event_list()` (resolves [#173](https://github.com/Breakthrough/PySceneDetect/issues/173))
* [api] Support for live video stream callbacks by adding new `callback` argument to the `detect_scenes()` method of `SceneManager` (resolves [#5](https://github.com/Breakthrough/PySceneDetect/issues/5))
* [bugfix] Fix unhandled exception causing improper error message when a video fails to load on non-Windows platforms (resolves [#192](https://github.com/Breakthrough/PySceneDetect/issues/192))
* [enhancement] Enabled dynamic resizing for progress bar (resolves [#193](https://github.com/Breakthrough/PySceneDetect/issues/193))
* [enhancement] Always ouptut version number via logger to assist with debugging (resolves [#171](https://github.com/Breakthrough/PySceneDetect/issues/171))
Expand Down
20 changes: 14 additions & 6 deletions scenedetect/scene_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,19 @@ def get_event_list(self, base_timecode=None):
for start, end in self._event_list]


def _process_frame(self, frame_num, frame_im):
def _process_frame(self, frame_num, frame_im, callback=None):
# type(int, numpy.ndarray) -> None
""" Adds any cuts detected with the current frame to the cutting list. """
for detector in self._detector_list:
self._cutting_list += detector.process_frame(frame_num, frame_im)
cuts = detector.process_frame(frame_num, frame_im)
if cuts and callback:
callback(frame_im, frame_num)
self._cutting_list += cuts
for detector in self._sparse_detector_list:
self._event_list += detector.process_frame(frame_num, frame_im)
events = detector.process_frame(frame_num, frame_im)
if events and callback:
callback(frame_im, frame_num)
self._event_list += events


def _is_processing_required(self, frame_num):
Expand All @@ -530,9 +536,9 @@ def _post_process(self, frame_num):


def detect_scenes(self, frame_source, end_time=None, frame_skip=0,
show_progress=True):
show_progress=True, callback=None):
# type: (VideoManager, Union[int, FrameTimecode],
# Optional[Union[int, FrameTimecode]], Optional[bool]) -> int
# Optional[Union[int, FrameTimecode]], Optional[bool], optional[callable[numpy.ndarray]) -> int
""" Perform scene detection on the given frame_source using the added SceneDetectors.
Blocks until all frames in the frame_source have been processed. Results can
Expand All @@ -554,6 +560,8 @@ def detect_scenes(self, frame_source, end_time=None, frame_skip=0,
show_progress (bool): If True, and the ``tqdm`` module is available, displays
a progress bar with the progress, framerate, and expected time to
complete processing the video frame source.
callback ((image_ndarray, frame_num: int) -> None): If not None, called after
each scene/event detected.
Returns:
int: Number of frames read and processed from the frame source.
Raises:
Expand Down Expand Up @@ -618,7 +626,7 @@ def detect_scenes(self, frame_source, end_time=None, frame_skip=0,

if not ret_val:
break
self._process_frame(self._num_frames + start_frame, frame_im)
self._process_frame(self._num_frames + start_frame, frame_im, callback)

curr_frame += 1
self._num_frames += 1
Expand Down

0 comments on commit 8b79bc8

Please sign in to comment.