nnspike.unit.webcam_video_stream

Classes

WebcamVideoStream(src, save_video[, ...])

A threaded video stream reader for webcams with optional video recording.

class nnspike.unit.webcam_video_stream.WebcamVideoStream(src, save_video, save_path='', resolution=(640, 320), fps=30)[source]

A threaded video stream reader for webcams with optional video recording.

This class provides a non-blocking way to read frames from a video source by running the capture loop in a separate thread. It supports configurable resolution, frame rate, and optional video recording to file.

The threaded approach helps prevent frame drops and provides smoother video processing by maintaining a minimal buffer size and continuous frame updates in the background.

Parameters:
  • src (int | str) – Video source - can be camera index (int) or video file path (str)

  • save_video (bool) – Whether to save captured video to file

  • save_path (str) – Path for saving video file (required if save_video is True)

  • resolution (tuple) – Video resolution as (width, height) tuple. Default: (640, 320)

  • fps (int) – Frames per second for capture and recording. Default: 30

Example

>>> stream = WebcamVideoStream(src=0, save_video=False)
>>> stream.start()
>>> grabbed, frame = stream.read()
>>> stream.stop()
__init__(src, save_video, save_path='', resolution=(640, 320), fps=30)[source]
start()[source]

Start the background thread for reading video frames.

Return type:

WebcamVideoStream

Returns:

Self reference for method chaining.

update()[source]

Continuously update frames from the video stream in a background thread.

This method runs in a loop until stopped, constantly reading new frames from the video source to keep the frame buffer current.

Return type:

None

read()[source]

Read the most recently captured frame.

If video recording is enabled, also writes the frame to the output file.

Return type:

tuple[bool, np.ndarray | None]

Returns:

Tuple of (success_flag, frame) where success_flag indicates if the frame was successfully captured and frame is the image data as a numpy array, or None if capture failed.

stop()[source]

Stop the video stream and clean up resources.

This method stops the background thread, releases the video writer (if recording), and releases the video capture stream.

Return type:

None