Spaces:
Runtime error
Runtime error
File size: 1,646 Bytes
2a146da | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | from __future__ import annotations
from typing import Tuple
import numpy as np
def ensure_uint8(image: np.ndarray) -> np.ndarray:
"""
Convert an image into uint8 representation.
"""
image = np.asarray(image)
if image.dtype == np.uint8:
return image
image = image.astype(np.float32)
if image.max() <= 1.0:
image = image * 255.0
image = np.clip(image, 0, 255)
return image.astype(np.uint8)
def normalize_image(image: np.ndarray) -> np.ndarray:
"""
Convert image to float32 [0, 1].
"""
image = ensure_uint8(image)
return image.astype(np.float32) / 255.0
def chw_to_hwc(image: np.ndarray) -> np.ndarray:
"""
Convert CHW tensor/image layout to HWC.
"""
if image.ndim != 3:
raise ValueError(
f"Expected 3D image, received shape {image.shape}"
)
return np.transpose(image, (1, 2, 0))
def hwc_to_chw(image: np.ndarray) -> np.ndarray:
"""
Convert HWC image layout to CHW.
"""
if image.ndim != 3:
raise ValueError(
f"Expected 3D image, received shape {image.shape}"
)
return np.transpose(image, (2, 0, 1))
def resize_shape(
height: int,
width: int,
target_size: Tuple[int, int],
) -> Tuple[int, int]:
"""
Return target spatial dimensions.
"""
if height <= 0 or width <= 0:
raise ValueError("Image dimensions must be positive.")
target_height, target_width = target_size
if target_height <= 0 or target_width <= 0:
raise ValueError("Target dimensions must be positive.")
return target_height, target_width |