|
1 | | -from typing import Tuple |
| 1 | +from typing import List, Optional, Tuple |
2 | 2 |
|
3 | 3 | import cv2 |
4 | 4 | import matplotlib.pyplot as plt |
5 | 5 | import numpy as np |
6 | 6 |
|
7 | 7 |
|
8 | | -def show_frame_in_notebook( |
9 | | - frame: np.ndarray, size: Tuple[int, int] = (10, 10), cmap: str = "gray" |
10 | | -): |
| 8 | +def plot_image( |
| 9 | + image: np.ndarray, size: Tuple[int, int] = (10, 10), cmap: Optional[str] = "gray" |
| 10 | +) -> None: |
11 | 11 | """ |
12 | | - Display a frame in Jupyter Notebook using Matplotlib |
| 12 | + Plots image using matplotlib. |
13 | 13 |
|
14 | | - Attributes: |
15 | | - frame (np.ndarray): The frame to be displayed. |
16 | | - size (Tuple[int, int]): The size of the plot. default:(10,10) |
17 | | - cmap (str): the colormap to use for single channel images. default:gray |
| 14 | + Args: |
| 15 | + image (np.ndarray): The frame to be displayed. |
| 16 | + size (Tuple[int, int]): The size of the plot. |
| 17 | + cmap (str): the colormap to use for single channel images. |
18 | 18 |
|
19 | 19 | Examples: |
20 | 20 | ```python |
| 21 | + >>> import cv2 |
21 | 22 | >>> import supervision as sv |
22 | 23 |
|
| 24 | + >>> image = cv2.imread("path/to/image.jpg") |
| 25 | +
|
23 | 26 | %matplotlib inline |
24 | | - >>> sv.show_frame_in_notebook(frame, (16, 16)) |
| 27 | + >>> sv.plot_image(image, (16, 16)) |
25 | 28 | ``` |
26 | 29 | """ |
27 | | - if frame.ndim == 2: |
| 30 | + if image.ndim == 2: |
28 | 31 | plt.figure(figsize=size) |
29 | | - plt.imshow(frame, cmap=cmap) |
| 32 | + plt.imshow(image, cmap=cmap) |
30 | 33 | else: |
31 | 34 | plt.figure(figsize=size) |
32 | | - plt.imshow(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) |
| 35 | + plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) |
| 36 | + plt.show() |
| 37 | + |
| 38 | + |
| 39 | +def plot_images_grid( |
| 40 | + images: List[np.ndarray], |
| 41 | + grid_size: Tuple[int, int], |
| 42 | + titles: Optional[List[str]] = None, |
| 43 | + size: Tuple[int, int] = (12, 12), |
| 44 | +) -> None: |
| 45 | + """ |
| 46 | + Plots images in a grid using matplotlib. |
| 47 | +
|
| 48 | + Args: |
| 49 | + images (List[np.ndarray]): A list of images as numpy arrays. |
| 50 | + grid_size (Tuple[int, int]): A tuple specifying the number of rows and columns for the grid. |
| 51 | + titles (Optional[List[str]]): A list of titles for each image. Defaults to None. |
| 52 | + size (Tuple[int, int]): A tuple specifying the width and height of the entire plot in inches. |
| 53 | +
|
| 54 | + Raises: |
| 55 | + ValueError: If the number of images exceeds the grid size. |
| 56 | +
|
| 57 | + Examples: |
| 58 | + ```python |
| 59 | + >>> import cv2 |
| 60 | + >>> import supervision as sv |
| 61 | +
|
| 62 | + >>> image1 = cv2.imread("path/to/image1.jpg") |
| 63 | + >>> image2 = cv2.imread("path/to/image2.jpg") |
| 64 | + >>> image3 = cv2.imread("path/to/image3.jpg") |
| 65 | +
|
| 66 | + >>> images = [image1, image2, image3] |
| 67 | + >>> titles = ["Image 1", "Image 2", "Image 3"] |
| 68 | +
|
| 69 | + %matplotlib inline |
| 70 | + >>> plot_images_grid(images, grid_size=(2, 2), titles=titles, figsize=(16, 16)) |
| 71 | + ``` |
| 72 | + """ |
| 73 | + |
| 74 | + nrows, ncols = grid_size |
| 75 | + |
| 76 | + if len(images) > nrows * ncols: |
| 77 | + raise ValueError( |
| 78 | + "The number of images exceeds the grid size. Please increase the grid size or reduce the number of images." |
| 79 | + ) |
| 80 | + |
| 81 | + fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=size) |
| 82 | + |
| 83 | + for idx, ax in enumerate(axes.flat): |
| 84 | + if idx < len(images): |
| 85 | + ax.imshow(cv2.cvtColor(images[idx], cv2.COLOR_BGR2RGB)) |
| 86 | + if titles is not None and idx < len(titles): |
| 87 | + ax.set_title(titles[idx]) |
| 88 | + ax.axis("off") |
| 89 | + else: |
| 90 | + ax.axis("off") |
| 91 | + |
33 | 92 | plt.show() |
0 commit comments