YAML Metadata Warning:empty or missing yaml metadata in repo card
Check out the documentation for more information.
MAX_AREA: int = 256 * 256 PATCH: int = 32
def compute_image_dims( h: int, w: int, max_area: int = MAX_AREA, m: int = PATCH ) -> Tuple[Tuple[int, int], Tuple[int, int]]: """((resize_h, resize_w), (crop_h, crop_w))""" smax = min(1.0, math.sqrt(max_area / (h * w))) short, long_ = (h, w) if h <= w else (w, h) short_r = max(m, int((short * smax) // m) * m) s = short_r / short long_r = int(long_ * s) h_r, w_r = (short_r, long_r) if h <= w else (long_r, short_r) h_c = h_r - (h_r % m) w_c = w_r - (w_r % m) return (h_r, w_r), (h_c, w_c)
def process_image(
frame: np.ndarray,
input_format: str = "rgb",
output_format: str = "rgb",
) -> np.ndarray:
"""
Input: (H, W, 3) uint8 image.
Output: (H', W', 3) uint8 image in output_format color space.
For the default 640x480 HMD cameras, output is 192x256x3.
"""
import cv2 # lazy so the module imports without cv2 for the non-image paths
h, w = frame.shape[:2]
(h_r, w_r), (h_c, w_c) = compute_image_dims(h, w)
resized = cv2.resize(frame, (w_r, h_r), interpolation=cv2.INTER_AREA)
y0 = (h_r - h_c) // 2
x0 = (w_r - w_c) // 2
cropped = resized[y0 : y0 + h_c, x0 : x0 + w_c]
in_fmt = input_format.lower()
out_fmt = output_format.lower()
if in_fmt != out_fmt:
if in_fmt == "bgr" and out_fmt == "rgb":
cropped = cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)
elif in_fmt == "rgb" and out_fmt == "bgr":
cropped = cv2.cvtColor(cropped, cv2.COLOR_RGB2BGR)
else:
raise ValueError(f"Unsupported color conversion: {in_fmt} -> {out_fmt}")
return cropped
img = rng.integers(0, 255, size=(480, 640, 3), dtype=np.uint8) out = process_image(img, input_format="bgr", output_format="rgb") assert out.shape == (192, 256, 3), out.shape (hr, wr), (hc, wc) = compute_image_dims(480, 640) assert (hr, wr) == (192, 256) and (hc, wc) == (192, 256)
- Downloads last month
- 13