Skip to content

Commit 8b79bc8

Browse files
committed
Merge pull request #151 from mhashim6/live_callback
live callbacks for live-streams. Resolves #5.
1 parent b22fe99 commit 8b79bc8

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ PySceneDetect Releases
1717
* [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))
1818
* [api] Removed unused argument base_timecode from `StatsManager.load_from_csv()` method
1919
* [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))
20+
* [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))
2021
* [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))
2122
* [enhancement] Enabled dynamic resizing for progress bar (resolves [#193](https://github.com/Breakthrough/PySceneDetect/issues/193))
2223
* [enhancement] Always ouptut version number via logger to assist with debugging (resolves [#171](https://github.com/Breakthrough/PySceneDetect/issues/171))

scenedetect/scene_manager.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,19 @@ def get_event_list(self, base_timecode=None):
505505
for start, end in self._event_list]
506506

507507

508-
def _process_frame(self, frame_num, frame_im):
508+
def _process_frame(self, frame_num, frame_im, callback=None):
509509
# type(int, numpy.ndarray) -> None
510510
""" Adds any cuts detected with the current frame to the cutting list. """
511511
for detector in self._detector_list:
512-
self._cutting_list += detector.process_frame(frame_num, frame_im)
512+
cuts = detector.process_frame(frame_num, frame_im)
513+
if cuts and callback:
514+
callback(frame_im, frame_num)
515+
self._cutting_list += cuts
513516
for detector in self._sparse_detector_list:
514-
self._event_list += detector.process_frame(frame_num, frame_im)
517+
events = detector.process_frame(frame_num, frame_im)
518+
if events and callback:
519+
callback(frame_im, frame_num)
520+
self._event_list += events
515521

516522

517523
def _is_processing_required(self, frame_num):
@@ -530,9 +536,9 @@ def _post_process(self, frame_num):
530536

531537

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

619627
if not ret_val:
620628
break
621-
self._process_frame(self._num_frames + start_frame, frame_im)
629+
self._process_frame(self._num_frames + start_frame, frame_im, callback)
622630

623631
curr_frame += 1
624632
self._num_frames += 1

0 commit comments

Comments
 (0)