MCPcopy Index your code
hub / github.com/Breakthrough/PySceneDetect / test_api_device_callback

Function test_api_device_callback

tests/test_api.py:137–158  ·  view source on GitHub ↗

Demonstrate how to use a webcam/device/pipe and a callback function. Instead of calling `open_video()`, an existing `cv2.VideoCapture` can be used by wrapping it with a `VideoCaptureAdapter.`

(test_video_file: str)

Source from the content-addressed store, hash-verified

135
136
137def test_api_device_callback(test_video_file: str):
138 """Demonstrate how to use a webcam/device/pipe and a callback function.
139 Instead of calling `open_video()`, an existing `cv2.VideoCapture` can be used by
140 wrapping it with a `VideoCaptureAdapter.`"""
141 import cv2
142 import numpy
143
144 from scenedetect import ContentDetector, FrameTimecode, SceneManager, VideoCaptureAdapter
145
146 # Callback to invoke on the first frame of every new scene detection.
147 def on_new_scene(frame_img: numpy.ndarray, position: FrameTimecode):
148 print(f"New scene found at frame {position.frame_num}.")
149
150 # We open a file just for test purposes, but we can also use a device or pipe here.
151 cap = cv2.VideoCapture(test_video_file)
152 video = VideoCaptureAdapter(cap)
153 # Now `video` can be used as normal with a `SceneManager`. If the input is non-terminating,
154 # either set `end_time/duration` when calling `detect_scenes`, or call `scene_manager.stop()`.
155 total_frames = 1000
156 scene_manager = SceneManager()
157 scene_manager.add_detector(ContentDetector())
158 scene_manager.detect_scenes(video=video, duration=total_frames, callback=on_new_scene)
159
160
161# TODO(v0.8): Remove this test when these deprecated modules are removed from the codebase.

Callers

nothing calls this directly

Calls 5

add_detectorMethod · 0.95
detect_scenesMethod · 0.95
VideoCaptureAdapterClass · 0.90
SceneManagerClass · 0.90
ContentDetectorClass · 0.90

Tested by

no test coverage detected