CoolFace
Apppublic

killah-t-cell/EditAnything

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
util.py74 linesDownload Raw Back to annotator
1import numpy as np2import cv23import os4 5 6annotator_ckpts_path = os.path.join(os.path.dirname(__file__), 'ckpts')7 8 9def HWC3(x):10    assert x.dtype == np.uint811    if x.ndim == 2:12        x = x[:, :, None]13    assert x.ndim == 314    H, W, C = x.shape15    assert C == 1 or C == 3 or C == 416    if C == 3:17        return x18    if C == 1:19        return np.concatenate([x, x, x], axis=2)20    if C == 4:21        color = x[:, :, 0:3].astype(np.float32)22        alpha = x[:, :, 3:4].astype(np.float32) / 255.023        y = color * alpha + 255.0 * (1.0 - alpha)24        y = y.clip(0, 255).astype(np.uint8)25        return y26 27 28def resize_image(input_image, resolution):29    H, W, C = input_image.shape30    H = float(H)31    W = float(W)32    k = float(resolution) / min(H, W)33    H *= k34    W *= k35    H = int(np.round(H / 64.0)) * 6436    W = int(np.round(W / 64.0)) * 6437    img = cv2.resize(input_image, (W, H), interpolation=cv2.INTER_LANCZOS4 if k > 1 else cv2.INTER_AREA)38    return img39 40def resize_points(clicked_points, original_shape, resolution):41    original_height, original_width, _ = original_shape42    original_height = float(original_height)43    original_width = float(original_width)44    45    scale_factor = float(resolution) / min(original_height, original_width)46    resized_points = []47    48    for point in clicked_points:49        x, y, lab = point50        resized_x = int(round(x * scale_factor))51        resized_y = int(round(y * scale_factor))52        resized_point = (resized_x, resized_y, lab)53        resized_points.append(resized_point)54    55    return resized_points56 57def get_bounding_box(mask):58    # Convert PIL Image to numpy array59    mask = np.array(mask).astype(np.uint8)60 61    # Take the first channel (R) of the mask62    mask = mask[:,:,0]63 64    # Get the indices of elements that are not zero65    rows = np.any(mask, axis=0)66    cols = np.any(mask, axis=1)67    68    # Get the minimum and maximum indices where the elements are not zero69    rmin, rmax = np.where(rows)[0][[0, -1]]70    cmin, cmax = np.where(cols)[0][[0, -1]]71    72    # Return as [xmin, ymin, xmax, ymax]73    return [rmin, cmin, rmax, cmax]74