Skip to content

courtvision.vis

load_image(file_name)

Loads the image with OpenCV.

Source code in courtvision/vis.py
245
246
247
248
249
250
251
def load_image(file_name):
    """Loads the image with OpenCV."""
    assert os.path.isfile(file_name), f"Invalid file {file_name}"  # nosec
    # load image with OpenCV
    if isinstance(file_name, Path):
        file_name = file_name.as_posix()
    return cv2.cvtColor(cv2.imread(file_name, cv2.IMREAD_COLOR), cv2.COLOR_BGR2RGB)

load_timg(file_name)

Loads the image with OpenCV and converts to torch.Tensor.

Source code in courtvision/vis.py
238
239
240
241
242
def load_timg(file_name):
    """Loads the image with OpenCV and converts to torch.Tensor."""
    img = load_image(file_name)
    # convert image to torch tensor
    return K.image_to_tensor(img, None).float() / 255.0

log_court_layout(camera_matrix, image_width, image_height, translation_vector, rotation_vector, court_mesh_path)

summary

Parameters:

Name Type Description Default
camera_matrix np.array

description

required
image_width int

description

required
image_height int

description

required
translation_vector np.array

description

required
rotation_vector np.array

description

required
court_mesh_path Path

description

required
Source code in courtvision/vis.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def log_court_layout(
    camera_matrix: np.array,
    image_width: int,
    image_height: int,
    translation_vector: np.array,
    rotation_vector: np.array,
    court_mesh_path: Path,
):
    """_summary_

    Args:
        camera_matrix (np.array): _description_
        image_width (int): _description_
        image_height (int): _description_
        translation_vector (np.array): _description_
        rotation_vector (np.array): _description_
        court_mesh_path (Path): _description_
    """
    rr.log_pinhole(
        "world/camera/image",
        child_from_parent=camera_matrix,
        width=image_width,
        height=image_height,
        timeless=True,
    )
    rotation_matrix, _ = cv2.Rodrigues(rotation_vector)
    rr.log_transform3d(
        "world/camera",
        transform=rr.TranslationAndMat3(
            translation=translation_vector.squeeze(),
            matrix=rotation_matrix,
        ),
        from_parent=True,
    )
    rr.log_point("world/front_left", np.array([0, 0, 0]))
    # TODO: this should be refectored to use the court_size
    rr.log_point("world/front_right", np.array([100, 0, 0]))
    rr.log_mesh_file(
        "world",
        mesh_format=rr.MeshFormat("GLB"),
        mesh_path=court_mesh_path,
    )

plot_3d_lines(xs, ys, zs, plt_axis=None, view_init=(90.0, 0.0, 0.0))

plots lines on a Axes3D

Parameters:

Name Type Description Default
xs np.array

array of x start and stop coordinates

required
ys np.array

array of y start and stop coordinates

required
zs np.array

array of z start and stop coordinates

required
plt_axis None | Axes3D

Axes3D to draw on. If not given one will be created Defaults to None.

None
view_init tuple[float, float, float]

Position of the camera to view. Defaults to (90., 0., 0.).

(90.0, 0.0, 0.0)

Returns:

Name Type Description
Axes3D Axes3D

matplotlib ax that can be added to or shown.

Source code in courtvision/vis.py
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
def plot_3d_lines(
    xs: np.array,
    ys: np.array,
    zs: np.array,
    plt_axis: None | Axes3D = None,
    view_init: tuple[float, float, float] = (90.0, 0.0, 0.0),
) -> Axes3D:
    """plots lines on a Axes3D

    Args:
        xs (np.array): array of x start and stop coordinates
        ys (np.array): array of y start and stop coordinates
        zs (np.array): array of z start and stop coordinates
        plt_axis (None | Axes3D, optional): Axes3D to draw on. If not given one will be created Defaults to None.
        view_init (tuple[float, float, float], optional): Position of the camera to view. Defaults to (90., 0., 0.).

    Returns:
        Axes3D: matplotlib ax that can be added to or shown.
    """

    fig = None
    if plt_axis is None:
        fig = plt.figure()
        plt_axis = fig.add_subplot(111, projection="3d")
    for i in range(len(xs)):
        plt_axis.plot(xs[i], ys[i], zs[i], c="b")
    # set the axis labels
    plt_axis.set_xlabel("X")
    plt_axis.set_ylabel("Y")
    plt_axis.set_zlabel("Z")
    plt_axis.set_aspect("equal")
    plt_axis.margins(x=0)
    plt_axis.view_init(*view_init)
    return plt_axis, fig

plot_coords(img, src_coords, show=True, thickness=2)

Draws lines on 'img'.

Parameters:

Name Type Description Default
img np.array

description

required
src_coords np.array

description

required
show bool

description. Defaults to True.

True
Source code in courtvision/vis.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def plot_coords(img: np.array, src_coords: np.array, show: bool = True, thickness=2):
    """Draws lines on 'img'.

    Args:
        img (np.array): _description_
        src_coords (np.array): _description_
        show (bool, optional): _description_. Defaults to True.
    """
    src_coords = src_coords.astype(int)
    cv2.polylines(img, [src_coords], True, (0, 255, 0), thickness=2)
    if show:
        from matplotlib import pyplot as plt

        plt.imshow(img)

plot_n_images_in_a_grid(images, n_cols=3)

draws a grid of images

Parameters:

Name Type Description Default
images list[np.array]

images to draw on - this is inplace

required
n_cols int

number of cols. Defaults to 3.

3

Returns:

Name Type Description
tuple fig, ax

matplotlib fig and ax

Source code in courtvision/vis.py
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
def plot_n_images_in_a_grid(images: list[np.array], n_cols: int = 3):
    """draws a grid of images

    Args:
        images (list[np.array]): images to draw on - this is inplace
        n_cols (int, optional): number of cols. Defaults to 3.

    Returns:
        tuple(fig, ax): matplotlib fig and ax
    """
    n_cols = min(n_cols, len(images))
    n_rows = int(np.ceil(len(images) / n_cols))
    fig, ax = plt.subplots(n_rows, n_cols, figsize=(15, 15))
    if n_rows == 1:
        ax = ax.reshape(1, n_cols)
    for i, img in enumerate(images):
        ax[i // n_cols, i % n_cols].imshow(img)
    return fig, ax