Theo Viel
commited on
Commit
·
416ba18
1
Parent(s):
897a736
fix doc & typing
Browse files
utils.py
CHANGED
|
@@ -23,22 +23,25 @@ def reformat_for_plotting(
|
|
| 23 |
boxes: npt.NDArray[np.float64],
|
| 24 |
labels: npt.NDArray[np.int_],
|
| 25 |
scores: npt.NDArray[np.float64],
|
| 26 |
-
shape: Tuple[int, int
|
| 27 |
num_classes: int,
|
| 28 |
) -> Tuple[List[npt.NDArray[np.int_]], List[npt.NDArray[np.float64]]]:
|
| 29 |
"""
|
| 30 |
Reformat YOLOX predictions for plotting.
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
Args:
|
| 33 |
-
boxes (np.ndarray): Array of bounding boxes.
|
| 34 |
-
labels (np.ndarray): Array of labels.
|
| 35 |
-
scores (np.ndarray): Array of confidence scores.
|
| 36 |
-
shape (tuple): Shape of the image.
|
| 37 |
num_classes (int): Number of classes.
|
| 38 |
|
| 39 |
Returns:
|
| 40 |
-
list[np.ndarray]: List of box bounding boxes per class.
|
| 41 |
-
list[np.ndarray]: List of confidence scores per class.
|
| 42 |
"""
|
| 43 |
boxes_plot = boxes.copy()
|
| 44 |
boxes_plot[:, [0, 2]] *= shape[1]
|
|
@@ -56,6 +59,7 @@ def plot_sample(
|
|
| 56 |
boxes_list: List[npt.NDArray[np.int_]],
|
| 57 |
confs_list: List[npt.NDArray[np.float64]],
|
| 58 |
labels: List[str],
|
|
|
|
| 59 |
) -> None:
|
| 60 |
"""
|
| 61 |
Plots an image with bounding boxes.
|
|
@@ -66,6 +70,7 @@ def plot_sample(
|
|
| 66 |
boxes_list (list[np.ndarray]): List of box bounding boxes per class.
|
| 67 |
confs_list (list[np.ndarray]): List of confidence scores per class.
|
| 68 |
labels (list): List of class labels.
|
|
|
|
| 69 |
"""
|
| 70 |
plt.imshow(img, cmap="gray")
|
| 71 |
plt.axis(False)
|
|
@@ -90,15 +95,16 @@ def plot_sample(
|
|
| 90 |
plt.gca().add_patch(rect)
|
| 91 |
|
| 92 |
# Add class and index label with proper alignment
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
def postprocess_preds_page_element(
|
|
|
|
| 23 |
boxes: npt.NDArray[np.float64],
|
| 24 |
labels: npt.NDArray[np.int_],
|
| 25 |
scores: npt.NDArray[np.float64],
|
| 26 |
+
shape: Tuple[int, int],
|
| 27 |
num_classes: int,
|
| 28 |
) -> Tuple[List[npt.NDArray[np.int_]], List[npt.NDArray[np.float64]]]:
|
| 29 |
"""
|
| 30 |
Reformat YOLOX predictions for plotting.
|
| 31 |
+
- Unnormalizes boxes to original image size.
|
| 32 |
+
- Reformats boxes to [xmin, ymin, width, height].
|
| 33 |
+
- Converts to list of boxes and scores per class.
|
| 34 |
|
| 35 |
Args:
|
| 36 |
+
boxes (np.ndarray [N, 4]): Array of bounding boxes in format [xmin, ymin, xmax, ymax].
|
| 37 |
+
labels (np.ndarray [N]): Array of labels.
|
| 38 |
+
scores (np.ndarray [N]): Array of confidence scores.
|
| 39 |
+
shape (tuple [2]): Shape of the image (height, width).
|
| 40 |
num_classes (int): Number of classes.
|
| 41 |
|
| 42 |
Returns:
|
| 43 |
+
list[np.ndarray[N]]: List of box bounding boxes per class.
|
| 44 |
+
list[np.ndarray[N]]: List of confidence scores per class.
|
| 45 |
"""
|
| 46 |
boxes_plot = boxes.copy()
|
| 47 |
boxes_plot[:, [0, 2]] *= shape[1]
|
|
|
|
| 59 |
boxes_list: List[npt.NDArray[np.int_]],
|
| 60 |
confs_list: List[npt.NDArray[np.float64]],
|
| 61 |
labels: List[str],
|
| 62 |
+
show_text: bool = True,
|
| 63 |
) -> None:
|
| 64 |
"""
|
| 65 |
Plots an image with bounding boxes.
|
|
|
|
| 70 |
boxes_list (list[np.ndarray]): List of box bounding boxes per class.
|
| 71 |
confs_list (list[np.ndarray]): List of confidence scores per class.
|
| 72 |
labels (list): List of class labels.
|
| 73 |
+
show_text (bool, optional): Whether to show the text. Defaults to True.
|
| 74 |
"""
|
| 75 |
plt.imshow(img, cmap="gray")
|
| 76 |
plt.axis(False)
|
|
|
|
| 95 |
plt.gca().add_patch(rect)
|
| 96 |
|
| 97 |
# Add class and index label with proper alignment
|
| 98 |
+
if show_text:
|
| 99 |
+
plt.text(
|
| 100 |
+
box[0], box[1],
|
| 101 |
+
f"{l}_{box_idx} conf={confs[box_idx]:.3f}",
|
| 102 |
+
color='white',
|
| 103 |
+
fontsize=8,
|
| 104 |
+
bbox=dict(facecolor=col, alpha=1, edgecolor=col, pad=0, linewidth=2),
|
| 105 |
+
verticalalignment='bottom',
|
| 106 |
+
horizontalalignment='left'
|
| 107 |
+
)
|
| 108 |
|
| 109 |
|
| 110 |
def postprocess_preds_page_element(
|