sportslabkit.camera#
Overview#
Pythonic wrapper around OpenCV’s VideoCapture(). |
|
Pythonic wrapper around OpenCV’s VideoCapture(). |
|
Calculate the intrinsic parameters of a camera from a video of a checkerboard pattern. |
|
Read pitch keypoints from xml file. |
|
Load cameras from a list of dictionaries containing camera information. |
Classes#
- class sportslabkit.camera.VideoReader(filename: str, threaded=False, queue_size=10)#
Pythonic wrapper around OpenCV’s VideoCapture().
This class provides a convenient way to access and manipulate video files using OpenCV’s VideoCapture object. It implements several convenient methods and properties to make it easy to work with video files, including slicing and indexing into the video file, iteration through the video frames, and more.
- Parameters:
- Properties:
frame_width (int): The width of the video frames. frame_height (int): The height of the video frames. frame_channels (int): The number of channels in the video frames. frame_rate (float): The frame rate of the video. frame_shape (tuple): The shape of the video frames (height, width, channels). number_of_frames (int): The total number of frames in the video. fourcc (int): The fourcc code of the video. current_frame_pos (int): The current position of the video frame.
- read(frame_number=None)#
Read the next frame or a specified frame from the video.
- close()#
Close the video file.
Overview
Methods# close()Close video file.
-
read(frame_number)Read next frame or frame specified by frame_number.
Members
- close()#
Close video file.
- read_thread()#
- read(frame_number=None)#
Read next frame or frame specified by frame_number.
- class sportslabkit.camera.Camera(video_path: sportslabkit.types.types.PathLike, threaded: bool = False, queue_size: int = 10, keypoint_xml: str | None = None, x_range: Sequence[float] | None = (0, 105), y_range: Sequence[float] | None = (0, 68), camera_matrix: ArrayLike | None = None, camera_matrix_path: str | None = None, distortion_coefficients: str | None = None, distortion_coefficients_path: str | None = None, calibration_video_path: str | None = None, calibration_method: str = 'zhang', label: str = '', verbose: int = 0)[source]#
Bases:
sportslabkit.camera.videoreader.VideoReaderPythonic wrapper around OpenCV’s VideoCapture().
This class provides a convenient way to access and manipulate video files using OpenCV’s VideoCapture object. It implements several convenient methods and properties to make it easy to work with video files, including slicing and indexing into the video file, iteration through the video frames, and more.
- Parameters:
- Properties:
frame_width (int): The width of the video frames. frame_height (int): The height of the video frames. frame_channels (int): The number of channels in the video frames. frame_rate (float): The frame rate of the video. frame_shape (tuple): The shape of the video frames (height, width, channels). number_of_frames (int): The total number of frames in the video. fourcc (int): The fourcc code of the video. current_frame_pos (int): The current position of the video frame.
- read(frame_number=None)#
Read the next frame or a specified frame from the video.
- close()#
Close the video file.
Overview
Methods# -
get_frame(frame_idx)Get frame from video.
iter_frames(calibrate, crop)Iterate over frames of video.
batch_frames(batch_size, calibrate, crop)Iterate over frames of video.
video2pitch(pts)Convert image coordinates to pitch coordinates.
pitch2video(pitch_pts)abc Converts pitch coordinates to image coordinates.
undistort_points(points)Undistort points with the camera matrix and distortion coefficients.
pitch_contour(frame_num)-
undistort_image(image)-
min()-
max()-
Members
- load_calibration_params()#
- get_frame(frame_idx: int) numpy.ndarray#
Get frame from video.
- Parameters:
frame (int) – frame
- Returns:
frame
- Return type:
np.ndarray
- iter_frames(calibrate: bool = False, crop: bool = False) collections.abc.Generator[numpy.typing.NDArray, None, None]#
Iterate over frames of video.
- Yields:
NDArray – frame of video.
- batch_frames(batch_size: int = 32, calibrate: bool = False, crop: bool = False) collections.abc.Generator[numpy.typing.NDArray, None, None]#
Iterate over frames of video.
- Yields:
NDArray – frame of video.
- video2pitch(pts: numpy.typing.ArrayLike) numpy.typing.NDArray[numpy.float64]#
Convert image coordinates to pitch coordinates.
- Parameters:
video_pts (np.ndarray) – points in image coordinate space
- Returns:
points in pitch coordinate
- Return type:
np.ndarray
- abstract pitch2video(pitch_pts: numpy.typing.ArrayLike) numpy.typing.NDArray[numpy.float64]#
Converts pitch coordinates to image coordinates.
- Parameters:
pitch_pts (ArrayLike) – coordinates in pitch coordinate space.
- Raises:
NotImplementedError – this method is not implemented.
- Returns:
…
- Return type:
NDArray[np.float64]
- undistort_points(points: numpy.typing.ArrayLike) numpy.typing.NDArray[numpy.float64]#
Undistort points with the camera matrix and distortion coefficients.
- Parameters:
points (ArrayLike) – [description]
- Returns:
[description]
- Return type:
NDArray[np.float64]
Note
Not to be confused with video2pitch which uses a homography transformation.
- pitch_contour(frame_num)#
- undistort_image(image: numpy.typing.NDArray) numpy.typing.NDArray#
- min()#
- max()#
Functions#
- sportslabkit.camera.find_intrinsic_camera_parameters(media_path: sportslabkit.types.types.PathLike, fps: int = 1, scale: int = 4, save_path: PathLike | None = None, draw_on_save: bool = False, points_to_use: int = 50, calibration_method: str = 'zhang', return_mappings: bool = True) tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray, numpy.ndarray]#
Calculate the intrinsic parameters of a camera from a video of a checkerboard pattern.
This function takes a video file containing a checkerboard pattern and calculates the intrinsic parameters of the camera. The video is first processed to locate the corners of the checkerboard in each frame. These corners are then used to compute the intrinsic parameters of the camera.
- Parameters:
media_path (Union[str, Path]) – Path to the video file or a list of video files containing the checkerboard pattern. Wildcards are supported.
fps (int, optional) – Frames per second to use when processing the video. Defaults to 1.
scale (int, optional) – Scale factor to use when processing the video. Defaults to 4.
save_path (Optional[Union[str, Path]], optional) – Path to save the computed intrinsic parameters. If not specified, the parameters are not saved. Defaults to None.
draw_on_save (bool, optional) – If True, the corners of the checkerboard are drawn on the frames and saved with the intrinsic parameters. Defaults to False.
points_to_use (int, optional) – Number of frames to use when calculating the intrinsic parameters. If more frames are found than this number, a subset of frames is selected based on their location in the image plane. Defaults to 50.
calibration_method (str, optional) – Calibration method to use. Must be either “zhang” or “fisheye”. Defaults to “zhang”.
return_mappings (bool, optional) – If True, the function returns the computed mapping functions along with the intrinsic parameters. Defaults to True.
- Returns:
A tuple containing the camera matrix, distortion coefficients, and mapping functions (if return_mappings is True).
- Return type:
Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]
- Raises:
ValueError – If the calibration_method is not “zhang” or “fisheye”.
- sportslabkit.camera.read_pitch_keypoints(xmlfile: str, annot_type: str) tuple[numpy.typing.NDArray[numpy.float64], numpy.typing.NDArray[numpy.float64]][source]#
Read pitch keypoints from xml file.
- Parameters:
- Raises:
ValueError – if annotation type is not ‘pitch’ or ‘video’.
- Returns:
pitch keypoints and video keypoints.
- Return type:
Tuple[NDArray[np.float64], NDArray[np.float64]]
- sportslabkit.camera.load_cameras(camera_info: list[collections.abc.Mapping]) list[Camera][source]#
Load cameras from a list of dictionaries containing camera information.
- Parameters:
camera_info (List[Mapping]) – list of dictionaries containing camera information.
- Returns:
list of cameras objects.
- Return type:
List[Camera]
Attributes#
- sportslabkit.camera.PathLike :TypeAlias#