Spaces:
Running
Running
from typing import * | |
from pathlib import Path | |
import cv2 | |
import numpy as np | |
from scipy.stats import norm | |
def read_image( | |
path: Union[str, Path], | |
image_size: Union[int, Tuple[int, int], None]=None | |
) -> np.ndarray: | |
"""Read image from file path in RGB format. | |
Args: | |
path (str | Path): Path to image file. | |
image_size (int | tuple[int, int] | None, optional): Resize image. | |
Returns: | |
image (np.ndarray): The image array. | |
Example: | |
>>> read_image("/path/to/image.jpg", 256) | |
""" | |
if isinstance(path, Path): | |
path = str(path) | |
image = cv2.imread(path) | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
if image_size: | |
if isinstance(image_size, int): | |
image_size = (image_size, image_size) | |
image = cv2.resize(image, image_size, interpolation=cv2.INTER_AREA) | |
return image | |
def standardize( | |
targets: np.ndarray, | |
mean: float, | |
std: float, | |
center_at: float | None = None | |
) -> np.ndarray: | |
"""Standardize the targets to the z-domain.""" | |
targets = np.log(targets) | |
standardized = (targets - mean) / std | |
if center_at: | |
standardized -= (center_at - mean) / std | |
return standardized | |
def normalize_cdf(targets: np.ndarray, threshold: float) -> np.ndarray: | |
return norm.cdf(targets - threshold) | |
def normalize_min_max( | |
targets: np.ndarray | np.float32, | |
threshold: float | np.ndarray, | |
min_val: float | np.ndarray, | |
max_val: float | np.ndarray | |
) -> np.ndarray: | |
normalized = ((targets - threshold) / (max_val - min_val)) + 0.5 | |
normalized = np.minimum(normalized, 1) | |
normalized = np.maximum(normalized, 0) | |
return normalized | |
def get_boxes(mask: np.ndarray) -> np.ndarray: | |
"""Get bounding boxes from masks. | |
Args: | |
masks (np.ndarray): Input mask of shape (H, W). | |
Returns: | |
boxes (np.ndarray): Array of shape (N, 4) containing bounding boxes in xyxy format. | |
""" | |
_, comps = cv2.connectedComponents(mask) | |
labels = comps = np.unique(comps) | |
boxes = [] | |
for label in labels[labels != 0]: | |
y_loc, x_loc = np.where(comps == label) | |
boxes.append((np.min(x_loc), np.min(y_loc), np.max(x_loc), np.ma(y_loc))) | |
boxes = np.stack(boxes) if boxes else np.empty((0, 4)) | |
return boxes |