2026-01-28 06:16:04 +00:00

33 lines
1.0 KiB
Python

"""Various utilities for preprocess or postprocess computations."""
from typing import List, Tuple
import numpy as np
import numpy.typing as npt
def bbox_to_center_and_scale(
bbox: List[int]) -> Tuple[npt.NDArray[np.float32], npt.NDArray[np.float32]]:
"""Returns the center and dimensions of a given box.
Args:
bbox: List of integers representing a box. Length 4 and is in [x, y, w, h] format.
Returns:
A tuple (center, scale) where center is a NumPy array of size 2 representing the
coordinates of the center of the box and scale is a NumPy array of size 2 representing
the dimensions of the box.
"""
x, y, w, h = bbox
center = np.zeros(2, dtype=np.float32)
center[0] = x + w / 2.0
center[1] = y + h / 2.0
scale = np.array([w, h], dtype=np.float32)
return center, scale
def softmax(values: npt.ArrayLike) -> np.ndarray:
"""Returns softmax of a dataset with dimensions (N, k)."""
exponential = np.exp(values)
return exponential / np.sum(exponential, axis=1, keepdims=True)