File size: 743 Bytes
53bec59 | 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 | """Preprocessing utilities."""
import numpy as np
from PIL import Image
from typing import Tuple
def preprocess_image(image: Image.Image, target_size: Tuple[int, int] = (299, 299)) -> np.ndarray:
"""
Preprocess image for model input.
Args:
image: PIL Image
target_size: Target dimensions
Returns:
Preprocessed image array
"""
# Resize
image = image.resize(target_size, Image.LANCZOS)
# Convert to array
img_array = np.array(image) / 255.0
# Normalize
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
img_array = (img_array - mean) / std
return img_array.astype(np.float32)
|