Get a generator that yields the frames of the video. Args: source_path: The path of the video file. stride: Indicates the interval at which frames are returned, skipping stride - 1 frames between each. start: Indicates the starting position from which
(
source_path: str,
stride: int = 1,
start: int = 0,
end: int | None = None,
iterative_seek: bool = False,
)
| 229 | |
| 230 | |
| 231 | def get_video_frames_generator( |
| 232 | source_path: str, |
| 233 | stride: int = 1, |
| 234 | start: int = 0, |
| 235 | end: int | None = None, |
| 236 | iterative_seek: bool = False, |
| 237 | ) -> Generator[npt.NDArray[np.uint8], None, None]: |
| 238 | """ |
| 239 | Get a generator that yields the frames of the video. |
| 240 | |
| 241 | Args: |
| 242 | source_path: The path of the video file. |
| 243 | stride: Indicates the interval at which frames are returned, |
| 244 | skipping stride - 1 frames between each. |
| 245 | start: Indicates the starting position from which |
| 246 | video should generate frames |
| 247 | end: Indicates the ending position at which video |
| 248 | should stop generating frames. If None, video will be read to the end. |
| 249 | iterative_seek: If True, the generator will seek to the |
| 250 | `start` frame by grabbing each frame, which is much slower. This is a |
| 251 | workaround for videos that don't open at all when you set the `start` value. |
| 252 | |
| 253 | Returns: |
| 254 | A generator that yields the |
| 255 | frames of the video. |
| 256 | |
| 257 | Examples: |
| 258 | ```python |
| 259 | import supervision as sv |
| 260 | |
| 261 | for frame in sv.get_video_frames_generator(source_path="<SOURCE_VIDEO_PATH>"): |
| 262 | ... |
| 263 | ``` |
| 264 | """ |
| 265 | video, start, end = _validate_and_setup_video( |
| 266 | source_path, start, end, iterative_seek |
| 267 | ) |
| 268 | frame_position = start |
| 269 | while True: |
| 270 | success, frame = video.read() |
| 271 | if not success or frame_position >= end: |
| 272 | break |
| 273 | if frame is not None: |
| 274 | yield frame |
| 275 | for _ in range(stride - 1): |
| 276 | success = video.grab() |
| 277 | if not success: |
| 278 | break |
| 279 | frame_position += stride |
| 280 | video.release() |
| 281 | |
| 282 | |
| 283 | def process_video( |