Upload anime_image_classification/preprocess.py with huggingface_hub
Browse files
anime_image_classification/preprocess.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Tuple, Optional
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from imgutils.data import rgb_encode
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _img_encode(image: Image.Image, size: Tuple[int, int] = (384, 384),
|
| 9 |
+
normalize: Optional[Tuple[float, float]] = (0.5, 0.5)):
|
| 10 |
+
image = image.resize(size, Image.BILINEAR)
|
| 11 |
+
data = rgb_encode(image, order_='CHW')
|
| 12 |
+
|
| 13 |
+
if normalize is not None:
|
| 14 |
+
mean_, std_ = normalize
|
| 15 |
+
mean = np.asarray([mean_]).reshape((-1, 1, 1))
|
| 16 |
+
std = np.asarray([std_]).reshape((-1, 1, 1))
|
| 17 |
+
data = (data - mean) / std
|
| 18 |
+
|
| 19 |
+
return data.astype(np.float32)
|