Plana-Archive's picture
Upload anime_image_classification/preprocess.py with huggingface_hub
931f5a2 verified
Raw
History Blame Contribute Delete
592 Bytes
from typing import Tuple, Optional
import numpy as np
from PIL import Image
from imgutils.data import rgb_encode
def _img_encode(image: Image.Image, size: Tuple[int, int] = (384, 384),
normalize: Optional[Tuple[float, float]] = (0.5, 0.5)):
image = image.resize(size, Image.BILINEAR)
data = rgb_encode(image, order_='CHW')
if normalize is not None:
mean_, std_ = normalize
mean = np.asarray([mean_]).reshape((-1, 1, 1))
std = np.asarray([std_]).reshape((-1, 1, 1))
data = (data - mean) / std
return data.astype(np.float32)