2025-10-13 14:48:00 +02:00
|
|
|
from typing import Sequence
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_anchor_grid(
|
|
|
|
|
num_rows: int,
|
|
|
|
|
num_cols: int,
|
|
|
|
|
scale_factor: float,
|
|
|
|
|
anchor_widths: Sequence[float],
|
|
|
|
|
aspect_ratios: Sequence[float],
|
|
|
|
|
) -> np.ndarray:
|
2025-11-06 15:14:11 +01:00
|
|
|
anchor_grid = np.empty(
|
2025-11-07 11:20:08 +01:00
|
|
|
[len(anchor_widths), len(aspect_ratios), num_rows, num_cols, 4], dtype=float
|
|
|
|
|
)
|
|
|
|
|
for width_idx, ratio_idx, row, col in np.ndindex(anchor_grid.shape[:-1]):
|
2025-11-07 11:15:51 +01:00
|
|
|
anchor_point = (
|
2025-11-07 11:20:08 +01:00
|
|
|
col * scale_factor + scale_factor / 2,
|
|
|
|
|
row * scale_factor + scale_factor / 2,
|
|
|
|
|
)
|
2025-11-07 11:15:51 +01:00
|
|
|
width = anchor_widths[width_idx]
|
|
|
|
|
ratio = aspect_ratios[ratio_idx]
|
|
|
|
|
anchor_grid[width_idx, ratio_idx, row, col] = get_box(
|
2025-11-07 11:20:08 +01:00
|
|
|
width, ratio, anchor_point
|
|
|
|
|
)
|
2025-11-06 15:14:11 +01:00
|
|
|
|
|
|
|
|
return anchor_grid
|
|
|
|
|
|
|
|
|
|
|
2025-11-07 11:20:08 +01:00
|
|
|
def get_box(
|
|
|
|
|
width: float, ratio: float, anchor_point: tuple[float, float]
|
|
|
|
|
) -> np.ndarray:
|
2025-11-07 11:15:51 +01:00
|
|
|
box = np.empty(4, dtype=float)
|
2025-11-06 15:14:11 +01:00
|
|
|
box[0] = anchor_point[0] - (width / 2)
|
|
|
|
|
box[1] = anchor_point[1] - (width * ratio / 2)
|
|
|
|
|
box[2] = anchor_point[0] + (width / 2)
|
|
|
|
|
box[3] = anchor_point[1] + (width * ratio / 2)
|
|
|
|
|
return box
|