Esempio n. 1
0
def compute_box_size_targets(hw_map: np.ndarray, boxes: np.ndarray) -> np.ndarray:
    y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes)
    heights, widths = np_frame_ops.boxes_heights_widths(boxes)
    fm_height, fm_width = hw_map.shape[:2]
    for k, (y, x) in enumerate(zip(y_center, x_center)):
        yi = int(y * fm_height)
        xi = int(x * fm_width)
        hw_map[yi, xi, 0] = heights[k] * fm_height
        hw_map[yi, xi, 1] = widths[k] * fm_width
        hw_map[yi, xi, 2] = 1
    return hw_map
Esempio n. 2
0
def draw_labels_frame_boxes(
    image: Union[np.ndarray, tf.Tensor],
    boxes: Union[np.ndarray, tf.Tensor],
    labels: Union[np.ndarray, tf.Tensor],
    figsize: Tuple[int, int] = (6, 6),
    title: Optional[str] = None,
    num_classes: Optional[int] = None,
    fontsize: int = 10,
    linewidth: float = 2,
    fmt: str = "png",
) -> Image:

    if isinstance(image, tf.Tensor):
        image = image.numpy()
    if isinstance(boxes, tf.Tensor):
        boxes = boxes.numpy()
    if isinstance(labels, tf.Tensor):
        labels = labels.numpy()

    if num_classes is None:
        num_classes = np.max(labels)

    num_boxes = boxes.shape[0]
    label2color = labels_to_colors(num_classes)
    figure = plt.figure(figsize=figsize)
    ax = figure.add_subplot(111, aspect="equal")
    if title is not None:
        plt.title(title)

    if image.dtype != np.uint8 and image.max() > 2:
        image = image / 255.0

    plt.imshow(image)
    height, width = image.shape[:2]
    heights, widths = boxes_heights_widths(boxes.astype(np.float32))
    y_min, x_min = boxes[:, 0], boxes[:, 1]

    for idx in range(num_boxes):
        pos = (int(x_min[idx] * width), int(y_min[idx] * height))
        label = labels[idx]
        color = label2color.get(label, "r")
        plt.text(*pos, f"{label}", color=color, size=fontsize)
        rect = patches.Rectangle(
            pos,
            int(widths[idx] * height),
            int(heights[idx] * width),
            linewidth=linewidth,
            edgecolor=color,
            facecolor="none",
        )
        ax.add_patch(rect)

    return plot_to_image(figure, format=fmt)
 def from_tf_boxes(
     boxes: np.ndarray,
     labels: np.ndarray,
     scores: Optional[np.ndarray] = None,
     classes_scores: Optional[np.ndarray] = None,
 ) -> "BoxDetectionOutput":
     y, c = np_frame_ops.boxes_centers(boxes)
     h, w = np_frame_ops.boxes_heights_widths(boxes)
     boxes = np.stack([h, w, y, c], axis=-1)
     return BoxDetectionOutput(
         boxes=boxes,
         labels=labels,
         scores=scores or np.ones(shape=labels.shape),
         classes_scores=classes_scores or np.ones(shape=labels.shape),
     )
Esempio n. 4
0
def compute_mean_box_size_targets(hw_map: np.ndarray, boxes: np.ndarray) -> np.ndarray:

    y_center, x_center = np_frame_ops.boxes_clipped_centers(boxes)
    heights, widths = np_frame_ops.boxes_heights_widths(boxes)
    fm_height, fm_width = hw_map.shape[:2]

    for k, (y, x) in enumerate(zip(y_center, x_center)):
        yi = int(y * fm_height)
        xi = int(x * fm_width)

        hw_map[yi, xi, 0] += heights[k] * fm_height
        hw_map[yi, xi, 1] += widths[k] * fm_width
        hw_map[yi, xi, 2] += 1

    hw_map[:, :, :2] = hw_map[:, :, :2] / (hw_map[:, :, 2:] + 1e-6)
    # make weights in range (0, 1)
    hw_map[:, :, 2] = np.minimum(hw_map[:, :, 2], 1)

    return hw_map